1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
|
// Copyright 2013 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 CHROME_BROWSER_UI_SEARCH_SEARCH_IPC_ROUTER_H_
#define CHROME_BROWSER_UI_SEARCH_SEARCH_IPC_ROUTER_H_
#include <memory>
#include <vector>
#include "base/gtest_prod_util.h"
#include "base/macros.h"
#include "base/time/time.h"
#include "chrome/common/instant.mojom.h"
#include "chrome/common/search/instant_types.h"
#include "chrome/common/search/ntp_logging_events.h"
#include "components/ntp_tiles/ntp_tile_source.h"
#include "components/omnibox/common/omnibox_focus_state.h"
#include "content/public/browser/web_contents_binding_set.h"
#include "content/public/browser/web_contents_observer.h"
#include "ui/base/window_open_disposition.h"
class GURL;
namespace content {
class WebContents;
}
class SearchIPCRouterTest;
// SearchIPCRouter is responsible for receiving and sending IPC messages between
// the browser and the Instant page.
class SearchIPCRouter : public content::WebContentsObserver,
public chrome::mojom::Instant {
public:
// SearchIPCRouter calls its delegate in response to messages received from
// the page.
class Delegate {
public:
// Called upon determination of Instant API support in response to the page
// load event.
virtual void OnInstantSupportDetermined(bool supports_instant) = 0;
// Called when the page wants the omnibox to be focused. |state| specifies
// the omnibox focus state.
virtual void FocusOmnibox(OmniboxFocusState state) = 0;
// Called when the SearchBox wants to delete a Most Visited item.
virtual void OnDeleteMostVisitedItem(const GURL& url) = 0;
// Called when the SearchBox wants to undo a Most Visited deletion.
virtual void OnUndoMostVisitedDeletion(const GURL& url) = 0;
// Called when the SearchBox wants to undo all Most Visited deletions.
virtual void OnUndoAllMostVisitedDeletions() = 0;
// Called to signal that an event has occurred on the New Tab Page at a
// particular time since navigation start.
virtual void OnLogEvent(NTPLoggingEventType event,
base::TimeDelta time) = 0;
// Called to log an impression from a given provider on the New Tab Page.
virtual void OnLogMostVisitedImpression(
int position,
ntp_tiles::NTPTileSource tile_source) = 0;
// Called to log a navigation from a given provider on the New Tab Page.
virtual void OnLogMostVisitedNavigation(
int position,
ntp_tiles::NTPTileSource tile_source) = 0;
// Called when the page wants to paste the |text| (or the clipboard contents
// if the |text| is empty) into the omnibox.
virtual void PasteIntoOmnibox(const base::string16& text) = 0;
// Called when the SearchBox wants to verify the signed-in Chrome identity
// against the provided |identity|. Will make a round-trip to the browser
// and eventually return the result through SendChromeIdentityCheckResult.
// Calls SendChromeIdentityCheckResult with true if the identity matches.
virtual void OnChromeIdentityCheck(const base::string16& identity) = 0;
// Called when the SearchBox wants to verify the signed-in Chrome identity
// against the provided |identity|. Will make a round-trip to the browser
// and eventually return the result through SendHistorySyncCheckResult.
// Calls SendHistorySyncCheckResult with true if the user syncs their
// history.
virtual void OnHistorySyncCheck() = 0;
};
// An interface to be implemented by consumers of SearchIPCRouter objects to
// decide whether to process the message received from the page, and vice
// versa (decide whether to send messages to the page).
class Policy {
public:
virtual ~Policy() {}
// SearchIPCRouter calls these functions before sending/receiving messages
// to/from the page.
virtual bool ShouldProcessFocusOmnibox(bool is_active_tab) = 0;
virtual bool ShouldProcessDeleteMostVisitedItem() = 0;
virtual bool ShouldProcessUndoMostVisitedDeletion() = 0;
virtual bool ShouldProcessUndoAllMostVisitedDeletions() = 0;
virtual bool ShouldProcessLogEvent() = 0;
virtual bool ShouldProcessPasteIntoOmnibox(bool is_active_tab) = 0;
virtual bool ShouldProcessChromeIdentityCheck() = 0;
virtual bool ShouldProcessHistorySyncCheck() = 0;
virtual bool ShouldSendSetSuggestionToPrefetch() = 0;
virtual bool ShouldSendSetInputInProgress(bool is_active_tab) = 0;
virtual bool ShouldSendOmniboxFocusChanged() = 0;
virtual bool ShouldSendMostVisitedItems() = 0;
virtual bool ShouldSendThemeBackgroundInfo() = 0;
virtual bool ShouldSubmitQuery() = 0;
};
// Creates chrome::mojom::SearchBox connections on request.
class SearchBoxClientFactory {
public:
SearchBoxClientFactory() = default;
virtual ~SearchBoxClientFactory() = default;
// The returned pointer is owned by the factory.
virtual chrome::mojom::SearchBox* GetSearchBox() = 0;
private:
DISALLOW_COPY_AND_ASSIGN(SearchBoxClientFactory);
};
SearchIPCRouter(content::WebContents* web_contents,
Delegate* delegate,
std::unique_ptr<Policy> policy);
~SearchIPCRouter() override;
// Tells the SearchIPCRouter that a new page in an Instant process committed.
void OnNavigationEntryCommitted();
// Tells the renderer to determine if the page supports the Instant API, which
// results in a call to OnInstantSupportDetermined() when the reply is
// received.
void DetermineIfPageSupportsInstant();
// Tells the renderer about the result of the Chrome identity check.
void SendChromeIdentityCheckResult(const base::string16& identity,
bool identity_match);
// Tells the renderer whether the user syncs history.
void SendHistorySyncCheckResult(bool sync_history);
// Tells the page the suggestion to be prefetched if any.
void SetSuggestionToPrefetch(const InstantSuggestion& suggestion);
// Tells the page that user input started or stopped.
void SetInputInProgress(bool input_in_progress);
// Tells the page that the omnibox focus has changed.
void OmniboxFocusChanged(OmniboxFocusState state,
OmniboxFocusChangeReason reason);
// Tells the renderer about the most visited items.
void SendMostVisitedItems(const std::vector<InstantMostVisitedItem>& items);
// Tells the renderer about the current theme background.
void SendThemeBackgroundInfo(const ThemeBackgroundInfo& theme_info);
// Tells the page that the user pressed Enter in the omnibox.
void Submit(const base::string16& text,
const EmbeddedSearchRequestParams& params);
// Called when the tab corresponding to |this| instance is activated.
void OnTabActivated();
// Called when the tab corresponding to |this| instance is deactivated.
void OnTabDeactivated();
// chrome::mojom::Instant:
void InstantSupportDetermined(int page_seq_no,
bool supports_instant) override;
void FocusOmnibox(int page_id, OmniboxFocusState state) override;
void SearchBoxDeleteMostVisitedItem(int page_seq_no,
const GURL& url) override;
void SearchBoxUndoMostVisitedDeletion(int page_seq_no,
const GURL& url) override;
void SearchBoxUndoAllMostVisitedDeletions(int page_seq_no) override;
void LogEvent(int page_seq_no,
NTPLoggingEventType event,
base::TimeDelta time) override;
void LogMostVisitedImpression(int page_seq_no,
int position,
ntp_tiles::NTPTileSource tile_source) override;
void LogMostVisitedNavigation(int page_seq_no,
int position,
ntp_tiles::NTPTileSource tile_source) override;
void PasteAndOpenDropdown(int page_seq_no,
const base::string16& text) override;
void ChromeIdentityCheck(int page_seq_no,
const base::string16& identity) override;
void HistorySyncCheck(int page_seq_no) override;
void set_search_box_client_factory_for_testing(
std::unique_ptr<SearchBoxClientFactory> factory) {
search_box_client_factory_ = std::move(factory);
}
private:
friend class SearchIPCRouterPolicyTest;
friend class SearchIPCRouterTest;
FRIEND_TEST_ALL_PREFIXES(SearchTabHelperTest,
DetermineIfPageSupportsInstant_Local);
FRIEND_TEST_ALL_PREFIXES(SearchTabHelperTest,
DetermineIfPageSupportsInstant_NonLocal);
FRIEND_TEST_ALL_PREFIXES(SearchTabHelperTest,
PageURLDoesntBelongToInstantRenderer);
FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterTest,
IgnoreMessageIfThePageIsNotActive);
FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterTest, HandleTabChangedEvents);
// Used by unit tests to set a fake delegate.
void set_delegate_for_testing(Delegate* delegate);
// Used by unit tests.
void set_policy_for_testing(std::unique_ptr<Policy> policy);
// Used by unit tests.
Policy* policy_for_testing() const { return policy_.get(); }
// Used by unit tests.
int page_seq_no_for_testing() const { return commit_counter_; }
chrome::mojom::SearchBox* search_box() {
return search_box_client_factory_->GetSearchBox();
}
Delegate* delegate_;
std::unique_ptr<Policy> policy_;
// Holds the number of main frame commits executed in this tab. Used by the
// SearchIPCRouter to ensure that delayed IPC replies are ignored.
int commit_counter_;
// Set to true, when the tab corresponding to |this| instance is active.
bool is_active_tab_;
content::WebContentsFrameBindingSet<chrome::mojom::Instant> bindings_;
std::unique_ptr<SearchBoxClientFactory> search_box_client_factory_;
DISALLOW_COPY_AND_ASSIGN(SearchIPCRouter);
};
#endif // CHROME_BROWSER_UI_SEARCH_SEARCH_IPC_ROUTER_H_
|