Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/search/search_ipc_router.h" | 5 #include "chrome/browser/ui/search/search_ipc_router.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/browser/search/instant_service.h" | |
| 11 #include "chrome/browser/search/instant_service_factory.h" | |
| 10 #include "chrome/browser/search/search.h" | 12 #include "chrome/browser/search/search.h" |
| 11 #include "chrome/common/render_messages.h" | 13 #include "chrome/common/render_messages.h" |
| 12 #include "components/search/search.h" | 14 #include "components/search/search.h" |
| 13 #include "content/public/browser/navigation_details.h" | 15 #include "content/public/browser/navigation_details.h" |
| 14 #include "content/public/browser/render_frame_host.h" | 16 #include "content/public/browser/render_frame_host.h" |
| 15 #include "content/public/browser/render_process_host.h" | 17 #include "content/public/browser/render_process_host.h" |
| 16 #include "content/public/browser/web_contents.h" | 18 #include "content/public/browser/web_contents.h" |
| 17 #include "content/public/common/associated_interface_provider.h" | 19 #include "content/public/common/associated_interface_provider.h" |
| 18 #include "content/public/common/child_process_host.h" | 20 #include "content/public/common/child_process_host.h" |
| 19 | 21 |
| 20 namespace { | 22 namespace { |
| 21 | 23 |
| 22 bool IsRenderedInInstantProcess(content::WebContents* web_contents) { | 24 bool IsInstantProcess(content::RenderFrameHost* render_frame) { |
|
Marc Treib
2017/02/13 09:35:56
nit: IsInInstantProcess maybe?
tibell
2017/02/19 23:18:45
Done.
| |
| 23 // TODO(tibell): Instead of rejecting messages check at connection time if we | 25 content::RenderProcessHost* process_host = render_frame->GetProcess(); |
| 24 // want to talk to the render frame or not. Later, in an out-of-process iframe | 26 const InstantService* instant_service = InstantServiceFactory::GetForProfile( |
| 25 // world, the IsRenderedInInstantProcess check will have to be done, as it's | 27 Profile::FromBrowserContext(process_host->GetBrowserContext())); |
| 26 // based on the RenderView. | 28 if (!instant_service) |
| 27 Profile* profile = | 29 return false; |
| 28 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 30 |
| 29 return search::IsRenderedInInstantProcess(web_contents, profile); | 31 return instant_service->IsInstantProcess(process_host->GetID()); |
| 30 } | 32 } |
| 31 | 33 |
| 32 class SearchBoxClientFactoryImpl | 34 class SearchBoxClientFactoryImpl |
| 33 : public SearchIPCRouter::SearchBoxClientFactory { | 35 : public SearchIPCRouter::SearchBoxClientFactory, |
| 36 public chrome::mojom::EmbeddedSearchConnector { | |
| 34 public: | 37 public: |
| 35 // The |web_contents| must outlive this object. | 38 // |web_contents| and |binding| must outlive this object. |
| 36 explicit SearchBoxClientFactoryImpl(content::WebContents* web_contents) | 39 SearchBoxClientFactoryImpl( |
| 37 : web_contents_(web_contents), | 40 content::WebContents* web_contents, |
| 38 last_connected_rfh_( | 41 mojo::AssociatedBinding<chrome::mojom::Instant>* binding) |
| 39 std::make_pair(content::ChildProcessHost::kInvalidUniqueID, | 42 : binding_(binding), bindings_(web_contents, this) { |
|
Marc Treib
2017/02/13 09:35:56
There's both "binding_" and "bindings_" which I fi
tibell
2017/02/19 23:18:45
Done.
| |
| 40 MSG_ROUTING_NONE)) {} | 43 DCHECK(web_contents); |
| 41 chrome::mojom::SearchBox* GetSearchBox() override; | 44 DCHECK(binding); |
| 45 // Before we are connected to a frame we throw away all messages. | |
| 46 mojo::GetIsolatedProxy(&search_box_); | |
| 47 } | |
| 48 | |
| 49 chrome::mojom::SearchBox* GetSearchBox() override { | |
| 50 return search_box_.get(); | |
| 51 } | |
| 42 | 52 |
| 43 private: | 53 private: |
| 44 void OnConnectionError(); | 54 void Connect(chrome::mojom::InstantAssociatedRequest request, |
| 55 chrome::mojom::SearchBoxAssociatedPtrInfo client) override; | |
| 45 | 56 |
| 46 content::WebContents* web_contents_; | 57 // An interface used to push updates to the frame that connected to us. Before |
| 58 // we've been connected to a frame, messages sent on this interface go into | |
| 59 // the void. | |
| 47 chrome::mojom::SearchBoxAssociatedPtr search_box_; | 60 chrome::mojom::SearchBoxAssociatedPtr search_box_; |
| 48 | 61 |
| 49 // The proccess ID and routing ID of the last connected main frame. May be | 62 // Used to bind incoming interface requests to the implementation, which lives |
| 50 // (ChildProcessHost::kInvalidUniqueID, MSG_ROUTING_NONE) if we haven't | 63 // in SearchIPCRouter. |
| 51 // connected yet. | 64 mojo::AssociatedBinding<chrome::mojom::Instant>* binding_; |
| 52 std::pair<int, int> last_connected_rfh_; | 65 |
| 66 // Binding used to listen to connection requests. | |
| 67 content::WebContentsFrameBindingSet<chrome::mojom::EmbeddedSearchConnector> | |
| 68 bindings_; | |
| 53 | 69 |
| 54 DISALLOW_COPY_AND_ASSIGN(SearchBoxClientFactoryImpl); | 70 DISALLOW_COPY_AND_ASSIGN(SearchBoxClientFactoryImpl); |
| 55 }; | 71 }; |
| 56 | 72 |
| 57 chrome::mojom::SearchBox* SearchBoxClientFactoryImpl::GetSearchBox() { | 73 void SearchBoxClientFactoryImpl::Connect( |
| 58 content::RenderFrameHost* frame = web_contents_->GetMainFrame(); | 74 chrome::mojom::InstantAssociatedRequest request, |
| 59 auto id = std::make_pair(frame->GetProcess()->GetID(), frame->GetRoutingID()); | 75 chrome::mojom::SearchBoxAssociatedPtrInfo client) { |
| 60 // Avoid reconnecting repeatedly to the same RenderFrameHost for performance | 76 if (!IsInstantProcess(bindings_.GetCurrentTargetFrame())) { |
| 61 // reasons. | 77 return; |
| 62 if (id != last_connected_rfh_) { | |
| 63 if (IsRenderedInInstantProcess(web_contents_)) { | |
| 64 frame->GetRemoteAssociatedInterfaces()->GetInterface(&search_box_); | |
| 65 search_box_.set_connection_error_handler( | |
| 66 base::Bind(&SearchBoxClientFactoryImpl::OnConnectionError, | |
| 67 base::Unretained(this))); | |
| 68 } else { | |
| 69 // Renderer is not an instant process. We'll create a connection that | |
| 70 // drops all messages. | |
| 71 mojo::GetIsolatedProxy(&search_box_); | |
| 72 } | |
| 73 last_connected_rfh_ = id; | |
| 74 } | 78 } |
| 75 return search_box_.get(); | 79 binding_->Bind(std::move(request)); |
| 76 } | 80 search_box_.Bind(std::move(client)); |
| 77 | |
| 78 void SearchBoxClientFactoryImpl::OnConnectionError() { | |
| 79 search_box_.reset(); | |
| 80 last_connected_rfh_ = std::make_pair( | |
| 81 content::ChildProcessHost::kInvalidUniqueID, | |
| 82 MSG_ROUTING_NONE); | |
| 83 } | 81 } |
| 84 | 82 |
| 85 } // namespace | 83 } // namespace |
| 86 | 84 |
| 87 SearchIPCRouter::SearchIPCRouter(content::WebContents* web_contents, | 85 SearchIPCRouter::SearchIPCRouter(content::WebContents* web_contents, |
| 88 Delegate* delegate, | 86 Delegate* delegate, |
| 89 std::unique_ptr<Policy> policy) | 87 std::unique_ptr<Policy> policy) |
| 90 : WebContentsObserver(web_contents), | 88 : WebContentsObserver(web_contents), |
| 91 delegate_(delegate), | 89 delegate_(delegate), |
| 92 policy_(std::move(policy)), | 90 policy_(std::move(policy)), |
| 93 commit_counter_(0), | 91 commit_counter_(0), |
| 94 is_active_tab_(false), | 92 is_active_tab_(false), |
| 95 bindings_(web_contents, this), | 93 binding_(this), |
| 96 search_box_client_factory_(new SearchBoxClientFactoryImpl(web_contents)) { | 94 search_box_client_factory_( |
| 95 new SearchBoxClientFactoryImpl(web_contents, &binding_)) { | |
| 97 DCHECK(web_contents); | 96 DCHECK(web_contents); |
| 98 DCHECK(delegate); | 97 DCHECK(delegate); |
| 99 DCHECK(policy_.get()); | 98 DCHECK(policy_.get()); |
| 100 } | 99 } |
| 101 | 100 |
| 102 SearchIPCRouter::~SearchIPCRouter() {} | 101 SearchIPCRouter::~SearchIPCRouter() = default; |
| 103 | 102 |
| 104 void SearchIPCRouter::OnNavigationEntryCommitted() { | 103 void SearchIPCRouter::OnNavigationEntryCommitted() { |
| 105 ++commit_counter_; | 104 ++commit_counter_; |
| 106 search_box()->SetPageSequenceNumber(commit_counter_); | 105 search_box()->SetPageSequenceNumber(commit_counter_); |
| 107 } | 106 } |
| 108 | 107 |
| 109 void SearchIPCRouter::DetermineIfPageSupportsInstant() { | 108 void SearchIPCRouter::DetermineIfPageSupportsInstant() { |
| 110 search_box()->DetermineIfPageSupportsInstant(); | 109 search_box()->DetermineIfPageSupportsInstant(); |
| 111 } | 110 } |
| 112 | 111 |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 176 void SearchIPCRouter::OnTabActivated() { | 175 void SearchIPCRouter::OnTabActivated() { |
| 177 is_active_tab_ = true; | 176 is_active_tab_ = true; |
| 178 } | 177 } |
| 179 | 178 |
| 180 void SearchIPCRouter::OnTabDeactivated() { | 179 void SearchIPCRouter::OnTabDeactivated() { |
| 181 is_active_tab_ = false; | 180 is_active_tab_ = false; |
| 182 } | 181 } |
| 183 | 182 |
| 184 void SearchIPCRouter::InstantSupportDetermined(int page_seq_no, | 183 void SearchIPCRouter::InstantSupportDetermined(int page_seq_no, |
| 185 bool instant_support) { | 184 bool instant_support) { |
| 186 if (!IsRenderedInInstantProcess(web_contents())) | |
| 187 return; | |
| 188 if (page_seq_no != commit_counter_) | 185 if (page_seq_no != commit_counter_) |
| 189 return; | 186 return; |
| 190 | 187 |
| 191 delegate_->OnInstantSupportDetermined(instant_support); | 188 delegate_->OnInstantSupportDetermined(instant_support); |
| 192 } | 189 } |
| 193 | 190 |
| 194 void SearchIPCRouter::FocusOmnibox(int page_seq_no, OmniboxFocusState state) { | 191 void SearchIPCRouter::FocusOmnibox(int page_seq_no, OmniboxFocusState state) { |
| 195 if (!IsRenderedInInstantProcess(web_contents())) | |
| 196 return; | |
| 197 if (page_seq_no != commit_counter_) | 192 if (page_seq_no != commit_counter_) |
| 198 return; | 193 return; |
| 199 | 194 |
| 200 delegate_->OnInstantSupportDetermined(true); | 195 delegate_->OnInstantSupportDetermined(true); |
| 201 if (!policy_->ShouldProcessFocusOmnibox(is_active_tab_)) | 196 if (!policy_->ShouldProcessFocusOmnibox(is_active_tab_)) |
| 202 return; | 197 return; |
| 203 | 198 |
| 204 delegate_->FocusOmnibox(state); | 199 delegate_->FocusOmnibox(state); |
| 205 } | 200 } |
| 206 | 201 |
| 207 void SearchIPCRouter::DeleteMostVisitedItem(int page_seq_no, const GURL& url) { | 202 void SearchIPCRouter::DeleteMostVisitedItem(int page_seq_no, const GURL& url) { |
| 208 if (!IsRenderedInInstantProcess(web_contents())) | |
| 209 return; | |
| 210 if (page_seq_no != commit_counter_) | 203 if (page_seq_no != commit_counter_) |
| 211 return; | 204 return; |
| 212 | 205 |
| 213 delegate_->OnInstantSupportDetermined(true); | 206 delegate_->OnInstantSupportDetermined(true); |
| 214 if (!policy_->ShouldProcessDeleteMostVisitedItem()) | 207 if (!policy_->ShouldProcessDeleteMostVisitedItem()) |
| 215 return; | 208 return; |
| 216 | 209 |
| 217 delegate_->OnDeleteMostVisitedItem(url); | 210 delegate_->OnDeleteMostVisitedItem(url); |
| 218 } | 211 } |
| 219 | 212 |
| 220 void SearchIPCRouter::UndoMostVisitedDeletion(int page_seq_no, | 213 void SearchIPCRouter::UndoMostVisitedDeletion(int page_seq_no, |
| 221 const GURL& url) { | 214 const GURL& url) { |
| 222 if (!IsRenderedInInstantProcess(web_contents())) | |
| 223 return; | |
| 224 if (page_seq_no != commit_counter_) | 215 if (page_seq_no != commit_counter_) |
| 225 return; | 216 return; |
| 226 | 217 |
| 227 delegate_->OnInstantSupportDetermined(true); | 218 delegate_->OnInstantSupportDetermined(true); |
| 228 if (!policy_->ShouldProcessUndoMostVisitedDeletion()) | 219 if (!policy_->ShouldProcessUndoMostVisitedDeletion()) |
| 229 return; | 220 return; |
| 230 | 221 |
| 231 delegate_->OnUndoMostVisitedDeletion(url); | 222 delegate_->OnUndoMostVisitedDeletion(url); |
| 232 } | 223 } |
| 233 | 224 |
| 234 void SearchIPCRouter::UndoAllMostVisitedDeletions(int page_seq_no) { | 225 void SearchIPCRouter::UndoAllMostVisitedDeletions(int page_seq_no) { |
| 235 if (!IsRenderedInInstantProcess(web_contents())) | |
| 236 return; | |
| 237 if (page_seq_no != commit_counter_) | 226 if (page_seq_no != commit_counter_) |
| 238 return; | 227 return; |
| 239 | 228 |
| 240 delegate_->OnInstantSupportDetermined(true); | 229 delegate_->OnInstantSupportDetermined(true); |
| 241 if (!policy_->ShouldProcessUndoAllMostVisitedDeletions()) | 230 if (!policy_->ShouldProcessUndoAllMostVisitedDeletions()) |
| 242 return; | 231 return; |
| 243 | 232 |
| 244 delegate_->OnUndoAllMostVisitedDeletions(); | 233 delegate_->OnUndoAllMostVisitedDeletions(); |
| 245 } | 234 } |
| 246 | 235 |
| 247 void SearchIPCRouter::LogEvent(int page_seq_no, | 236 void SearchIPCRouter::LogEvent(int page_seq_no, |
| 248 NTPLoggingEventType event, | 237 NTPLoggingEventType event, |
| 249 base::TimeDelta time) { | 238 base::TimeDelta time) { |
| 250 if (!IsRenderedInInstantProcess(web_contents())) | |
| 251 return; | |
| 252 if (page_seq_no != commit_counter_) | 239 if (page_seq_no != commit_counter_) |
| 253 return; | 240 return; |
| 254 | 241 |
| 255 delegate_->OnInstantSupportDetermined(true); | 242 delegate_->OnInstantSupportDetermined(true); |
| 256 if (!policy_->ShouldProcessLogEvent()) | 243 if (!policy_->ShouldProcessLogEvent()) |
| 257 return; | 244 return; |
| 258 | 245 |
| 259 delegate_->OnLogEvent(event, time); | 246 delegate_->OnLogEvent(event, time); |
| 260 } | 247 } |
| 261 | 248 |
| 262 void SearchIPCRouter::LogMostVisitedImpression( | 249 void SearchIPCRouter::LogMostVisitedImpression( |
| 263 int page_seq_no, | 250 int page_seq_no, |
| 264 int position, | 251 int position, |
| 265 ntp_tiles::NTPTileSource tile_source) { | 252 ntp_tiles::NTPTileSource tile_source) { |
| 266 if (!IsRenderedInInstantProcess(web_contents())) | |
| 267 return; | |
| 268 if (page_seq_no != commit_counter_) | 253 if (page_seq_no != commit_counter_) |
| 269 return; | 254 return; |
| 270 | 255 |
| 271 delegate_->OnInstantSupportDetermined(true); | 256 delegate_->OnInstantSupportDetermined(true); |
| 272 // Logging impressions is controlled by the same policy as logging events. | 257 // Logging impressions is controlled by the same policy as logging events. |
| 273 if (!policy_->ShouldProcessLogEvent()) | 258 if (!policy_->ShouldProcessLogEvent()) |
| 274 return; | 259 return; |
| 275 | 260 |
| 276 delegate_->OnLogMostVisitedImpression(position, tile_source); | 261 delegate_->OnLogMostVisitedImpression(position, tile_source); |
| 277 } | 262 } |
| 278 | 263 |
| 279 void SearchIPCRouter::LogMostVisitedNavigation( | 264 void SearchIPCRouter::LogMostVisitedNavigation( |
| 280 int page_seq_no, | 265 int page_seq_no, |
| 281 int position, | 266 int position, |
| 282 ntp_tiles::NTPTileSource tile_source) { | 267 ntp_tiles::NTPTileSource tile_source) { |
| 283 if (!IsRenderedInInstantProcess(web_contents())) | |
| 284 return; | |
| 285 if (page_seq_no != commit_counter_) | 268 if (page_seq_no != commit_counter_) |
| 286 return; | 269 return; |
| 287 | 270 |
| 288 delegate_->OnInstantSupportDetermined(true); | 271 delegate_->OnInstantSupportDetermined(true); |
| 289 // Logging navigations is controlled by the same policy as logging events. | 272 // Logging navigations is controlled by the same policy as logging events. |
| 290 if (!policy_->ShouldProcessLogEvent()) | 273 if (!policy_->ShouldProcessLogEvent()) |
| 291 return; | 274 return; |
| 292 | 275 |
| 293 delegate_->OnLogMostVisitedNavigation(position, tile_source); | 276 delegate_->OnLogMostVisitedNavigation(position, tile_source); |
| 294 } | 277 } |
| 295 | 278 |
| 296 void SearchIPCRouter::PasteAndOpenDropdown(int page_seq_no, | 279 void SearchIPCRouter::PasteAndOpenDropdown(int page_seq_no, |
| 297 const base::string16& text) { | 280 const base::string16& text) { |
| 298 if (!IsRenderedInInstantProcess(web_contents())) | |
| 299 return; | |
| 300 if (page_seq_no != commit_counter_) | 281 if (page_seq_no != commit_counter_) |
| 301 return; | 282 return; |
| 302 | 283 |
| 303 delegate_->OnInstantSupportDetermined(true); | 284 delegate_->OnInstantSupportDetermined(true); |
| 304 if (!policy_->ShouldProcessPasteIntoOmnibox(is_active_tab_)) | 285 if (!policy_->ShouldProcessPasteIntoOmnibox(is_active_tab_)) |
| 305 return; | 286 return; |
| 306 | 287 |
| 307 delegate_->PasteIntoOmnibox(text); | 288 delegate_->PasteIntoOmnibox(text); |
| 308 } | 289 } |
| 309 | 290 |
| 310 void SearchIPCRouter::ChromeIdentityCheck(int page_seq_no, | 291 void SearchIPCRouter::ChromeIdentityCheck(int page_seq_no, |
| 311 const base::string16& identity) { | 292 const base::string16& identity) { |
| 312 if (!IsRenderedInInstantProcess(web_contents())) | |
| 313 return; | |
| 314 if (page_seq_no != commit_counter_) | 293 if (page_seq_no != commit_counter_) |
| 315 return; | 294 return; |
| 316 | 295 |
| 317 delegate_->OnInstantSupportDetermined(true); | 296 delegate_->OnInstantSupportDetermined(true); |
| 318 if (!policy_->ShouldProcessChromeIdentityCheck()) | 297 if (!policy_->ShouldProcessChromeIdentityCheck()) |
| 319 return; | 298 return; |
| 320 | 299 |
| 321 delegate_->OnChromeIdentityCheck(identity); | 300 delegate_->OnChromeIdentityCheck(identity); |
| 322 } | 301 } |
| 323 | 302 |
| 324 void SearchIPCRouter::HistorySyncCheck(int page_seq_no) { | 303 void SearchIPCRouter::HistorySyncCheck(int page_seq_no) { |
| 325 if (!IsRenderedInInstantProcess(web_contents())) | |
| 326 return; | |
| 327 if (page_seq_no != commit_counter_) | 304 if (page_seq_no != commit_counter_) |
| 328 return; | 305 return; |
| 329 | 306 |
| 330 delegate_->OnInstantSupportDetermined(true); | 307 delegate_->OnInstantSupportDetermined(true); |
| 331 if (!policy_->ShouldProcessHistorySyncCheck()) | 308 if (!policy_->ShouldProcessHistorySyncCheck()) |
| 332 return; | 309 return; |
| 333 | 310 |
| 334 delegate_->OnHistorySyncCheck(); | 311 delegate_->OnHistorySyncCheck(); |
| 335 } | 312 } |
| 336 | 313 |
| 337 void SearchIPCRouter::set_delegate_for_testing(Delegate* delegate) { | 314 void SearchIPCRouter::set_delegate_for_testing(Delegate* delegate) { |
| 338 DCHECK(delegate); | 315 DCHECK(delegate); |
| 339 delegate_ = delegate; | 316 delegate_ = delegate; |
| 340 } | 317 } |
| 341 | 318 |
| 342 void SearchIPCRouter::set_policy_for_testing(std::unique_ptr<Policy> policy) { | 319 void SearchIPCRouter::set_policy_for_testing(std::unique_ptr<Policy> policy) { |
| 343 DCHECK(policy); | 320 DCHECK(policy); |
| 344 policy_ = std::move(policy); | 321 policy_ = std::move(policy); |
| 345 } | 322 } |
| OLD | NEW |