Commit 9a51cfb2 authored by kristipark's avatar kristipark Committed by Commit Bot

[NTP] Add isCustomLink property to EmbeddedSearchAPI

The property is used to determine if we need to show the "Restore
default shortcuts" option. Previously this was calculated by checking
the tile source of the most visited items, but this did not account for
the case where there are no available items. As such, if there were no
custom links, the restore default option was disabled.

Bug: 879082
Change-Id: Ia15f4fc863642b520234c061ee5ecf6f7a54c50e
Reviewed-on: https://chromium-review.googlesource.com/1197465
Commit-Queue: Kristi Park <kristipark@chromium.org>
Reviewed-by: default avatarMustafa Emre Acer <meacer@chromium.org>
Reviewed-by: default avatarMathieu Perreault <mathp@chromium.org>
Cr-Commit-Position: refs/heads/master@{#587835}
parent a9fbdb35
...@@ -154,13 +154,6 @@ let isMDEnabled = false; ...@@ -154,13 +154,6 @@ let isMDEnabled = false;
let isCustomLinksEnabled = false; let isCustomLinksEnabled = false;
/**
* True if the tiles to be shown are custom links.
* @type {boolean}
*/
let tilesAreCustomLinks = false;
/** /**
* Log an event on the NTP. * Log an event on the NTP.
* @param {number} eventType Event from LOG_TYPE. * @param {number} eventType Event from LOG_TYPE.
...@@ -213,19 +206,18 @@ var countLoad = function() { ...@@ -213,19 +206,18 @@ var countLoad = function() {
if (loadedCounter <= 0) { if (loadedCounter <= 0) {
swapInNewTiles(); swapInNewTiles();
logEvent(LOG_TYPE.NTP_ALL_TILES_LOADED); logEvent(LOG_TYPE.NTP_ALL_TILES_LOADED);
let tilesAreCustomLinks =
isCustomLinksEnabled && chrome.embeddedSearch.newTabPage.isCustomLinks;
// Note that it's easiest to capture this when all custom links are loaded, // Note that it's easiest to capture this when all custom links are loaded,
// rather than when the impression for each link is logged. // rather than when the impression for each link is logged.
if (isCustomLinksEnabled && tilesAreCustomLinks) { if (tilesAreCustomLinks) {
chrome.embeddedSearch.newTabPage.logEvent( chrome.embeddedSearch.newTabPage.logEvent(
LOG_TYPE.NTP_SHORTCUT_CUSTOMIZED); LOG_TYPE.NTP_SHORTCUT_CUSTOMIZED);
} }
// Tell the parent page whether to show the restore default shortcuts option // Tell the parent page whether to show the restore default shortcuts option
// in the menu. // in the menu.
window.parent.postMessage( window.parent.postMessage(
{ {cmd: 'loaded', showRestoreDefault: tilesAreCustomLinks},
cmd: 'loaded',
showRestoreDefault: (isCustomLinksEnabled && tilesAreCustomLinks)
},
DOMAIN_ORIGIN); DOMAIN_ORIGIN);
tilesAreCustomLinks = false; tilesAreCustomLinks = false;
// Reset to 1, so that any further 'show' message will cause us to swap in // Reset to 1, so that any further 'show' message will cause us to swap in
...@@ -671,9 +663,6 @@ function renderMaterialDesignTile(data) { ...@@ -671,9 +663,6 @@ function renderMaterialDesignTile(data) {
} }
mdTileContainer.className = CLASSES.MD_TILE_CONTAINER; mdTileContainer.className = CLASSES.MD_TILE_CONTAINER;
if (data.isCustomLink)
tilesAreCustomLinks = true;
// The tile will be appended to tiles. // The tile will be appended to tiles.
const position = tiles.children.length; const position = tiles.children.length;
// This is set in the load/error event for the favicon image. // This is set in the load/error event for the favicon image.
......
...@@ -388,8 +388,11 @@ void InstantService::OnURLsAvailable( ...@@ -388,8 +388,11 @@ void InstantService::OnURLsAvailable(
void InstantService::OnIconMadeAvailable(const GURL& site_url) {} void InstantService::OnIconMadeAvailable(const GURL& site_url) {}
void InstantService::NotifyAboutMostVisitedItems() { void InstantService::NotifyAboutMostVisitedItems() {
bool is_custom_links = features::IsCustomLinksEnabled() && most_visited_sites_
? most_visited_sites_->IsCustomLinksInitialized()
: false;
for (InstantServiceObserver& observer : observers_) for (InstantServiceObserver& observer : observers_)
observer.MostVisitedItemsChanged(most_visited_items_); observer.MostVisitedItemsChanged(most_visited_items_, is_custom_links);
} }
void InstantService::NotifyAboutThemeInfo() { void InstantService::NotifyAboutThemeInfo() {
......
...@@ -8,5 +8,5 @@ void InstantServiceObserver::ThemeInfoChanged(const ThemeBackgroundInfo&) { ...@@ -8,5 +8,5 @@ void InstantServiceObserver::ThemeInfoChanged(const ThemeBackgroundInfo&) {
} }
void InstantServiceObserver::MostVisitedItemsChanged( void InstantServiceObserver::MostVisitedItemsChanged(
const std::vector<InstantMostVisitedItem>&) { const std::vector<InstantMostVisitedItem>&,
} bool is_custom_links) {}
...@@ -22,9 +22,11 @@ class InstantServiceObserver { ...@@ -22,9 +22,11 @@ class InstantServiceObserver {
// Indicates that the user's custom theme has changed in some way. // Indicates that the user's custom theme has changed in some way.
virtual void ThemeInfoChanged(const ThemeBackgroundInfo&); virtual void ThemeInfoChanged(const ThemeBackgroundInfo&);
// Indicates that the most visited items has changed. // Indicates that the most visited items has changed. |is_custom_links| is
// true if the items are custom links.
virtual void MostVisitedItemsChanged( virtual void MostVisitedItemsChanged(
const std::vector<InstantMostVisitedItem>&); const std::vector<InstantMostVisitedItem>&,
bool is_custom_links);
protected: protected:
virtual ~InstantServiceObserver() {} virtual ~InstantServiceObserver() {}
......
...@@ -72,8 +72,8 @@ class TestThemeInfoObserver : public InstantServiceObserver { ...@@ -72,8 +72,8 @@ class TestThemeInfoObserver : public InstantServiceObserver {
} }
} }
void MostVisitedItemsChanged( void MostVisitedItemsChanged(const std::vector<InstantMostVisitedItem>&,
const std::vector<InstantMostVisitedItem>&) override {} bool is_custom_links) override {}
InstantService* const service_; InstantService* const service_;
......
...@@ -84,8 +84,8 @@ class TestMostVisitedObserver : public InstantServiceObserver { ...@@ -84,8 +84,8 @@ class TestMostVisitedObserver : public InstantServiceObserver {
private: private:
void ThemeInfoChanged(const ThemeBackgroundInfo&) override {} void ThemeInfoChanged(const ThemeBackgroundInfo&) override {}
void MostVisitedItemsChanged( void MostVisitedItemsChanged(const std::vector<InstantMostVisitedItem>& items,
const std::vector<InstantMostVisitedItem>& items) override { bool is_custom_links) override {
items_ = items; items_ = items;
if (quit_closure_ && items_.size() == expected_count_) { if (quit_closure_ && items_.size() == expected_count_) {
......
...@@ -124,11 +124,12 @@ void SearchIPCRouter::OmniboxFocusChanged(OmniboxFocusState state, ...@@ -124,11 +124,12 @@ void SearchIPCRouter::OmniboxFocusChanged(OmniboxFocusState state,
} }
void SearchIPCRouter::SendMostVisitedItems( void SearchIPCRouter::SendMostVisitedItems(
const std::vector<InstantMostVisitedItem>& items) { const std::vector<InstantMostVisitedItem>& items,
bool is_custom_links) {
if (!policy_->ShouldSendMostVisitedItems()) if (!policy_->ShouldSendMostVisitedItems())
return; return;
embedded_search_client()->MostVisitedChanged(items); embedded_search_client()->MostVisitedChanged(items, is_custom_links);
} }
void SearchIPCRouter::SendThemeBackgroundInfo( void SearchIPCRouter::SendThemeBackgroundInfo(
......
...@@ -178,7 +178,8 @@ class SearchIPCRouter : public content::WebContentsObserver, ...@@ -178,7 +178,8 @@ class SearchIPCRouter : public content::WebContentsObserver,
OmniboxFocusChangeReason reason); OmniboxFocusChangeReason reason);
// Tells the renderer about the most visited items. // Tells the renderer about the most visited items.
void SendMostVisitedItems(const std::vector<InstantMostVisitedItem>& items); void SendMostVisitedItems(const std::vector<InstantMostVisitedItem>& items,
bool is_custom_links);
// Tells the renderer about the current theme background. // Tells the renderer about the current theme background.
void SendThemeBackgroundInfo(const ThemeBackgroundInfo& theme_info); void SendThemeBackgroundInfo(const ThemeBackgroundInfo& theme_info);
......
...@@ -726,9 +726,9 @@ TEST_F(SearchIPCRouterTest, SendMostVisitedItemsMsg) { ...@@ -726,9 +726,9 @@ TEST_F(SearchIPCRouterTest, SendMostVisitedItemsMsg) {
.Times(1) .Times(1)
.WillOnce(Return(true)); .WillOnce(Return(true));
EXPECT_CALL(*mock_embedded_search_client(), MostVisitedChanged(_)); EXPECT_CALL(*mock_embedded_search_client(), MostVisitedChanged(_, false));
GetSearchIPCRouter().SendMostVisitedItems( GetSearchIPCRouter().SendMostVisitedItems(
std::vector<InstantMostVisitedItem>()); std::vector<InstantMostVisitedItem>(), false);
} }
TEST_F(SearchIPCRouterTest, DoNotSendMostVisitedItemsMsg) { TEST_F(SearchIPCRouterTest, DoNotSendMostVisitedItemsMsg) {
...@@ -739,9 +739,10 @@ TEST_F(SearchIPCRouterTest, DoNotSendMostVisitedItemsMsg) { ...@@ -739,9 +739,10 @@ TEST_F(SearchIPCRouterTest, DoNotSendMostVisitedItemsMsg) {
.Times(1) .Times(1)
.WillOnce(Return(false)); .WillOnce(Return(false));
EXPECT_CALL(*mock_embedded_search_client(), MostVisitedChanged(_)).Times(0); EXPECT_CALL(*mock_embedded_search_client(), MostVisitedChanged(_, false))
.Times(0);
GetSearchIPCRouter().SendMostVisitedItems( GetSearchIPCRouter().SendMostVisitedItems(
std::vector<InstantMostVisitedItem>()); std::vector<InstantMostVisitedItem>(), false);
} }
TEST_F(SearchIPCRouterTest, SendThemeBackgroundInfoMsg) { TEST_F(SearchIPCRouterTest, SendThemeBackgroundInfoMsg) {
......
...@@ -239,8 +239,9 @@ void SearchTabHelper::ThemeInfoChanged(const ThemeBackgroundInfo& theme_info) { ...@@ -239,8 +239,9 @@ void SearchTabHelper::ThemeInfoChanged(const ThemeBackgroundInfo& theme_info) {
} }
void SearchTabHelper::MostVisitedItemsChanged( void SearchTabHelper::MostVisitedItemsChanged(
const std::vector<InstantMostVisitedItem>& items) { const std::vector<InstantMostVisitedItem>& items,
ipc_router_.SendMostVisitedItems(items); bool is_custom_links) {
ipc_router_.SendMostVisitedItems(items, is_custom_links);
} }
void SearchTabHelper::FocusOmnibox(OmniboxFocusState state) { void SearchTabHelper::FocusOmnibox(OmniboxFocusState state) {
......
...@@ -125,8 +125,8 @@ class SearchTabHelper : public content::WebContentsObserver, ...@@ -125,8 +125,8 @@ class SearchTabHelper : public content::WebContentsObserver,
// Overridden from InstantServiceObserver: // Overridden from InstantServiceObserver:
void ThemeInfoChanged(const ThemeBackgroundInfo& theme_info) override; void ThemeInfoChanged(const ThemeBackgroundInfo& theme_info) override;
void MostVisitedItemsChanged( void MostVisitedItemsChanged(const std::vector<InstantMostVisitedItem>& items,
const std::vector<InstantMostVisitedItem>& items) override; bool is_custom_links) override;
// Overridden from SelectFileDialog::Listener: // Overridden from SelectFileDialog::Listener:
void FileSelected(const base::FilePath& path, void FileSelected(const base::FilePath& path,
......
...@@ -120,7 +120,7 @@ interface EmbeddedSearchClient { ...@@ -120,7 +120,7 @@ interface EmbeddedSearchClient {
FocusChanged(OmniboxFocusState new_focus_state, FocusChanged(OmniboxFocusState new_focus_state,
OmniboxFocusChangeReason reason); OmniboxFocusChangeReason reason);
MostVisitedChanged(array<InstantMostVisitedItem> items); MostVisitedChanged(array<InstantMostVisitedItem> items, bool is_custom_links);
SetInputInProgress(bool input_in_progress); SetInputInProgress(bool input_in_progress);
......
...@@ -17,8 +17,8 @@ class MockEmbeddedSearchClient : public chrome::mojom::EmbeddedSearchClient { ...@@ -17,8 +17,8 @@ class MockEmbeddedSearchClient : public chrome::mojom::EmbeddedSearchClient {
MOCK_METHOD2(ChromeIdentityCheckResult, void(const base::string16&, bool)); MOCK_METHOD2(ChromeIdentityCheckResult, void(const base::string16&, bool));
MOCK_METHOD2(FocusChanged, void(OmniboxFocusState, OmniboxFocusChangeReason)); MOCK_METHOD2(FocusChanged, void(OmniboxFocusState, OmniboxFocusChangeReason));
MOCK_METHOD1(HistorySyncCheckResult, void(bool)); MOCK_METHOD1(HistorySyncCheckResult, void(bool));
MOCK_METHOD1(MostVisitedChanged, MOCK_METHOD2(MostVisitedChanged,
void(const std::vector<InstantMostVisitedItem>&)); void(const std::vector<InstantMostVisitedItem>&, bool));
MOCK_METHOD1(SetInputInProgress, void(bool)); MOCK_METHOD1(SetInputInProgress, void(bool));
MOCK_METHOD1(ThemeChanged, void(const ThemeBackgroundInfo&)); MOCK_METHOD1(ThemeChanged, void(const ThemeBackgroundInfo&));
MOCK_METHOD0(SelectLocalImageSuccess, void()); MOCK_METHOD0(SelectLocalImageSuccess, void());
......
...@@ -340,6 +340,10 @@ void SearchBox::UndoMostVisitedDeletion( ...@@ -340,6 +340,10 @@ void SearchBox::UndoMostVisitedDeletion(
embedded_search_service_->UndoMostVisitedDeletion(page_seq_no_, url); embedded_search_service_->UndoMostVisitedDeletion(page_seq_no_, url);
} }
bool SearchBox::IsCustomLinks() const {
return is_custom_links_;
}
void SearchBox::AddCustomLink(const GURL& url, const std::string& title) { void SearchBox::AddCustomLink(const GURL& url, const std::string& title) {
if (!url.is_valid()) { if (!url.is_valid()) {
AddCustomLinkResult(false); AddCustomLinkResult(false);
...@@ -477,8 +481,10 @@ void SearchBox::DeleteCustomLinkResult(bool success) { ...@@ -477,8 +481,10 @@ void SearchBox::DeleteCustomLinkResult(bool success) {
} }
void SearchBox::MostVisitedChanged( void SearchBox::MostVisitedChanged(
const std::vector<InstantMostVisitedItem>& items) { const std::vector<InstantMostVisitedItem>& items,
bool is_custom_links) {
has_received_most_visited_ = true; has_received_most_visited_ = true;
is_custom_links_ = is_custom_links;
std::vector<InstantMostVisitedItemIDPair> last_known_items; std::vector<InstantMostVisitedItemIDPair> last_known_items;
GetMostVisitedItems(&last_known_items); GetMostVisitedItems(&last_known_items);
......
...@@ -119,6 +119,9 @@ class SearchBox : public content::RenderFrameObserver, ...@@ -119,6 +119,9 @@ class SearchBox : public content::RenderFrameObserver,
// Sends UndoMostVisitedDeletion to the browser. // Sends UndoMostVisitedDeletion to the browser.
void UndoMostVisitedDeletion(InstantRestrictedID most_visited_item_id); void UndoMostVisitedDeletion(InstantRestrictedID most_visited_item_id);
// Returns true if the most visited items are custom links.
bool IsCustomLinks() const;
// Sends AddCustomLink to the browser. // Sends AddCustomLink to the browser.
void AddCustomLink(const GURL& url, const std::string& title); void AddCustomLink(const GURL& url, const std::string& title);
...@@ -167,8 +170,8 @@ class SearchBox : public content::RenderFrameObserver, ...@@ -167,8 +170,8 @@ class SearchBox : public content::RenderFrameObserver,
void SetPageSequenceNumber(int page_seq_no) override; void SetPageSequenceNumber(int page_seq_no) override;
void FocusChanged(OmniboxFocusState new_focus_state, void FocusChanged(OmniboxFocusState new_focus_state,
OmniboxFocusChangeReason reason) override; OmniboxFocusChangeReason reason) override;
void MostVisitedChanged( void MostVisitedChanged(const std::vector<InstantMostVisitedItem>& items,
const std::vector<InstantMostVisitedItem>& items) override; bool is_custom_links) override;
void SetInputInProgress(bool input_in_progress) override; void SetInputInProgress(bool input_in_progress) override;
void ThemeChanged(const ThemeBackgroundInfo& theme_info) override; void ThemeChanged(const ThemeBackgroundInfo& theme_info) override;
...@@ -206,6 +209,8 @@ class SearchBox : public content::RenderFrameObserver, ...@@ -206,6 +209,8 @@ class SearchBox : public content::RenderFrameObserver,
bool is_key_capture_enabled_; bool is_key_capture_enabled_;
InstantRestrictedIDCache<InstantMostVisitedItem> most_visited_items_cache_; InstantRestrictedIDCache<InstantMostVisitedItem> most_visited_items_cache_;
bool has_received_most_visited_; bool has_received_most_visited_;
// True if the most visited items are custom links.
bool is_custom_links_ = false;
ThemeBackgroundInfo theme_info_; ThemeBackgroundInfo theme_info_;
base::WeakPtrFactory<SearchBox> weak_ptr_factory_; base::WeakPtrFactory<SearchBox> weak_ptr_factory_;
......
...@@ -169,8 +169,6 @@ v8::Local<v8::Object> GenerateMostVisitedItemData( ...@@ -169,8 +169,6 @@ v8::Local<v8::Object> GenerateMostVisitedItemData(
.Set("thumbnailUrl", thumbnail_url) .Set("thumbnailUrl", thumbnail_url)
.Set("tileTitleSource", static_cast<int>(mv_item.title_source)) .Set("tileTitleSource", static_cast<int>(mv_item.title_source))
.Set("tileSource", static_cast<int>(mv_item.source)) .Set("tileSource", static_cast<int>(mv_item.source))
.Set("isCustomLink",
mv_item.source == ntp_tiles::TileSource::CUSTOM_LINKS)
.Set("title", title) .Set("title", title)
.Set("domain", mv_item.url.host()) .Set("domain", mv_item.url.host())
.Set("direction", base::StringPiece(direction)) .Set("direction", base::StringPiece(direction))
...@@ -605,6 +603,7 @@ class NewTabPageBindings : public gin::Wrappable<NewTabPageBindings> { ...@@ -605,6 +603,7 @@ class NewTabPageBindings : public gin::Wrappable<NewTabPageBindings> {
static v8::Local<v8::Value> GetMostVisited(v8::Isolate* isolate); static v8::Local<v8::Value> GetMostVisited(v8::Isolate* isolate);
static bool GetMostVisitedAvailable(v8::Isolate* isolate); static bool GetMostVisitedAvailable(v8::Isolate* isolate);
static v8::Local<v8::Value> GetThemeBackgroundInfo(v8::Isolate* isolate); static v8::Local<v8::Value> GetThemeBackgroundInfo(v8::Isolate* isolate);
static bool GetIsCustomLinks();
// Handlers for JS functions visible to all NTPs. // Handlers for JS functions visible to all NTPs.
static void CheckIsUserSignedInToChromeAs(const std::string& identity); static void CheckIsUserSignedInToChromeAs(const std::string& identity);
...@@ -664,6 +663,7 @@ gin::ObjectTemplateBuilder NewTabPageBindings::GetObjectTemplateBuilder( ...@@ -664,6 +663,7 @@ gin::ObjectTemplateBuilder NewTabPageBindings::GetObjectTemplateBuilder(
&NewTabPageBindings::GetMostVisitedAvailable) &NewTabPageBindings::GetMostVisitedAvailable)
.SetProperty("themeBackgroundInfo", .SetProperty("themeBackgroundInfo",
&NewTabPageBindings::GetThemeBackgroundInfo) &NewTabPageBindings::GetThemeBackgroundInfo)
.SetProperty("isCustomLinks", &NewTabPageBindings::GetIsCustomLinks)
.SetMethod("checkIsUserSignedIntoChromeAs", .SetMethod("checkIsUserSignedIntoChromeAs",
&NewTabPageBindings::CheckIsUserSignedInToChromeAs) &NewTabPageBindings::CheckIsUserSignedInToChromeAs)
.SetMethod("checkIsUserSyncingHistory", .SetMethod("checkIsUserSyncingHistory",
...@@ -759,6 +759,15 @@ v8::Local<v8::Value> NewTabPageBindings::GetThemeBackgroundInfo( ...@@ -759,6 +759,15 @@ v8::Local<v8::Value> NewTabPageBindings::GetThemeBackgroundInfo(
return GenerateThemeBackgroundInfo(isolate, theme_info); return GenerateThemeBackgroundInfo(isolate, theme_info);
} }
// static
bool NewTabPageBindings::GetIsCustomLinks() {
const SearchBox* search_box = GetSearchBoxForCurrentContext();
if (!search_box || !HasOrigin(GURL(chrome::kChromeSearchMostVisitedUrl)))
return false;
return search_box->IsCustomLinks();
}
// static // static
void NewTabPageBindings::CheckIsUserSignedInToChromeAs( void NewTabPageBindings::CheckIsUserSignedInToChromeAs(
const std::string& identity) { const std::string& identity) {
......
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