[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors. All rights reserved. |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/ui/browser_instant_controller.h" |
| 6 | |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 7 | #include "base/bind.h" |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 8 | #include "chrome/browser/extensions/extension_service.h" |
[email protected] | 26015ac | 2013-02-27 02:50:13 | [diff] [blame] | 9 | #include "chrome/browser/extensions/extension_web_ui.h" |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 10 | #include "chrome/browser/profiles/profile.h" |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 11 | #include "chrome/browser/search/instant_service.h" |
| 12 | #include "chrome/browser/search/instant_service_factory.h" |
[email protected] | a7b8e43d | 2013-03-18 18:52:43 | [diff] [blame] | 13 | #include "chrome/browser/search/search.h" |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 14 | #include "chrome/browser/ui/browser.h" |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 15 | #include "chrome/browser/ui/browser_window.h" |
| 16 | #include "chrome/browser/ui/omnibox/location_bar.h" |
[email protected] | 6cf51b6 | 2013-08-10 13:49:22 | [diff] [blame] | 17 | #include "chrome/browser/ui/omnibox/omnibox_popup_model.h" |
[email protected] | c18cb67 | 2012-12-05 04:42:12 | [diff] [blame] | 18 | #include "chrome/browser/ui/omnibox/omnibox_view.h" |
[email protected] | 4ff347e | 2013-07-22 19:39:00 | [diff] [blame] | 19 | #include "chrome/browser/ui/search/instant_ntp.h" |
[email protected] | 4066a695d | 2013-06-20 14:08:54 | [diff] [blame] | 20 | #include "chrome/browser/ui/search/search_model.h" |
[email protected] | 9d3d1170 | 2012-11-08 01:01:12 | [diff] [blame] | 21 | #include "chrome/browser/ui/search/search_tab_helper.h" |
[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame] | 22 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 23 | #include "chrome/browser/ui/webui/ntp/app_launcher_handler.h" |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 24 | #include "chrome/common/url_constants.h" |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 25 | #include "content/public/browser/render_process_host.h" |
[email protected] | 233f0f96 | 2013-02-27 21:14:19 | [diff] [blame] | 26 | #include "content/public/browser/user_metrics.h" |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 27 | #include "content/public/browser/web_contents.h" |
[email protected] | 6cf51b6 | 2013-08-10 13:49:22 | [diff] [blame] | 28 | #include "content/public/browser/web_contents_view.h" |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 29 | |
[email protected] | 233f0f96 | 2013-02-27 21:14:19 | [diff] [blame] | 30 | using content::UserMetricsAction; |
| 31 | |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 32 | //////////////////////////////////////////////////////////////////////////////// |
| 33 | // BrowserInstantController, public: |
| 34 | |
| 35 | BrowserInstantController::BrowserInstantController(Browser* browser) |
[email protected] | 8a23670 | 2012-09-28 13:30:57 | [diff] [blame] | 36 | : browser_(browser), |
[email protected] | a780c7b2 | 2013-08-02 18:36:59 | [diff] [blame] | 37 | instant_(this), |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 38 | instant_unload_handler_(browser) { |
[email protected] | 0b10c9ff | 2012-10-09 17:31:55 | [diff] [blame] | 39 | browser_->search_model()->AddObserver(this); |
[email protected] | 4ff347e | 2013-07-22 19:39:00 | [diff] [blame] | 40 | |
| 41 | InstantService* instant_service = |
| 42 | InstantServiceFactory::GetForProfile(profile()); |
| 43 | instant_service->OnBrowserInstantControllerCreated(); |
[email protected] | c8a118e | 2013-09-24 21:33:40 | [diff] [blame] | 44 | instant_service->AddObserver(this); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | BrowserInstantController::~BrowserInstantController() { |
[email protected] | 0b10c9ff | 2012-10-09 17:31:55 | [diff] [blame] | 48 | browser_->search_model()->RemoveObserver(this); |
[email protected] | 4ff347e | 2013-07-22 19:39:00 | [diff] [blame] | 49 | |
| 50 | InstantService* instant_service = |
| 51 | InstantServiceFactory::GetForProfile(profile()); |
[email protected] | c8a118e | 2013-09-24 21:33:40 | [diff] [blame] | 52 | instant_service->RemoveObserver(this); |
[email protected] | 4ff347e | 2013-07-22 19:39:00 | [diff] [blame] | 53 | instant_service->OnBrowserInstantControllerDestroyed(); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 54 | } |
| 55 | |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 56 | bool BrowserInstantController::MaybeSwapInInstantNTPContents( |
| 57 | const GURL& url, |
| 58 | content::WebContents* source_contents, |
| 59 | content::WebContents** target_contents) { |
| 60 | if (url != GURL(chrome::kChromeUINewTabURL)) |
| 61 | return false; |
| 62 | |
[email protected] | 26015ac | 2013-02-27 02:50:13 | [diff] [blame] | 63 | GURL extension_url(url); |
| 64 | if (ExtensionWebUI::HandleChromeURLOverride(&extension_url, profile())) { |
| 65 | // If there is an extension overriding the NTP do not use the Instant NTP. |
| 66 | return false; |
| 67 | } |
| 68 | |
[email protected] | 4ff347e | 2013-07-22 19:39:00 | [diff] [blame] | 69 | InstantService* instant_service = |
| 70 | InstantServiceFactory::GetForProfile(profile()); |
| 71 | scoped_ptr<content::WebContents> instant_ntp = |
| 72 | instant_service->ReleaseNTPContents(); |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 73 | if (!instant_ntp) |
| 74 | return false; |
| 75 | |
| 76 | *target_contents = instant_ntp.get(); |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 77 | if (source_contents) { |
[email protected] | 474f851 | 2013-05-31 22:31:16 | [diff] [blame] | 78 | // If the Instant NTP hasn't yet committed an entry, we can't call |
| 79 | // CopyStateFromAndPrune. Instead, load the Local NTP URL directly in the |
| 80 | // source contents. |
| 81 | // TODO(sreeram): Always using the local URL is wrong in the case of the |
| 82 | // first tab in a window where we might want to use the remote URL. Fix. |
| 83 | if (!instant_ntp->GetController().CanPruneAllButVisible()) { |
| 84 | source_contents->GetController().LoadURL(chrome::GetLocalInstantURL( |
| 85 | profile()), content::Referrer(), content::PAGE_TRANSITION_GENERATED, |
| 86 | std::string()); |
| 87 | *target_contents = source_contents; |
| 88 | } else { |
| 89 | instant_ntp->GetController().CopyStateFromAndPrune( |
| 90 | &source_contents->GetController()); |
| 91 | ReplaceWebContentsAt( |
| 92 | browser_->tab_strip_model()->GetIndexOfWebContents(source_contents), |
| 93 | instant_ntp.Pass()); |
| 94 | } |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 95 | } else { |
[email protected] | 474f851 | 2013-05-31 22:31:16 | [diff] [blame] | 96 | // If the Instant NTP hasn't yet committed an entry, we can't call |
| 97 | // PruneAllButVisible. In that case, there shouldn't be any entries to |
| 98 | // prune anyway. |
| 99 | if (instant_ntp->GetController().CanPruneAllButVisible()) |
| 100 | instant_ntp->GetController().PruneAllButVisible(); |
| 101 | else |
| 102 | CHECK(!instant_ntp->GetController().GetLastCommittedEntry()); |
| 103 | |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 104 | // If |source_contents| is NULL, then the caller is responsible for |
| 105 | // inserting instant_ntp into the tabstrip and will take ownership. |
| 106 | ignore_result(instant_ntp.release()); |
| 107 | } |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 108 | return true; |
| 109 | } |
| 110 | |
[email protected] | 413558cb | 2013-06-10 16:44:45 | [diff] [blame] | 111 | bool BrowserInstantController::OpenInstant(WindowOpenDisposition disposition, |
| 112 | const GURL& url) { |
[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame] | 113 | // Unsupported dispositions. |
[email protected] | 413558cb | 2013-06-10 16:44:45 | [diff] [blame] | 114 | if (disposition == NEW_BACKGROUND_TAB || disposition == NEW_WINDOW || |
| 115 | disposition == NEW_FOREGROUND_TAB) |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 116 | return false; |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 117 | |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 118 | // The omnibox currently doesn't use other dispositions, so we don't attempt |
[email protected] | c72226c8 | 2012-10-01 21:02:32 | [diff] [blame] | 119 | // to handle them. If you hit this DCHECK file a bug and I'll (sky) add |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 120 | // support for the new disposition. |
[email protected] | 413558cb | 2013-06-10 16:44:45 | [diff] [blame] | 121 | DCHECK(disposition == CURRENT_TAB) << disposition; |
[email protected] | c72226c8 | 2012-10-01 21:02:32 | [diff] [blame] | 122 | |
[email protected] | 413558cb | 2013-06-10 16:44:45 | [diff] [blame] | 123 | // If we will not be replacing search terms from this URL, don't send to |
| 124 | // InstantController. |
| 125 | const string16& search_terms = |
| 126 | chrome::GetSearchTermsFromURL(browser_->profile(), url); |
| 127 | if (search_terms.empty()) |
| 128 | return false; |
| 129 | |
| 130 | return instant_.SubmitQuery(search_terms); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 131 | } |
| 132 | |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 133 | Profile* BrowserInstantController::profile() const { |
| 134 | return browser_->profile(); |
| 135 | } |
| 136 | |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 137 | void BrowserInstantController::ReplaceWebContentsAt( |
| 138 | int index, |
| 139 | scoped_ptr<content::WebContents> new_contents) { |
| 140 | DCHECK_NE(TabStripModel::kNoTab, index); |
[email protected] | d572bfd | 2013-02-14 06:14:20 | [diff] [blame] | 141 | scoped_ptr<content::WebContents> old_contents(browser_->tab_strip_model()-> |
| 142 | ReplaceWebContentsAt(index, new_contents.release())); |
| 143 | instant_unload_handler_.RunUnloadListenersOrDestroy(old_contents.Pass(), |
| 144 | index); |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 145 | } |
| 146 | |
[email protected] | 6cf51b6 | 2013-08-10 13:49:22 | [diff] [blame] | 147 | void BrowserInstantController::FocusOmnibox(OmniboxFocusState state) { |
[email protected] | c18cb67 | 2012-12-05 04:42:12 | [diff] [blame] | 148 | OmniboxView* omnibox_view = browser_->window()->GetLocationBar()-> |
| 149 | GetLocationEntry(); |
[email protected] | 6cf51b6 | 2013-08-10 13:49:22 | [diff] [blame] | 150 | |
| 151 | // Do not add a default case in the switch block for the following reasons: |
| 152 | // (1) Explicitly handle the new states. If new states are added in the |
| 153 | // OmniboxFocusState, the compiler will warn the developer to handle the new |
| 154 | // states. |
| 155 | // (2) An attacker may control the renderer and sends the browser process a |
| 156 | // malformed IPC. This function responds to the invalid |state| values by |
| 157 | // doing nothing instead of crashing the browser process (intentional no-op). |
| 158 | switch (state) { |
| 159 | case OMNIBOX_FOCUS_VISIBLE: |
| 160 | omnibox_view->SetFocus(); |
| 161 | omnibox_view->model()->SetCaretVisibility(true); |
| 162 | break; |
| 163 | case OMNIBOX_FOCUS_INVISIBLE: |
| 164 | omnibox_view->SetFocus(); |
| 165 | omnibox_view->model()->SetCaretVisibility(false); |
| 166 | // If the user clicked on the fakebox, any text already in the omnibox |
| 167 | // should get cleared when they start typing. Selecting all the existing |
| 168 | // text is a convenient way to accomplish this. It also gives a slight |
| 169 | // visual cue to users who really understand selection state about what |
| 170 | // will happen if they start typing. |
| 171 | omnibox_view->SelectAll(false); |
| 172 | break; |
| 173 | case OMNIBOX_FOCUS_NONE: |
| 174 | // Remove focus only if the popup is closed. This will prevent someone |
| 175 | // from changing the omnibox value and closing the popup without user |
| 176 | // interaction. |
| 177 | if (!omnibox_view->model()->popup_model()->IsOpen()) { |
| 178 | content::WebContents* contents = GetActiveWebContents(); |
| 179 | if (contents) |
| 180 | contents->GetView()->Focus(); |
| 181 | } |
| 182 | break; |
[email protected] | 0d0b4a4 | 2013-06-14 00:46:26 | [diff] [blame] | 183 | } |
[email protected] | c18cb67 | 2012-12-05 04:42:12 | [diff] [blame] | 184 | } |
| 185 | |
[email protected] | cd533bf | 2012-12-04 19:14:59 | [diff] [blame] | 186 | content::WebContents* BrowserInstantController::GetActiveWebContents() const { |
| 187 | return browser_->tab_strip_model()->GetActiveWebContents(); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 188 | } |
| 189 | |
[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame] | 190 | void BrowserInstantController::ActiveTabChanged() { |
| 191 | instant_.ActiveTabChanged(); |
| 192 | } |
| 193 | |
[email protected] | 3d6a895 | 2012-12-14 03:18:07 | [diff] [blame] | 194 | void BrowserInstantController::TabDeactivated(content::WebContents* contents) { |
| 195 | instant_.TabDeactivated(contents); |
| 196 | } |
| 197 | |
[email protected] | 3c3acca | 2013-02-26 03:07:07 | [diff] [blame] | 198 | void BrowserInstantController::OpenURL( |
[email protected] | e3033eb | 2012-12-13 23:46:08 | [diff] [blame] | 199 | const GURL& url, |
[email protected] | 3c3acca | 2013-02-26 03:07:07 | [diff] [blame] | 200 | content::PageTransition transition, |
| 201 | WindowOpenDisposition disposition) { |
[email protected] | e3033eb | 2012-12-13 23:46:08 | [diff] [blame] | 202 | browser_->OpenURL(content::OpenURLParams(url, |
| 203 | content::Referrer(), |
[email protected] | 3c3acca | 2013-02-26 03:07:07 | [diff] [blame] | 204 | disposition, |
[email protected] | e3033eb | 2012-12-13 23:46:08 | [diff] [blame] | 205 | transition, |
| 206 | false)); |
| 207 | } |
| 208 | |
[email protected] | 6cf51b6 | 2013-08-10 13:49:22 | [diff] [blame] | 209 | void BrowserInstantController::PasteIntoOmnibox(const string16& text) { |
| 210 | OmniboxView* omnibox_view = browser_->window()->GetLocationBar()-> |
| 211 | GetLocationEntry(); |
| 212 | // The first case is for right click to paste, where the text is retrieved |
| 213 | // from the clipboard already sanitized. The second case is needed to handle |
| 214 | // drag-and-drop value and it has to be sanitazed before setting it into the |
| 215 | // omnibox. |
| 216 | string16 text_to_paste = text.empty() ? |
| 217 | omnibox_view->GetClipboardText() : |
| 218 | omnibox_view->SanitizeTextForPaste(text); |
| 219 | |
| 220 | if (!text_to_paste.empty()) { |
| 221 | if (!omnibox_view->model()->has_focus()) |
| 222 | omnibox_view->SetFocus(); |
| 223 | omnibox_view->OnBeforePossibleChange(); |
| 224 | omnibox_view->model()->on_paste(); |
| 225 | omnibox_view->SetUserText(text_to_paste); |
| 226 | omnibox_view->OnAfterPossibleChange(); |
| 227 | } |
| 228 | } |
| 229 | |
[email protected] | fcde79a | 2013-02-28 02:25:09 | [diff] [blame] | 230 | void BrowserInstantController::SetOmniboxBounds(const gfx::Rect& bounds) { |
| 231 | instant_.SetOmniboxBounds(bounds); |
[email protected] | ec4aad54 | 2012-12-14 01:11:04 | [diff] [blame] | 232 | } |
| 233 | |
[email protected] | 987fad78 | 2013-08-28 06:23:18 | [diff] [blame] | 234 | void BrowserInstantController::SetSuggestionToPrefetch( |
| 235 | const InstantSuggestion& suggestion) { |
| 236 | instant_.SetSuggestionToPrefetch(suggestion); |
| 237 | } |
| 238 | |
[email protected] | 3473ae0 | 2013-06-07 00:28:08 | [diff] [blame] | 239 | void BrowserInstantController::ToggleVoiceSearch() { |
| 240 | instant_.ToggleVoiceSearch(); |
| 241 | } |
| 242 | |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 243 | //////////////////////////////////////////////////////////////////////////////// |
[email protected] | 165fe42 | 2013-03-27 06:34:03 | [diff] [blame] | 244 | // BrowserInstantController, SearchModelObserver implementation: |
[email protected] | 0b10c9ff | 2012-10-09 17:31:55 | [diff] [blame] | 245 | |
[email protected] | 5ee671f | 2013-03-19 11:23:05 | [diff] [blame] | 246 | void BrowserInstantController::ModelChanged( |
[email protected] | 165fe42 | 2013-03-27 06:34:03 | [diff] [blame] | 247 | const SearchModel::State& old_state, |
| 248 | const SearchModel::State& new_state) { |
[email protected] | 4066a695d | 2013-06-20 14:08:54 | [diff] [blame] | 249 | if (old_state.mode != new_state.mode) { |
| 250 | const SearchMode& new_mode = new_state.mode; |
[email protected] | 5ee671f | 2013-03-19 11:23:05 | [diff] [blame] | 251 | |
[email protected] | a780c7b2 | 2013-08-02 18:36:59 | [diff] [blame] | 252 | // Record some actions corresponding to the mode change. Note that to get |
| 253 | // the full story, it's necessary to look at other UMA actions as well, |
| 254 | // such as tab switches. |
| 255 | if (new_mode.is_search_results()) |
| 256 | content::RecordAction(UserMetricsAction("InstantExtended.ShowSRP")); |
| 257 | else if (new_mode.is_ntp()) |
| 258 | content::RecordAction(UserMetricsAction("InstantExtended.ShowNTP")); |
[email protected] | 5ee671f | 2013-03-19 11:23:05 | [diff] [blame] | 259 | |
[email protected] | 4066a695d | 2013-06-20 14:08:54 | [diff] [blame] | 260 | instant_.SearchModeChanged(old_state.mode, new_mode); |
[email protected] | c19ba104 | 2013-03-11 17:17:13 | [diff] [blame] | 261 | } |
| 262 | |
[email protected] | 4066a695d | 2013-06-20 14:08:54 | [diff] [blame] | 263 | if (old_state.instant_support != new_state.instant_support) |
| 264 | instant_.InstantSupportChanged(new_state.instant_support); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 265 | } |
| 266 | |
[email protected] | c8a118e | 2013-09-24 21:33:40 | [diff] [blame] | 267 | //////////////////////////////////////////////////////////////////////////////// |
| 268 | // BrowserInstantController, InstantServiceObserver implementation: |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 269 | |
[email protected] | c8a118e | 2013-09-24 21:33:40 | [diff] [blame] | 270 | void BrowserInstantController::DefaultSearchProviderChanged() { |
| 271 | ReloadTabsInInstantProcess(); |
| 272 | } |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 273 | |
[email protected] | c8a118e | 2013-09-24 21:33:40 | [diff] [blame] | 274 | void BrowserInstantController::GoogleURLUpdated() { |
| 275 | ReloadTabsInInstantProcess(); |
| 276 | } |
| 277 | |
| 278 | void BrowserInstantController::ReloadTabsInInstantProcess() { |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 279 | InstantService* instant_service = |
[email protected] | c8a118e | 2013-09-24 21:33:40 | [diff] [blame] | 280 | InstantServiceFactory::GetForProfile(profile()); |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 281 | if (!instant_service) |
| 282 | return; |
| 283 | |
| 284 | TabStripModel* tab_model = browser_->tab_strip_model(); |
| 285 | int count = tab_model->count(); |
| 286 | for (int index = 0; index < count; ++index) { |
| 287 | content::WebContents* contents = tab_model->GetWebContentsAt(index); |
| 288 | if (!contents) |
| 289 | continue; |
| 290 | |
[email protected] | 2309e91 | 2013-10-01 01:33:30 | [diff] [blame^] | 291 | // Send new search URLs to the renderer. |
| 292 | content::RenderProcessHost* rph = contents->GetRenderProcessHost(); |
| 293 | instant_service->SendSearchURLsToRenderer(rph); |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 294 | |
| 295 | // Reload the contents to ensure that it gets assigned to a non-priviledged |
| 296 | // renderer. |
[email protected] | 2309e91 | 2013-10-01 01:33:30 | [diff] [blame^] | 297 | if (!instant_service->IsInstantProcess(rph->GetID())) |
| 298 | continue; |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 299 | contents->GetController().Reload(false); |
| 300 | } |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 301 | } |