OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 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_tab_helper.h" | 5 #include "chrome/browser/ui/search/search_tab_helper.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <set> | 8 #include <set> |
9 | 9 |
10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
11 #include "base/metrics/histogram_macros.h" | 11 #include "base/metrics/histogram_macros.h" |
12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
13 #include "build/build_config.h" | 13 #include "build/build_config.h" |
14 #include "chrome/browser/chrome_notification_types.h" | 14 #include "chrome/browser/chrome_notification_types.h" |
15 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
16 #include "chrome/browser/search/instant_service.h" | 16 #include "chrome/browser/search/instant_service.h" |
17 #include "chrome/browser/search/instant_service_factory.h" | 17 #include "chrome/browser/search/instant_service_factory.h" |
18 #include "chrome/browser/search/search.h" | 18 #include "chrome/browser/search/search.h" |
19 #include "chrome/browser/signin/signin_manager_factory.h" | 19 #include "chrome/browser/signin/signin_manager_factory.h" |
20 #include "chrome/browser/sync/profile_sync_service_factory.h" | 20 #include "chrome/browser/sync/profile_sync_service_factory.h" |
21 #include "chrome/browser/ui/browser_window.h" | 21 #include "chrome/browser/ui/browser_window.h" |
22 #include "chrome/browser/ui/location_bar/location_bar.h" | 22 #include "chrome/browser/ui/location_bar/location_bar.h" |
23 #include "chrome/browser/ui/omnibox/clipboard_utils.h" | 23 #include "chrome/browser/ui/omnibox/clipboard_utils.h" |
24 #include "chrome/browser/ui/search/instant_tab.h" | 24 #include "chrome/browser/ui/search/instant_tab.h" |
25 #include "chrome/browser/ui/search/search_ipc_router_policy_impl.h" | 25 #include "chrome/browser/ui/search/search_ipc_router_policy_impl.h" |
26 #include "chrome/browser/ui/search/search_tab_helper_delegate.h" | |
27 #include "chrome/browser/ui/tab_contents/core_tab_helper.h" | 26 #include "chrome/browser/ui/tab_contents/core_tab_helper.h" |
28 #include "chrome/browser/ui/webui/ntp/ntp_user_data_logger.h" | 27 #include "chrome/browser/ui/webui/ntp/ntp_user_data_logger.h" |
29 #include "chrome/common/url_constants.h" | 28 #include "chrome/common/url_constants.h" |
30 #include "chrome/grit/generated_resources.h" | 29 #include "chrome/grit/generated_resources.h" |
31 #include "components/browser_sync/profile_sync_service.h" | 30 #include "components/browser_sync/profile_sync_service.h" |
32 #include "components/google/core/browser/google_util.h" | 31 #include "components/google/core/browser/google_util.h" |
33 #include "components/omnibox/browser/omnibox_edit_model.h" | 32 #include "components/omnibox/browser/omnibox_edit_model.h" |
34 #include "components/omnibox/browser/omnibox_popup_model.h" | 33 #include "components/omnibox/browser/omnibox_popup_model.h" |
35 #include "components/omnibox/browser/omnibox_view.h" | 34 #include "components/omnibox/browser/omnibox_view.h" |
36 #include "components/search/search.h" | 35 #include "components/search/search.h" |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 } // namespace | 133 } // namespace |
135 | 134 |
136 SearchTabHelper::SearchTabHelper(content::WebContents* web_contents) | 135 SearchTabHelper::SearchTabHelper(content::WebContents* web_contents) |
137 : WebContentsObserver(web_contents), | 136 : WebContentsObserver(web_contents), |
138 is_search_enabled_(search::IsInstantExtendedAPIEnabled()), | 137 is_search_enabled_(search::IsInstantExtendedAPIEnabled()), |
139 web_contents_(web_contents), | 138 web_contents_(web_contents), |
140 ipc_router_( | 139 ipc_router_( |
141 web_contents, | 140 web_contents, |
142 this, | 141 this, |
143 base::WrapUnique(new SearchIPCRouterPolicyImpl(web_contents))), | 142 base::WrapUnique(new SearchIPCRouterPolicyImpl(web_contents))), |
144 instant_service_(NULL), | 143 instant_service_(nullptr), |
145 delegate_(NULL) { | 144 omnibox_view_(nullptr) { |
146 if (!is_search_enabled_) | 145 if (!is_search_enabled_) |
147 return; | 146 return; |
148 | 147 |
149 instant_service_ = | 148 instant_service_ = |
150 InstantServiceFactory::GetForProfile( | 149 InstantServiceFactory::GetForProfile( |
151 Profile::FromBrowserContext(web_contents_->GetBrowserContext())); | 150 Profile::FromBrowserContext(web_contents_->GetBrowserContext())); |
152 if (instant_service_) | 151 if (instant_service_) |
153 instant_service_->AddObserver(this); | 152 instant_service_->AddObserver(this); |
154 } | 153 } |
155 | 154 |
(...skipping 14 matching lines...) Expand all Loading... |
170 content::NotificationService::current()->Notify( | 169 content::NotificationService::current()->Notify( |
171 chrome::NOTIFICATION_OMNIBOX_FOCUS_CHANGED, | 170 chrome::NOTIFICATION_OMNIBOX_FOCUS_CHANGED, |
172 content::Source<SearchTabHelper>(this), | 171 content::Source<SearchTabHelper>(this), |
173 content::NotificationService::NoDetails()); | 172 content::NotificationService::NoDetails()); |
174 | 173 |
175 ipc_router_.OmniboxFocusChanged(state, reason); | 174 ipc_router_.OmniboxFocusChanged(state, reason); |
176 | 175 |
177 // Don't send oninputstart/oninputend updates in response to focus changes | 176 // Don't send oninputstart/oninputend updates in response to focus changes |
178 // if there's a navigation in progress. This prevents Chrome from sending | 177 // if there's a navigation in progress. This prevents Chrome from sending |
179 // a spurious oninputend when the user accepts a match in the omnibox. | 178 // a spurious oninputend when the user accepts a match in the omnibox. |
180 if (web_contents_->GetController().GetPendingEntry() == NULL) | 179 if (web_contents_->GetController().GetPendingEntry() == nullptr) |
181 ipc_router_.SetInputInProgress(IsInputInProgress()); | 180 ipc_router_.SetInputInProgress(IsInputInProgress()); |
182 } | 181 } |
183 | 182 |
184 void SearchTabHelper::NavigationEntryUpdated() { | 183 void SearchTabHelper::NavigationEntryUpdated() { |
185 if (!is_search_enabled_) | 184 if (!is_search_enabled_) |
186 return; | 185 return; |
187 | 186 |
188 UpdateMode(false); | 187 UpdateMode(false); |
189 } | 188 } |
190 | 189 |
191 void SearchTabHelper::SetSuggestionToPrefetch( | 190 void SearchTabHelper::SetSuggestionToPrefetch( |
192 const InstantSuggestion& suggestion) { | 191 const InstantSuggestion& suggestion) { |
193 ipc_router_.SetSuggestionToPrefetch(suggestion); | 192 ipc_router_.SetSuggestionToPrefetch(suggestion); |
194 } | 193 } |
195 | 194 |
196 void SearchTabHelper::Submit(const base::string16& text, | 195 void SearchTabHelper::Submit(const base::string16& text, |
197 const EmbeddedSearchRequestParams& params) { | 196 const EmbeddedSearchRequestParams& params) { |
198 ipc_router_.Submit(text, params); | 197 ipc_router_.Submit(text, params); |
199 } | 198 } |
200 | 199 |
| 200 void SearchTabHelper::OnTabAttachedToWindow(BrowserWindow* window) { |
| 201 omnibox_view_ = window->GetLocationBar()->GetOmniboxView(); |
| 202 } |
| 203 |
201 void SearchTabHelper::OnTabActivated() { | 204 void SearchTabHelper::OnTabActivated() { |
202 ipc_router_.OnTabActivated(); | 205 ipc_router_.OnTabActivated(); |
203 | 206 |
204 if (search::IsInstantNTP(web_contents_)) { | 207 if (search::IsInstantNTP(web_contents_)) { |
205 if (instant_service_) | 208 if (instant_service_) |
206 instant_service_->OnNewTabPageOpened(); | 209 instant_service_->OnNewTabPageOpened(); |
207 | 210 |
208 // Force creation of NTPUserDataLogger, if we loaded an NTP. The | 211 // Force creation of NTPUserDataLogger, if we loaded an NTP. The |
209 // NTPUserDataLogger tries to detect whether the NTP is being created at | 212 // NTPUserDataLogger tries to detect whether the NTP is being created at |
210 // startup or from the user opening a new tab, and if we wait until later, | 213 // startup or from the user opening a new tab, and if we wait until later, |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
334 } | 337 } |
335 | 338 |
336 void SearchTabHelper::MostVisitedItemsChanged( | 339 void SearchTabHelper::MostVisitedItemsChanged( |
337 const std::vector<InstantMostVisitedItem>& items) { | 340 const std::vector<InstantMostVisitedItem>& items) { |
338 ipc_router_.SendMostVisitedItems(items); | 341 ipc_router_.SendMostVisitedItems(items); |
339 } | 342 } |
340 | 343 |
341 void SearchTabHelper::FocusOmnibox(OmniboxFocusState state) { | 344 void SearchTabHelper::FocusOmnibox(OmniboxFocusState state) { |
342 // TODO(kmadhusu): Move platform specific code from here and get rid of #ifdef. | 345 // TODO(kmadhusu): Move platform specific code from here and get rid of #ifdef. |
343 #if !defined(OS_ANDROID) | 346 #if !defined(OS_ANDROID) |
344 OmniboxView* omnibox = GetOmniboxView(); | 347 if (!omnibox_view_) |
345 if (!omnibox) | |
346 return; | 348 return; |
347 | 349 |
348 // Do not add a default case in the switch block for the following reasons: | 350 // Do not add a default case in the switch block for the following reasons: |
349 // (1) Explicitly handle the new states. If new states are added in the | 351 // (1) Explicitly handle the new states. If new states are added in the |
350 // OmniboxFocusState, the compiler will warn the developer to handle the new | 352 // OmniboxFocusState, the compiler will warn the developer to handle the new |
351 // states. | 353 // states. |
352 // (2) An attacker may control the renderer and sends the browser process a | 354 // (2) An attacker may control the renderer and sends the browser process a |
353 // malformed IPC. This function responds to the invalid |state| values by | 355 // malformed IPC. This function responds to the invalid |state| values by |
354 // doing nothing instead of crashing the browser process (intentional no-op). | 356 // doing nothing instead of crashing the browser process (intentional no-op). |
355 switch (state) { | 357 switch (state) { |
356 case OMNIBOX_FOCUS_VISIBLE: | 358 case OMNIBOX_FOCUS_VISIBLE: |
357 omnibox->SetFocus(); | 359 omnibox_view_->SetFocus(); |
358 omnibox->model()->SetCaretVisibility(true); | 360 omnibox_view_->model()->SetCaretVisibility(true); |
359 break; | 361 break; |
360 case OMNIBOX_FOCUS_INVISIBLE: | 362 case OMNIBOX_FOCUS_INVISIBLE: |
361 omnibox->SetFocus(); | 363 omnibox_view_->SetFocus(); |
362 omnibox->model()->SetCaretVisibility(false); | 364 omnibox_view_->model()->SetCaretVisibility(false); |
363 // If the user clicked on the fakebox, any text already in the omnibox | 365 // If the user clicked on the fakebox, any text already in the omnibox |
364 // should get cleared when they start typing. Selecting all the existing | 366 // should get cleared when they start typing. Selecting all the existing |
365 // text is a convenient way to accomplish this. It also gives a slight | 367 // text is a convenient way to accomplish this. It also gives a slight |
366 // visual cue to users who really understand selection state about what | 368 // visual cue to users who really understand selection state about what |
367 // will happen if they start typing. | 369 // will happen if they start typing. |
368 omnibox->SelectAll(false); | 370 omnibox_view_->SelectAll(false); |
369 omnibox->ShowImeIfNeeded(); | 371 omnibox_view_->ShowImeIfNeeded(); |
370 break; | 372 break; |
371 case OMNIBOX_FOCUS_NONE: | 373 case OMNIBOX_FOCUS_NONE: |
372 // Remove focus only if the popup is closed. This will prevent someone | 374 // Remove focus only if the popup is closed. This will prevent someone |
373 // from changing the omnibox value and closing the popup without user | 375 // from changing the omnibox value and closing the popup without user |
374 // interaction. | 376 // interaction. |
375 if (!omnibox->model()->popup_model()->IsOpen()) | 377 if (!omnibox_view_->model()->popup_model()->IsOpen()) |
376 web_contents()->Focus(); | 378 web_contents()->Focus(); |
377 break; | 379 break; |
378 } | 380 } |
379 #endif | 381 #endif |
380 } | 382 } |
381 | 383 |
382 void SearchTabHelper::OnDeleteMostVisitedItem(const GURL& url) { | 384 void SearchTabHelper::OnDeleteMostVisitedItem(const GURL& url) { |
383 DCHECK(!url.is_empty()); | 385 DCHECK(!url.is_empty()); |
384 if (instant_service_) | 386 if (instant_service_) |
385 instant_service_->DeleteMostVisitedItem(url); | 387 instant_service_->DeleteMostVisitedItem(url); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
421 // TODO(kmadhusu): Move platform specific code from here and get rid of #ifdef. | 423 // TODO(kmadhusu): Move platform specific code from here and get rid of #ifdef. |
422 #if !defined(OS_ANDROID) | 424 #if !defined(OS_ANDROID) |
423 NTPUserDataLogger::GetOrCreateFromWebContents( | 425 NTPUserDataLogger::GetOrCreateFromWebContents( |
424 web_contents())->LogMostVisitedNavigation(position, tile_source); | 426 web_contents())->LogMostVisitedNavigation(position, tile_source); |
425 #endif | 427 #endif |
426 } | 428 } |
427 | 429 |
428 void SearchTabHelper::PasteIntoOmnibox(const base::string16& text) { | 430 void SearchTabHelper::PasteIntoOmnibox(const base::string16& text) { |
429 // TODO(kmadhusu): Move platform specific code from here and get rid of #ifdef. | 431 // TODO(kmadhusu): Move platform specific code from here and get rid of #ifdef. |
430 #if !defined(OS_ANDROID) | 432 #if !defined(OS_ANDROID) |
431 OmniboxView* omnibox = GetOmniboxView(); | 433 if (!omnibox_view_) |
432 if (!omnibox) | |
433 return; | 434 return; |
434 // The first case is for right click to paste, where the text is retrieved | 435 // The first case is for right click to paste, where the text is retrieved |
435 // from the clipboard already sanitized. The second case is needed to handle | 436 // from the clipboard already sanitized. The second case is needed to handle |
436 // drag-and-drop value and it has to be sanitazed before setting it into the | 437 // drag-and-drop value and it has to be sanitazed before setting it into the |
437 // omnibox. | 438 // omnibox. |
438 base::string16 text_to_paste = | 439 base::string16 text_to_paste = |
439 text.empty() ? GetClipboardText() : omnibox->SanitizeTextForPaste(text); | 440 text.empty() ? GetClipboardText() |
| 441 : omnibox_view_->SanitizeTextForPaste(text); |
440 | 442 |
441 if (text_to_paste.empty()) | 443 if (text_to_paste.empty()) |
442 return; | 444 return; |
443 | 445 |
444 if (!omnibox->model()->has_focus()) | 446 if (!omnibox_view_->model()->has_focus()) |
445 omnibox->SetFocus(); | 447 omnibox_view_->SetFocus(); |
446 | 448 |
447 omnibox->OnBeforePossibleChange(); | 449 omnibox_view_->OnBeforePossibleChange(); |
448 omnibox->model()->OnPaste(); | 450 omnibox_view_->model()->OnPaste(); |
449 omnibox->SetUserText(text_to_paste); | 451 omnibox_view_->SetUserText(text_to_paste); |
450 omnibox->OnAfterPossibleChange(true); | 452 omnibox_view_->OnAfterPossibleChange(true); |
451 #endif | 453 #endif |
452 } | 454 } |
453 | 455 |
454 void SearchTabHelper::OnChromeIdentityCheck(const base::string16& identity) { | 456 void SearchTabHelper::OnChromeIdentityCheck(const base::string16& identity) { |
455 SigninManagerBase* manager = SigninManagerFactory::GetForProfile(profile()); | 457 SigninManagerBase* manager = SigninManagerFactory::GetForProfile(profile()); |
456 if (manager) { | 458 if (manager) { |
457 ipc_router_.SendChromeIdentityCheckResult( | 459 ipc_router_.SendChromeIdentityCheckResult( |
458 identity, | 460 identity, |
459 gaia::AreEmailsSame(base::UTF16ToUTF8(identity), | 461 gaia::AreEmailsSame(base::UTF16ToUTF8(identity), |
460 manager->GetAuthenticatedAccountInfo().email)); | 462 manager->GetAuthenticatedAccountInfo().email)); |
(...skipping 19 matching lines...) Expand all Loading... |
480 void SearchTabHelper::UpdateMode(bool update_origin) { | 482 void SearchTabHelper::UpdateMode(bool update_origin) { |
481 SearchMode::Type type = SearchMode::MODE_DEFAULT; | 483 SearchMode::Type type = SearchMode::MODE_DEFAULT; |
482 SearchMode::Origin origin = SearchMode::ORIGIN_DEFAULT; | 484 SearchMode::Origin origin = SearchMode::ORIGIN_DEFAULT; |
483 if (IsNTP(web_contents_)) { | 485 if (IsNTP(web_contents_)) { |
484 type = SearchMode::MODE_NTP; | 486 type = SearchMode::MODE_NTP; |
485 origin = SearchMode::ORIGIN_NTP; | 487 origin = SearchMode::ORIGIN_NTP; |
486 } | 488 } |
487 if (!update_origin) | 489 if (!update_origin) |
488 origin = model_.mode().origin; | 490 origin = model_.mode().origin; |
489 | 491 |
490 OmniboxView* omnibox = GetOmniboxView(); | 492 if (omnibox_view_ && omnibox_view_->model()->user_input_in_progress()) |
491 if (omnibox && omnibox->model()->user_input_in_progress()) | |
492 type = SearchMode::MODE_SEARCH_SUGGESTIONS; | 493 type = SearchMode::MODE_SEARCH_SUGGESTIONS; |
493 | 494 |
494 SearchMode old_mode(model_.mode()); | 495 SearchMode old_mode(model_.mode()); |
495 model_.SetMode(SearchMode(type, origin)); | 496 model_.SetMode(SearchMode(type, origin)); |
496 if (old_mode.is_ntp() != model_.mode().is_ntp()) { | 497 if (old_mode.is_ntp() != model_.mode().is_ntp()) { |
497 ipc_router_.SetInputInProgress(IsInputInProgress()); | 498 ipc_router_.SetInputInProgress(IsInputInProgress()); |
498 } | 499 } |
499 } | 500 } |
500 | 501 |
501 void SearchTabHelper::DetermineIfPageSupportsInstant() { | 502 void SearchTabHelper::DetermineIfPageSupportsInstant() { |
502 if (!InInstantProcess(profile(), web_contents_)) { | 503 if (!InInstantProcess(profile(), web_contents_)) { |
503 // The page is not in the Instant process. This page does not support | 504 // The page is not in the Instant process. This page does not support |
504 // instant. If we send an IPC message to a page that is not in the Instant | 505 // instant. If we send an IPC message to a page that is not in the Instant |
505 // process, it will never receive it and will never respond. Therefore, | 506 // process, it will never receive it and will never respond. Therefore, |
506 // return immediately. | 507 // return immediately. |
507 InstantSupportChanged(false); | 508 InstantSupportChanged(false); |
508 } else if (IsLocal(web_contents_)) { | 509 } else if (IsLocal(web_contents_)) { |
509 // Local pages always support Instant. | 510 // Local pages always support Instant. |
510 InstantSupportChanged(true); | 511 InstantSupportChanged(true); |
511 } else { | 512 } else { |
512 ipc_router_.DetermineIfPageSupportsInstant(); | 513 ipc_router_.DetermineIfPageSupportsInstant(); |
513 } | 514 } |
514 } | 515 } |
515 | 516 |
516 Profile* SearchTabHelper::profile() const { | 517 Profile* SearchTabHelper::profile() const { |
517 return Profile::FromBrowserContext(web_contents_->GetBrowserContext()); | 518 return Profile::FromBrowserContext(web_contents_->GetBrowserContext()); |
518 } | 519 } |
519 | 520 |
520 bool SearchTabHelper::IsInputInProgress() const { | 521 bool SearchTabHelper::IsInputInProgress() const { |
521 OmniboxView* omnibox = GetOmniboxView(); | 522 return !model_.mode().is_ntp() && omnibox_view_ && |
522 return !model_.mode().is_ntp() && omnibox && | 523 omnibox_view_->model()->focus_state() == OMNIBOX_FOCUS_VISIBLE; |
523 omnibox->model()->focus_state() == OMNIBOX_FOCUS_VISIBLE; | |
524 } | 524 } |
525 | |
526 OmniboxView* SearchTabHelper::GetOmniboxView() const { | |
527 return delegate_ ? delegate_->GetOmniboxView() : NULL; | |
528 } | |
OLD | NEW |