Commit 4a4957da authored by Hector Carmona's avatar Hector Carmona Committed by Commit Bot

NUX Google Apps - Add functionality to show bookmarks and bubble.

Bug: 832938
Change-Id: Ibfe28802ae133cc5391eea1b6788067e13468856
Reviewed-on: https://chromium-review.googlesource.com/1141155
Commit-Queue: Hector Carmona <hcarmona@chromium.org>
Reviewed-by: default avatarDominic Battré <battre@chromium.org>
Reviewed-by: default avatarGreg Thompson <grt@chromium.org>
Reviewed-by: default avatarScott Violet <sky@chromium.org>
Reviewed-by: default avatarPeter Kasting <pkasting@chromium.org>
Reviewed-by: default avatarDemetrios Papadopoulos <dpapad@chromium.org>
Cr-Commit-Position: refs/heads/master@{#577983}
parent 5731cb95
......@@ -29,7 +29,7 @@
#if defined(OS_WIN) && defined(GOOGLE_CHROME_BUILD)
#include "components/nux_google_apps/constants.h"
#include "components/nux_google_apps/webui.h"
#include "components/nux_google_apps/google_apps_handler.h"
#endif // defined(OS_WIN) && defined(GOOGLE_CHROME_BUILD)
namespace {
......
......@@ -11,6 +11,10 @@
#include "chrome/grit/generated_resources.h"
#include "components/bookmarks/browser/bookmark_node.h"
std::unique_ptr<ShowPromoDelegate> ShowPromoDelegate::CreatePromoDelegate() {
return std::make_unique<BookmarkBarPromoBubbleView>();
}
struct BookmarkBarPromoBubbleView::BubbleImpl : public FeaturePromoBubbleView {
// Anchors the BookmarkBarPromoBubbleView to |anchor_view|.
// The bubble widget and promo are owned by their native widget.
......
......@@ -21,8 +21,11 @@
#include "ui/base/l10n/l10n_util.h"
#if defined(OS_WIN) && defined(GOOGLE_CHROME_BUILD)
#include "chrome/browser/bookmarks/bookmark_model_factory.h"
#include "components/nux/show_promo_delegate.h"
#include "components/nux_google_apps/constants.h"
#include "components/nux_google_apps/webui.h"
#include "components/nux_google_apps/google_apps_handler.h"
#include "content/public/browser/web_contents.h"
#endif // defined(OS_WIN) && defined(GOOGLE_CHROME_BUILD)
namespace {
......@@ -106,7 +109,14 @@ WelcomeUI::WelcomeUI(content::WebUI* web_ui, const GURL& url)
#if defined(OS_WIN) && defined(GOOGLE_CHROME_BUILD)
if (base::FeatureList::IsEnabled(nux_google_apps::kNuxGoogleAppsFeature)) {
nux_google_apps::AddSources(html_source);
content::BrowserContext* browser_context =
web_ui->GetWebContents()->GetBrowserContext();
web_ui->AddMessageHandler(
std::make_unique<nux_google_apps::GoogleAppsHandler>(
profile->GetPrefs(),
BookmarkModelFactory::GetForBrowserContext(browser_context)));
nux_google_apps::GoogleAppsHandler::AddSources(html_source);
}
#endif // defined(OS_WIN) && defined(GOOGLE_CHROME_BUILD
......
......@@ -5,17 +5,21 @@
#ifndef COMPONENTS_NUX_SHOW_PROMO_DELEGATE_H_
#define COMPONENTS_NUX_SHOW_PROMO_DELEGATE_H_
#include <memory>
namespace bookmarks {
class BookmarkNode;
} // namespace bookmarks
class ShowPromoDelegate {
public:
virtual ~ShowPromoDelegate() = default;
// Shows a promotional popup for the specified bookmark node.
virtual void ShowForNode(const bookmarks::BookmarkNode* node) = 0;
protected:
virtual ~ShowPromoDelegate() = default;
// Return an instance of the promo delegate.
static std::unique_ptr<ShowPromoDelegate> CreatePromoDelegate();
};
#endif // COMPONENTS_NUX_SHOW_PROMO_DELEGATE_H_
......@@ -9,8 +9,8 @@ if (is_win && is_chrome_branded) {
sources = [
"constants.cc",
"constants.h",
"webui.cc",
"webui.h",
"google_apps_handler.cc",
"google_apps_handler.h",
]
public_deps = [
......
include_rules = [
"+components/bookmarks",
"+components/grit",
"+components/prefs",
"+components/nux",
"+components/strings/grit/components_strings.h",
"+content/public/browser",
]
......@@ -2,18 +2,82 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/nux_google_apps/webui.h"
#include "components/nux_google_apps/google_apps_handler.h"
#include "base/bind.h"
#include "base/metrics/field_trial_params.h"
#include "base/stl_util.h"
#include "base/strings/utf_string_conversions.h"
#include "components/bookmarks/browser/bookmark_model.h"
#include "components/bookmarks/common/bookmark_pref_names.h"
#include "components/grit/components_resources.h"
#include "components/grit/components_scaled_resources.h"
#include "components/nux/show_promo_delegate.h"
#include "components/nux_google_apps/constants.h"
#include "components/prefs/pref_service.h"
#include "components/strings/grit/components_strings.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_ui.h"
#include "content/public/browser/web_ui_data_source.h"
namespace nux_google_apps {
void AddSources(content::WebUIDataSource* html_source) {
// Strings in costants not translated because this is an experiment.
// Translate before wide release.
constexpr const char* kGoogleAppNames[] = {
"Gmail", "YouTube", "Maps", "Translate", "News", "Chrome Web Store",
};
constexpr const char* kGoogleAppUrls[] = {
"https://gmail.com", "https://youtube.com",
"https://maps.google.com", "https://translate.google.com",
"https://news.google.com", "https://chrome.google.com/webstore",
};
static_assert(base::size(kGoogleAppNames) == base::size(kGoogleAppUrls),
"names and urls must match");
constexpr const size_t kGoogleAppCount = 6;
GoogleAppsHandler::GoogleAppsHandler(PrefService* prefs,
bookmarks::BookmarkModel* bookmark_model)
: prefs_(prefs), bookmark_model_(bookmark_model) {}
GoogleAppsHandler::~GoogleAppsHandler() {}
void GoogleAppsHandler::RegisterMessages() {
web_ui()->RegisterMessageCallback(
"addGoogleApps",
base::BindRepeating(&GoogleAppsHandler::HandleAddBookmarks,
base::Unretained(this)));
}
void GoogleAppsHandler::HandleAddBookmarks(const base::ListValue* args) {
// Add bookmarks for all selected apps.
int bookmarkIndex = 0;
for (size_t i = 0; i < kGoogleAppCount; ++i) {
bool selected = false;
CHECK(args->GetBoolean(i, &selected));
if (selected) {
bookmark_model_->AddURL(
bookmark_model_->bookmark_bar_node(), bookmarkIndex++,
base::ASCIIToUTF16(kGoogleAppNames[i]), GURL(kGoogleAppUrls[i]));
}
}
// Enable bookmark bar.
prefs_->SetBoolean(bookmarks::prefs::kShowBookmarkBar, true);
// Wait to show bookmark bar.
// TODO(hcarmona): Any advice here would be helpful.
// Show bookmark bubble.
ShowPromoDelegate::CreatePromoDelegate()->ShowForNode(
bookmark_model_->bookmark_bar_node()->GetChild(0));
}
void GoogleAppsHandler::AddSources(content::WebUIDataSource* html_source) {
// Localized strings.
html_source->AddLocalizedString("noThanks", IDS_NO_THANKS);
html_source->AddLocalizedString("getStarted",
......@@ -26,6 +90,11 @@ void AddSources(content::WebUIDataSource* html_source) {
html_source->AddResourcePath("apps/nux_google_apps.js",
IDR_NUX_GOOGLE_APPS_JS);
html_source->AddResourcePath("apps/nux_google_apps_proxy.html",
IDR_NUX_GOOGLE_APPS_PROXY_HTML);
html_source->AddResourcePath("apps/nux_google_apps_proxy.js",
IDR_NUX_GOOGLE_APPS_PROXY_JS);
html_source->AddResourcePath("apps/apps_chooser.html",
IDR_NUX_GOOGLE_APPS_CHOOSER_HTML);
html_source->AddResourcePath("apps/apps_chooser.js",
......@@ -54,4 +123,4 @@ void AddSources(content::WebUIDataSource* html_source) {
IDR_NUX_GOOGLE_APPS_YOUTUBE_2X);
}
} // namespace nux_google_apps
} // namespace nux_google_apps
\ No newline at end of file
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_NUX_GOOGLE_APPS_GOOGLE_APPS_HANDLER_H_
#define COMPONENTS_NUX_GOOGLE_APPS_GOOGLE_APPS_HANDLER_H_
#include "base/macros.h"
#include "base/values.h"
#include "content/public/browser/web_ui_message_handler.h"
class PrefService;
namespace bookmarks {
class BookmarkModel;
} // namespace bookmarks
namespace content {
class WebUIDataSource;
} // namespace content
namespace nux_google_apps {
class GoogleAppsHandler : public content::WebUIMessageHandler {
public:
GoogleAppsHandler(PrefService* prefs,
bookmarks::BookmarkModel* bookmark_model);
~GoogleAppsHandler() override;
// WebUIMessageHandler:
void RegisterMessages() override;
// Callback for JS API that will add bookmarks.
void HandleAddBookmarks(const base::ListValue* args);
// Adds webui sources.
static void AddSources(content::WebUIDataSource* html_source);
private:
// Weak reference.
PrefService* prefs_;
// Weak reference.
bookmarks::BookmarkModel* bookmark_model_;
DISALLOW_COPY_AND_ASSIGN(GoogleAppsHandler);
};
} // namespace nux_google_apps
#endif // COMPONENTS_NUX_GOOGLE_APPS_GOOGLE_APPS_HANDLER_H_
......@@ -18,5 +18,10 @@ js_library("apps_chooser") {
js_library("nux_google_apps") {
deps = [
":apps_chooser",
":nux_google_apps_proxy",
]
}
js_library("nux_google_apps_proxy") {
deps = []
}
......@@ -60,6 +60,20 @@ Polymer({
];
},
},
hasAppsSelected: {
type: Boolean,
notify: true,
value: true,
}
},
/**
* Returns an array of booleans for each selected app.
* @return {Array<boolean>}
*/
getSelectedAppList() {
return this.appList.map(a => a.selected)
},
/**
......@@ -69,5 +83,11 @@ Polymer({
*/
onAppClick_: function(e) {
e.model.set('item.selected', !e.model.item.selected);
this.hasAppsSelected = this.computeHasAppsSelected_();
},
/** @private {boolean} */
computeHasAppsSelected_: function() {
return this.appList.some(a => a.selected);
},
});
......@@ -7,6 +7,7 @@
<link rel="import" href="chrome://resources/cr_elements/paper_button_style_css.html">
<link rel="import" href="chrome://resources/polymer/v1_0/paper-button/paper-button.html">
<link rel="import" href="chrome://welcome/apps/apps_chooser.html">
<link rel="import" href="chrome://welcome/apps/nux_google_apps_proxy.html">
<dom-module id="nux-google-apps">
<template>
......@@ -65,11 +66,15 @@
<h1>$i18n{headerText}</h1>
<div class="description">$i18n{nuxDescription}</div>
<apps-chooser></apps-chooser>
<apps-chooser id="appChooser" has-apps-selected="{{hasAppsSelected_}}">
</apps-chooser>
<div class="button-bar">
<paper-button>$i18n{noThanks}</paper-button>
<paper-button class="action-button">$i18n{getStarted}</paper-button>
<paper-button on-click="onNoThanksClicked_">
$i18n{noThanks}
</paper-button>
<paper-button class="action-button" disabled$="[[!hasAppsSelected_]]"
on-click="onGetStartedClicked_">$i18n{getStarted}</paper-button>
</div>
</div>
</template>
......
......@@ -4,4 +4,21 @@
Polymer({
is: 'nux-google-apps',
properties: {
/** @private */
hasAppsSelected_: Boolean,
},
/** @private */
onNoThanksClicked_: function() {
window.location.replace('chrome://newtab');
},
/** @private */
onGetStartedClicked_: function() {
let selectedApps = this.$.appChooser.getSelectedAppList();
nux.NuxGoogleAppsProxyImpl.getInstance().addGoogleApps(selectedApps);
window.location.replace('chrome://newtab');
},
});
<link rel="import" href="chrome://resources/html/cr.html">
<script src="nux_google_apps_proxy.js"></script>
\ No newline at end of file
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
cr.define('nux', function() {
/** @interface */
class NuxGoogleAppsProxy {
/**
* Adds the selected apps to the bookmark bar.
* @param {!Array<boolean>} selectedApps
*/
addGoogleApps(selectedApps) {}
}
/** @implements {NuxGoogleAppsProxy} */
class NuxGoogleAppsProxyImpl {
/** @override */
addGoogleApps(selectedApps) {
chrome.send('addGoogleApps', selectedApps);
}
}
cr.addSingletonGetter(NuxGoogleAppsProxyImpl);
return {
NuxGoogleAppsProxy: NuxGoogleAppsProxy,
NuxGoogleAppsProxyImpl: NuxGoogleAppsProxyImpl,
};
});
\ No newline at end of file
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_NUX_GOOGLE_APPS_WEBUI_H_
#define COMPONENTS_NUX_GOOGLE_APPS_WEBUI_H_
#include "base/macros.h"
#include "content/public/browser/web_ui_data_source.h"
namespace nux_google_apps {
void AddSources(content::WebUIDataSource* html_source);
} // namespace nux_google_apps
#endif // COMPONENTS_NUX_GOOGLE_APPS_WEBUI_H_
......@@ -2,6 +2,8 @@
<grit-part>
<include name="IDR_NUX_GOOGLE_APPS_HTML" file="../nux_google_apps/resources/nux_google_apps.html" type="BINDATA" />
<include name="IDR_NUX_GOOGLE_APPS_JS" file="../nux_google_apps/resources/nux_google_apps.js" type="BINDATA" />
<include name="IDR_NUX_GOOGLE_APPS_PROXY_HTML" file="../nux_google_apps/resources/nux_google_apps_proxy.html" type="BINDATA" />
<include name="IDR_NUX_GOOGLE_APPS_PROXY_JS" file="../nux_google_apps/resources/nux_google_apps_proxy.js" type="BINDATA" />
<include name="IDR_NUX_GOOGLE_APPS_CHOOSER_HTML" file="../nux_google_apps/resources/apps_chooser.html" type="BINDATA" />
<include name="IDR_NUX_GOOGLE_APPS_CHOOSER_JS" file="../nux_google_apps/resources/apps_chooser.js" type="BINDATA" />
<include name="IDR_NUX_GOOGLE_APPS_CHROME_STORE_1X" file="../nux_google_apps/resources/chrome_store_24dp_1x.png" type="BINDATA" />
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment