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 return true; |
| 124 } |
| 125 |
98 bool BrowserInstantController::OpenInstant(WindowOpenDisposition disposition) { | 126 bool BrowserInstantController::OpenInstant(WindowOpenDisposition disposition) { |
99 // Unsupported dispositions. | 127 // Unsupported dispositions. |
100 if (disposition == NEW_BACKGROUND_TAB || disposition == NEW_WINDOW) | 128 if (disposition == NEW_BACKGROUND_TAB || disposition == NEW_WINDOW) |
101 return false; | 129 return false; |
102 | 130 |
103 // The omnibox currently doesn't use other dispositions, so we don't attempt | 131 // 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 | 132 // to handle them. If you hit this DCHECK file a bug and I'll (sky) add |
105 // support for the new disposition. | 133 // support for the new disposition. |
106 DCHECK(disposition == CURRENT_TAB || | 134 DCHECK(disposition == CURRENT_TAB || |
107 disposition == NEW_FOREGROUND_TAB) << disposition; | 135 disposition == NEW_FOREGROUND_TAB) << disposition; |
108 | 136 |
109 return instant_.CommitIfPossible(disposition == CURRENT_TAB ? | 137 return instant_.CommitIfPossible(disposition == CURRENT_TAB ? |
110 INSTANT_COMMIT_PRESSED_ENTER : INSTANT_COMMIT_PRESSED_ALT_ENTER); | 138 INSTANT_COMMIT_PRESSED_ENTER : INSTANT_COMMIT_PRESSED_ALT_ENTER); |
111 } | 139 } |
112 | 140 |
113 void BrowserInstantController::CommitInstant(content::WebContents* preview, | 141 Profile* BrowserInstantController::profile() const { |
114 bool in_new_tab) { | 142 return browser_->profile(); |
| 143 } |
| 144 |
| 145 void BrowserInstantController::CommitInstant( |
| 146 scoped_ptr<content::WebContents> preview, |
| 147 bool in_new_tab) { |
115 if (in_new_tab) { | 148 if (in_new_tab) { |
116 // TabStripModel takes ownership of |preview|. | 149 // TabStripModel takes ownership of |preview|. |
117 browser_->tab_strip_model()->AddWebContents(preview, -1, | 150 browser_->tab_strip_model()->AddWebContents(preview.release(), -1, |
118 instant_.last_transition_type(), TabStripModel::ADD_ACTIVE); | 151 instant_.last_transition_type(), TabStripModel::ADD_ACTIVE); |
119 } else { | 152 } else { |
120 int index = browser_->tab_strip_model()->active_index(); | 153 int index = browser_->tab_strip_model()->active_index(); |
121 DCHECK_NE(TabStripModel::kNoTab, index); | 154 const GURL& url = preview->GetURL(); |
122 content::WebContents* active_tab = | 155 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()); | 156 DCHECK(browser_->profile()->GetExtensionService()); |
131 if (browser_->profile()->GetExtensionService()->IsInstalledApp(url)) { | 157 if (browser_->profile()->GetExtensionService()->IsInstalledApp(url)) { |
132 AppLauncherHandler::RecordAppLaunchType( | 158 AppLauncherHandler::RecordAppLaunchType( |
133 extension_misc::APP_LAUNCH_OMNIBOX_INSTANT); | 159 extension_misc::APP_LAUNCH_OMNIBOX_INSTANT); |
134 } | 160 } |
135 } | 161 } |
136 } | 162 } |
137 | 163 |
| 164 void BrowserInstantController::ReplaceWebContentsAt( |
| 165 int index, |
| 166 scoped_ptr<content::WebContents> new_contents) { |
| 167 DCHECK_NE(TabStripModel::kNoTab, index); |
| 168 content::WebContents* old_contents = |
| 169 browser_->tab_strip_model()->GetWebContentsAt(index); |
| 170 // TabStripModel takes ownership of |new_contents|. |
| 171 browser_->tab_strip_model()->ReplaceWebContentsAt( |
| 172 index, new_contents.release()); |
| 173 // TODO(samarth): use scoped_ptr instead of comments to document ownership |
| 174 // transfer. |
| 175 // InstantUnloadHandler takes ownership of |old_contents|. |
| 176 instant_unload_handler_.RunUnloadListenersOrDestroy(old_contents, index); |
| 177 } |
| 178 |
138 void BrowserInstantController::SetInstantSuggestion( | 179 void BrowserInstantController::SetInstantSuggestion( |
139 const InstantSuggestion& suggestion) { | 180 const InstantSuggestion& suggestion) { |
140 browser_->window()->GetLocationBar()->SetInstantSuggestion(suggestion); | 181 browser_->window()->GetLocationBar()->SetInstantSuggestion(suggestion); |
141 } | 182 } |
142 | 183 |
143 gfx::Rect BrowserInstantController::GetInstantBounds() { | 184 gfx::Rect BrowserInstantController::GetInstantBounds() { |
144 return browser_->window()->GetInstantBounds(); | 185 return browser_->window()->GetInstantBounds(); |
145 } | 186 } |
146 | 187 |
147 void BrowserInstantController::InstantPreviewFocused() { | 188 void BrowserInstantController::InstantPreviewFocused() { |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
313 // part of the image overlay should draw, 'cos the origin is top-left. | 354 // part of the image overlay should draw, 'cos the origin is top-left. |
314 if (!browser_->search_model()->mode().is_ntp() || | 355 if (!browser_->search_model()->mode().is_ntp() || |
315 theme_info_.theme_id.empty() || | 356 theme_info_.theme_id.empty() || |
316 theme_info_.image_vertical_alignment == THEME_BKGRND_IMAGE_ALIGN_TOP) { | 357 theme_info_.image_vertical_alignment == THEME_BKGRND_IMAGE_ALIGN_TOP) { |
317 return; | 358 return; |
318 } | 359 } |
319 instant_.ThemeAreaHeightChanged(theme_area_height_); | 360 instant_.ThemeAreaHeightChanged(theme_area_height_); |
320 } | 361 } |
321 | 362 |
322 } // namespace chrome | 363 } // namespace chrome |
OLD | NEW |