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