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 } |
32 } | 33 } |
33 | 34 |
34 namespace chrome { | 35 namespace chrome { |
35 | 36 |
36 //////////////////////////////////////////////////////////////////////////////// | 37 //////////////////////////////////////////////////////////////////////////////// |
37 // BrowserInstantController, public: | 38 // BrowserInstantController, public: |
38 | 39 |
39 BrowserInstantController::BrowserInstantController(Browser* browser) | 40 BrowserInstantController::BrowserInstantController(Browser* browser) |
40 : browser_(browser), | 41 : browser_(browser), |
41 instant_(ALLOW_THIS_IN_INITIALIZER_LIST(this), | 42 instant_(ALLOW_THIS_IN_INITIALIZER_LIST(this), |
42 chrome::search::IsInstantExtendedAPIEnabled(browser->profile())), | 43 chrome::search::IsInstantExtendedAPIEnabled(browser->profile())), |
sreeram
2013/02/07 21:17:50
Replace browser_->profile() with just profile(), t
samarth
2013/02/07 22:11:55
Done.
| |
43 instant_unload_handler_(browser), | 44 instant_unload_handler_(browser), |
44 initialized_theme_info_(false), | 45 initialized_theme_info_(false), |
45 theme_area_height_(0) { | 46 theme_area_height_(0) { |
46 profile_pref_registrar_.Init(browser_->profile()->GetPrefs()); | 47 profile_pref_registrar_.Init(browser_->profile()->GetPrefs()); |
47 profile_pref_registrar_.Add( | 48 profile_pref_registrar_.Add( |
48 GetInstantPrefName(browser_->profile()), | 49 GetInstantPrefName(browser_->profile()), |
49 base::Bind(&BrowserInstantController::ResetInstant, | 50 base::Bind(&BrowserInstantController::ResetInstant, |
50 base::Unretained(this))); | 51 base::Unretained(this))); |
51 profile_pref_registrar_.Add( | 52 profile_pref_registrar_.Add( |
52 prefs::kSearchSuggestEnabled, | 53 prefs::kSearchSuggestEnabled, |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
91 case search::INSTANT_FORCE_OFF: | 92 case search::INSTANT_FORCE_OFF: |
92 instant_extended_value = false; | 93 instant_extended_value = false; |
93 break; | 94 break; |
94 } | 95 } |
95 | 96 |
96 prefs->RegisterBooleanPref(prefs::kInstantExtendedEnabled, | 97 prefs->RegisterBooleanPref(prefs::kInstantExtendedEnabled, |
97 instant_extended_value, | 98 instant_extended_value, |
98 PrefServiceSyncable::SYNCABLE_PREF); | 99 PrefServiceSyncable::SYNCABLE_PREF); |
99 } | 100 } |
100 | 101 |
102 bool BrowserInstantController::MaybeSwapInInstantNTPContents( | |
103 const GURL& url, | |
104 content::WebContents* source_contents, | |
105 content::WebContents** target_contents) { | |
106 if (url != GURL(chrome::kChromeUINewTabURL)) | |
107 return false; | |
108 | |
109 scoped_ptr<content::WebContents> instant_ntp = instant_.ReleaseNTPContents(); | |
110 if (!instant_ntp) | |
111 return false; | |
112 | |
113 *target_contents = instant_ntp.get(); | |
114 instant_ntp->GetController().PruneAllButActive(); | |
115 if (source_contents) { | |
116 instant_ntp->GetController().CopyStateFromAndPrune( | |
117 &source_contents->GetController()); | |
118 ReplaceWebContentsAt( | |
119 browser_->tab_strip_model()->GetIndexOfWebContents(source_contents), | |
120 instant_ntp.Pass()); | |
121 } else { | |
122 // If |source_contents| is NULL, then the caller is responsible for | |
123 // inserting instant_ntp into the tabstrip and will take ownership. | |
124 ignore_result(instant_ntp.release()); | |
125 } | |
126 return true; | |
127 } | |
128 | |
101 bool BrowserInstantController::OpenInstant(WindowOpenDisposition disposition) { | 129 bool BrowserInstantController::OpenInstant(WindowOpenDisposition disposition) { |
102 // Unsupported dispositions. | 130 // Unsupported dispositions. |
103 if (disposition == NEW_BACKGROUND_TAB || disposition == NEW_WINDOW) | 131 if (disposition == NEW_BACKGROUND_TAB || disposition == NEW_WINDOW) |
104 return false; | 132 return false; |
105 | 133 |
106 // The omnibox currently doesn't use other dispositions, so we don't attempt | 134 // The omnibox currently doesn't use other dispositions, so we don't attempt |
107 // to handle them. If you hit this DCHECK file a bug and I'll (sky) add | 135 // to handle them. If you hit this DCHECK file a bug and I'll (sky) add |
108 // support for the new disposition. | 136 // support for the new disposition. |
109 DCHECK(disposition == CURRENT_TAB || | 137 DCHECK(disposition == CURRENT_TAB || |
110 disposition == NEW_FOREGROUND_TAB) << disposition; | 138 disposition == NEW_FOREGROUND_TAB) << disposition; |
111 | 139 |
112 return instant_.CommitIfPossible(disposition == CURRENT_TAB ? | 140 return instant_.CommitIfPossible(disposition == CURRENT_TAB ? |
113 INSTANT_COMMIT_PRESSED_ENTER : INSTANT_COMMIT_PRESSED_ALT_ENTER); | 141 INSTANT_COMMIT_PRESSED_ENTER : INSTANT_COMMIT_PRESSED_ALT_ENTER); |
114 } | 142 } |
115 | 143 |
116 void BrowserInstantController::CommitInstant(content::WebContents* preview, | 144 Profile* BrowserInstantController::profile() const { |
117 bool in_new_tab) { | 145 return browser_->profile(); |
146 } | |
147 | |
148 void BrowserInstantController::CommitInstant( | |
149 scoped_ptr<content::WebContents> preview, | |
150 bool in_new_tab) { | |
118 if (in_new_tab) { | 151 if (in_new_tab) { |
119 // TabStripModel takes ownership of |preview|. | 152 // TabStripModel takes ownership of |preview|. |
120 browser_->tab_strip_model()->AddWebContents(preview, -1, | 153 browser_->tab_strip_model()->AddWebContents(preview.release(), -1, |
121 instant_.last_transition_type(), TabStripModel::ADD_ACTIVE); | 154 instant_.last_transition_type(), TabStripModel::ADD_ACTIVE); |
122 } else { | 155 } else { |
123 int index = browser_->tab_strip_model()->active_index(); | 156 int index = browser_->tab_strip_model()->active_index(); |
124 DCHECK_NE(TabStripModel::kNoTab, index); | 157 const GURL& url = preview->GetURL(); |
sreeram
2013/02/07 21:17:50
GURL url = preview->GetURL();
Because, given prev
samarth
2013/02/07 22:11:55
Done.
| |
125 content::WebContents* active_tab = | 158 ReplaceWebContentsAt(index, preview.Pass()); |
sreeram
2013/02/07 21:17:50
Nit: ReplaceWebContentsAt(browser_->tab_strip_mode
samarth
2013/02/07 22:11:55
Done.
| |
126 browser_->tab_strip_model()->GetWebContentsAt(index); | |
127 // TabStripModel takes ownership of |preview|. | |
128 browser_->tab_strip_model()->ReplaceWebContentsAt(index, preview); | |
129 // InstantUnloadHandler takes ownership of |active_tab|. | |
130 instant_unload_handler_.RunUnloadListenersOrDestroy(active_tab, index); | |
131 | |
132 GURL url = preview->GetURL(); | |
133 DCHECK(browser_->profile()->GetExtensionService()); | 159 DCHECK(browser_->profile()->GetExtensionService()); |
134 if (browser_->profile()->GetExtensionService()->IsInstalledApp(url)) { | 160 if (browser_->profile()->GetExtensionService()->IsInstalledApp(url)) { |
135 AppLauncherHandler::RecordAppLaunchType( | 161 AppLauncherHandler::RecordAppLaunchType( |
136 extension_misc::APP_LAUNCH_OMNIBOX_INSTANT); | 162 extension_misc::APP_LAUNCH_OMNIBOX_INSTANT); |
137 } | 163 } |
sreeram
2013/02/07 21:17:50
Move these lines (159-163) to outside the "if" sta
samarth
2013/02/07 22:11:55
Moved it to the top (I assume that is OK?). That w
sreeram
2013/02/07 22:18:15
Yup, moving to the top is totally fine.
| |
138 } | 164 } |
139 } | 165 } |
140 | 166 |
167 void BrowserInstantController::ReplaceWebContentsAt( | |
168 int index, | |
169 scoped_ptr<content::WebContents> new_contents) { | |
170 DCHECK_NE(TabStripModel::kNoTab, index); | |
171 content::WebContents* old_contents = | |
172 browser_->tab_strip_model()->GetWebContentsAt(index); | |
173 // TabStripModel takes ownership of |new_contents|. | |
174 browser_->tab_strip_model()->ReplaceWebContentsAt( | |
175 index, new_contents.release()); | |
176 // TODO(samarth): use scoped_ptr instead of comments to document ownership | |
177 // transfer. | |
178 // InstantUnloadHandler takes ownership of |old_contents|. | |
179 instant_unload_handler_.RunUnloadListenersOrDestroy(old_contents, index); | |
180 } | |
181 | |
141 void BrowserInstantController::SetInstantSuggestion( | 182 void BrowserInstantController::SetInstantSuggestion( |
142 const InstantSuggestion& suggestion) { | 183 const InstantSuggestion& suggestion) { |
143 browser_->window()->GetLocationBar()->SetInstantSuggestion(suggestion); | 184 browser_->window()->GetLocationBar()->SetInstantSuggestion(suggestion); |
144 } | 185 } |
145 | 186 |
146 gfx::Rect BrowserInstantController::GetInstantBounds() { | 187 gfx::Rect BrowserInstantController::GetInstantBounds() { |
147 return browser_->window()->GetInstantBounds(); | 188 return browser_->window()->GetInstantBounds(); |
148 } | 189 } |
149 | 190 |
150 void BrowserInstantController::InstantPreviewFocused() { | 191 void BrowserInstantController::InstantPreviewFocused() { |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
322 // part of the image overlay should draw, 'cos the origin is top-left. | 363 // part of the image overlay should draw, 'cos the origin is top-left. |
323 if (!browser_->search_model()->mode().is_ntp() || | 364 if (!browser_->search_model()->mode().is_ntp() || |
324 theme_info_.theme_id.empty() || | 365 theme_info_.theme_id.empty() || |
325 theme_info_.image_vertical_alignment == THEME_BKGRND_IMAGE_ALIGN_TOP) { | 366 theme_info_.image_vertical_alignment == THEME_BKGRND_IMAGE_ALIGN_TOP) { |
326 return; | 367 return; |
327 } | 368 } |
328 instant_.ThemeAreaHeightChanged(theme_area_height_); | 369 instant_.ThemeAreaHeightChanged(theme_area_height_); |
329 } | 370 } |
330 | 371 |
331 } // namespace chrome | 372 } // namespace chrome |
OLD | NEW |