Chromium Code Reviews| 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/browser_instant_controller.h" | 5 #include "chrome/browser/ui/browser_instant_controller.h" |
| 6 | 6 |
| 7 #include "chrome/browser/extensions/extension_service.h" | 7 #include "chrome/browser/extensions/extension_service.h" |
| 8 #include "chrome/browser/prefs/pref_service.h" | 8 #include "chrome/browser/prefs/pref_service.h" |
| 9 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/browser/themes/theme_service.h" | 10 #include "chrome/browser/themes/theme_service.h" |
| 11 #include "chrome/browser/themes/theme_service_factory.h" | 11 #include "chrome/browser/themes/theme_service_factory.h" |
| 12 #include "chrome/browser/ui/browser.h" | 12 #include "chrome/browser/ui/browser.h" |
| 13 #include "chrome/browser/ui/browser_window.h" | 13 #include "chrome/browser/ui/browser_window.h" |
| 14 #include "chrome/browser/ui/omnibox/location_bar.h" | 14 #include "chrome/browser/ui/omnibox/location_bar.h" |
| 15 #include "chrome/browser/ui/omnibox/omnibox_view.h" | 15 #include "chrome/browser/ui/omnibox/omnibox_view.h" |
| 16 #include "chrome/browser/ui/search/search.h" | 16 #include "chrome/browser/ui/search/search.h" |
| 17 #include "chrome/browser/ui/search/search_tab_helper.h" | 17 #include "chrome/browser/ui/search/search_tab_helper.h" |
| 18 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 18 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 19 #include "chrome/browser/ui/webui/ntp/app_launcher_handler.h" | 19 #include "chrome/browser/ui/webui/ntp/app_launcher_handler.h" |
| 20 #include "chrome/common/chrome_notification_types.h" | 20 #include "chrome/common/chrome_notification_types.h" |
| 21 #include "chrome/common/pref_names.h" | 21 #include "chrome/common/pref_names.h" |
| 22 #include "chrome/common/url_constants.h" | |
| 22 #include "content/public/browser/notification_service.h" | 23 #include "content/public/browser/notification_service.h" |
| 23 #include "grit/theme_resources.h" | 24 #include "grit/theme_resources.h" |
| 24 #include "ui/gfx/color_utils.h" | 25 #include "ui/gfx/color_utils.h" |
| 25 #include "ui/gfx/sys_color_change_listener.h" | 26 #include "ui/gfx/sys_color_change_listener.h" |
| 26 | 27 |
| 27 namespace { | 28 namespace { |
| 28 const char* GetInstantPrefName(Profile* profile) { | 29 const char* GetInstantPrefName(Profile* profile) { |
| 29 return chrome::search::IsInstantExtendedAPIEnabled(profile) ? | 30 return chrome::search::IsInstantExtendedAPIEnabled(profile) ? |
| 30 prefs::kInstantExtendedEnabled : prefs::kInstantEnabled; | 31 prefs::kInstantExtendedEnabled : prefs::kInstantEnabled; |
| 31 } | 32 } |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 88 case search::INSTANT_FORCE_OFF: | 89 case search::INSTANT_FORCE_OFF: |
| 89 instant_extended_value = false; | 90 instant_extended_value = false; |
| 90 break; | 91 break; |
| 91 } | 92 } |
| 92 | 93 |
| 93 prefs->RegisterBooleanPref(prefs::kInstantExtendedEnabled, | 94 prefs->RegisterBooleanPref(prefs::kInstantExtendedEnabled, |
| 94 instant_extended_value, | 95 instant_extended_value, |
| 95 PrefServiceSyncable::SYNCABLE_PREF); | 96 PrefServiceSyncable::SYNCABLE_PREF); |
| 96 } | 97 } |
| 97 | 98 |
| 99 bool BrowserInstantController::MaybeSwapInInstantNTPContents( | |
| 100 const GURL& url, | |
| 101 content::WebContents* source_contents, | |
| 102 content::WebContents** target_contents) { | |
| 103 if (url != GURL(chrome::kChromeUINewTabURL)) | |
| 104 return false; | |
| 105 | |
| 106 scoped_ptr<content::WebContents> instant_ntp = instant_.ReleaseNTPContents(); | |
| 107 if (!instant_ntp) | |
| 108 return false; | |
| 109 | |
| 110 *target_contents = instant_ntp.get(); | |
| 111 instant_ntp->GetController().PruneAllButActive(); | |
| 112 if (source_contents) { | |
| 113 instant_ntp->GetController().CopyStateFromAndPrune( | |
| 114 &source_contents->GetController()); | |
| 115 ReplaceWebContentsAt( | |
| 116 browser_->tab_strip_model()->GetIndexOfWebContents(source_contents), | |
| 117 instant_ntp.Pass()); | |
| 118 } else { | |
| 119 // If |source_contents| is NULL, then the caller is responsible for | |
| 120 // inserting instant_ntp into the tabstrip and will take ownership. | |
| 121 ignore_result(instant_ntp.release()); | |
| 122 } | |
| 123 // Focus the omnibox since the omnibox should have focus whenever the New Tab | |
| 124 // Page is opened. | |
| 125 // | |
| 126 // TODO(samarth): this doesn't handle all cases, e.g., if you arrive at the | |
| 127 // NTP using back/forward buttons. Doing this properly will require telling | |
| 128 // the content layer that navigations to this page should result in the | |
| 129 // omnibox being focused. | |
| 130 MessageLoop::current()->PostTask( | |
| 131 FROM_HERE, | |
| 132 base::Bind(&BrowserInstantController::FocusOmniboxForNTP, | |
|
sky
2013/02/05 21:40:13
Why the delay here? What if this is destroyed befo
samarth
2013/02/05 22:41:57
Yeah, you'right: this will probably break.
I need
| |
| 133 base::Unretained(this))); | |
| 134 return true; | |
| 135 } | |
| 136 | |
| 98 bool BrowserInstantController::OpenInstant(WindowOpenDisposition disposition) { | 137 bool BrowserInstantController::OpenInstant(WindowOpenDisposition disposition) { |
| 99 // Unsupported dispositions. | 138 // Unsupported dispositions. |
| 100 if (disposition == NEW_BACKGROUND_TAB || disposition == NEW_WINDOW) | 139 if (disposition == NEW_BACKGROUND_TAB || disposition == NEW_WINDOW) |
| 101 return false; | 140 return false; |
| 102 | 141 |
| 103 // The omnibox currently doesn't use other dispositions, so we don't attempt | 142 // The omnibox currently doesn't use other dispositions, so we don't attempt |
| 104 // to handle them. If you hit this DCHECK file a bug and I'll (sky) add | 143 // to handle them. If you hit this DCHECK file a bug and I'll (sky) add |
| 105 // support for the new disposition. | 144 // support for the new disposition. |
| 106 DCHECK(disposition == CURRENT_TAB || | 145 DCHECK(disposition == CURRENT_TAB || |
| 107 disposition == NEW_FOREGROUND_TAB) << disposition; | 146 disposition == NEW_FOREGROUND_TAB) << disposition; |
| 108 | 147 |
| 109 return instant_.CommitIfPossible(disposition == CURRENT_TAB ? | 148 return instant_.CommitIfPossible(disposition == CURRENT_TAB ? |
| 110 INSTANT_COMMIT_PRESSED_ENTER : INSTANT_COMMIT_PRESSED_ALT_ENTER); | 149 INSTANT_COMMIT_PRESSED_ENTER : INSTANT_COMMIT_PRESSED_ALT_ENTER); |
| 111 } | 150 } |
| 112 | 151 |
| 113 void BrowserInstantController::CommitInstant(content::WebContents* preview, | 152 void BrowserInstantController::CommitInstant( |
| 114 bool in_new_tab) { | 153 scoped_ptr<content::WebContents> preview, |
| 154 bool in_new_tab) { | |
| 115 if (in_new_tab) { | 155 if (in_new_tab) { |
| 116 // TabStripModel takes ownership of |preview|. | 156 // TabStripModel takes ownership of |preview|. |
| 117 browser_->tab_strip_model()->AddWebContents(preview, -1, | 157 browser_->tab_strip_model()->AddWebContents(preview.release(), -1, |
| 118 instant_.last_transition_type(), TabStripModel::ADD_ACTIVE); | 158 instant_.last_transition_type(), TabStripModel::ADD_ACTIVE); |
| 119 } else { | 159 } else { |
| 120 int index = browser_->tab_strip_model()->active_index(); | 160 int index = browser_->tab_strip_model()->active_index(); |
| 121 DCHECK_NE(TabStripModel::kNoTab, index); | 161 const GURL& url = preview->GetURL(); |
| 122 content::WebContents* active_tab = | 162 ReplaceWebContentsAt(index, preview.Pass()); |
| 123 browser_->tab_strip_model()->GetWebContentsAt(index); | |
| 124 // TabStripModel takes ownership of |preview|. | |
| 125 browser_->tab_strip_model()->ReplaceWebContentsAt(index, preview); | |
| 126 // InstantUnloadHandler takes ownership of |active_tab|. | |
| 127 instant_unload_handler_.RunUnloadListenersOrDestroy(active_tab, index); | |
| 128 | |
| 129 GURL url = preview->GetURL(); | |
| 130 DCHECK(browser_->profile()->GetExtensionService()); | 163 DCHECK(browser_->profile()->GetExtensionService()); |
| 131 if (browser_->profile()->GetExtensionService()->IsInstalledApp(url)) { | 164 if (browser_->profile()->GetExtensionService()->IsInstalledApp(url)) { |
| 132 AppLauncherHandler::RecordAppLaunchType( | 165 AppLauncherHandler::RecordAppLaunchType( |
| 133 extension_misc::APP_LAUNCH_OMNIBOX_INSTANT); | 166 extension_misc::APP_LAUNCH_OMNIBOX_INSTANT); |
| 134 } | 167 } |
| 135 } | 168 } |
| 136 } | 169 } |
| 137 | 170 |
| 171 void BrowserInstantController::ReplaceWebContentsAt( | |
| 172 int index, | |
| 173 scoped_ptr<content::WebContents> new_contents) { | |
| 174 DCHECK_NE(TabStripModel::kNoTab, index); | |
| 175 content::WebContents* old_contents = | |
| 176 browser_->tab_strip_model()->GetWebContentsAt(index); | |
| 177 // TabStripModel takes ownership of |new_contents|. | |
| 178 browser_->tab_strip_model()->ReplaceWebContentsAt( | |
| 179 index, new_contents.release()); | |
| 180 // TODO(samarth): use scoped_ptr instead of comments to document ownership | |
| 181 // transfer. | |
| 182 // InstantUnloadHandler takes ownership of |old_contents|. | |
| 183 instant_unload_handler_.RunUnloadListenersOrDestroy(old_contents, index); | |
| 184 } | |
| 185 | |
| 138 void BrowserInstantController::SetInstantSuggestion( | 186 void BrowserInstantController::SetInstantSuggestion( |
| 139 const InstantSuggestion& suggestion) { | 187 const InstantSuggestion& suggestion) { |
| 140 browser_->window()->GetLocationBar()->SetInstantSuggestion(suggestion); | 188 browser_->window()->GetLocationBar()->SetInstantSuggestion(suggestion); |
| 141 } | 189 } |
| 142 | 190 |
| 143 gfx::Rect BrowserInstantController::GetInstantBounds() { | 191 gfx::Rect BrowserInstantController::GetInstantBounds() { |
| 144 return browser_->window()->GetInstantBounds(); | 192 return browser_->window()->GetInstantBounds(); |
| 145 } | 193 } |
| 146 | 194 |
| 147 void BrowserInstantController::InstantPreviewFocused() { | 195 void BrowserInstantController::InstantPreviewFocused() { |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 312 // aligned; top-aligned images don't need theme area height to determine which | 360 // aligned; top-aligned images don't need theme area height to determine which |
| 313 // part of the image overlay should draw, 'cos the origin is top-left. | 361 // part of the image overlay should draw, 'cos the origin is top-left. |
| 314 if (!browser_->search_model()->mode().is_ntp() || | 362 if (!browser_->search_model()->mode().is_ntp() || |
| 315 theme_info_.theme_id.empty() || | 363 theme_info_.theme_id.empty() || |
| 316 theme_info_.image_vertical_alignment == THEME_BKGRND_IMAGE_ALIGN_TOP) { | 364 theme_info_.image_vertical_alignment == THEME_BKGRND_IMAGE_ALIGN_TOP) { |
| 317 return; | 365 return; |
| 318 } | 366 } |
| 319 instant_.ThemeAreaHeightChanged(theme_area_height_); | 367 instant_.ThemeAreaHeightChanged(theme_area_height_); |
| 320 } | 368 } |
| 321 | 369 |
| 370 void BrowserInstantController::FocusOmniboxForNTP() { | |
| 371 const content::WebContents* active_contents = | |
| 372 browser_->tab_strip_model()->GetActiveWebContents(); | |
| 373 if (active_contents && active_contents->GetURL() == | |
| 374 GURL(chrome::kChromeUINewTabURL)) { | |
| 375 browser_->window()->GetLocationBar()->GetLocationEntry()->SetFocus(); | |
| 376 } | |
| 377 } | |
| 378 | |
| 322 } // namespace chrome | 379 } // namespace chrome |
| OLD | NEW |