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 "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/prefs/pref_service.h" | 8 #include "base/prefs/pref_service.h" |
9 #include "chrome/browser/extensions/extension_service.h" | 9 #include "chrome/browser/extensions/extension_service.h" |
10 #include "chrome/browser/extensions/extension_web_ui.h" | 10 #include "chrome/browser/extensions/extension_web_ui.h" |
11 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
12 #include "chrome/browser/search/instant_service.h" | 12 #include "chrome/browser/search/instant_service.h" |
13 #include "chrome/browser/search/instant_service_factory.h" | 13 #include "chrome/browser/search/instant_service_factory.h" |
14 #include "chrome/browser/search/search.h" | 14 #include "chrome/browser/search/search.h" |
15 #include "chrome/browser/search_engines/template_url.h" | 15 #include "chrome/browser/search_engines/template_url.h" |
16 #include "chrome/browser/search_engines/template_url_service.h" | 16 #include "chrome/browser/search_engines/template_url_service.h" |
17 #include "chrome/browser/search_engines/template_url_service_factory.h" | 17 #include "chrome/browser/search_engines/template_url_service_factory.h" |
18 #include "chrome/browser/ui/browser.h" | 18 #include "chrome/browser/ui/browser.h" |
19 #include "chrome/browser/ui/browser_window.h" | 19 #include "chrome/browser/ui/browser_window.h" |
20 #include "chrome/browser/ui/omnibox/location_bar.h" | 20 #include "chrome/browser/ui/omnibox/location_bar.h" |
21 #include "chrome/browser/ui/omnibox/omnibox_view.h" | 21 #include "chrome/browser/ui/omnibox/omnibox_view.h" |
22 #include "chrome/browser/ui/omnibox/omnibox_popup_model.h" | |
22 #include "chrome/browser/ui/search/instant_ntp.h" | 23 #include "chrome/browser/ui/search/instant_ntp.h" |
23 #include "chrome/browser/ui/search/search_model.h" | 24 #include "chrome/browser/ui/search/search_model.h" |
24 #include "chrome/browser/ui/search/search_tab_helper.h" | 25 #include "chrome/browser/ui/search/search_tab_helper.h" |
25 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 26 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
26 #include "chrome/browser/ui/webui/ntp/app_launcher_handler.h" | 27 #include "chrome/browser/ui/webui/ntp/app_launcher_handler.h" |
27 #include "chrome/common/pref_names.h" | 28 #include "chrome/common/pref_names.h" |
28 #include "chrome/common/url_constants.h" | 29 #include "chrome/common/url_constants.h" |
29 #include "components/user_prefs/pref_registry_syncable.h" | 30 #include "components/user_prefs/pref_registry_syncable.h" |
30 #include "content/public/browser/render_process_host.h" | 31 #include "content/public/browser/render_process_host.h" |
31 #include "content/public/browser/user_metrics.h" | 32 #include "content/public/browser/user_metrics.h" |
32 #include "content/public/browser/web_contents.h" | 33 #include "content/public/browser/web_contents.h" |
34 #include "content/public/browser/web_contents_view.h" | |
33 | 35 |
34 using content::UserMetricsAction; | 36 using content::UserMetricsAction; |
35 | 37 |
36 //////////////////////////////////////////////////////////////////////////////// | 38 //////////////////////////////////////////////////////////////////////////////// |
37 // BrowserInstantController, public: | 39 // BrowserInstantController, public: |
38 | 40 |
39 BrowserInstantController::BrowserInstantController(Browser* browser) | 41 BrowserInstantController::BrowserInstantController(Browser* browser) |
40 : browser_(browser), | 42 : browser_(browser), |
41 instant_(this, chrome::IsInstantExtendedAPIEnabled()), | 43 instant_(this, chrome::IsInstantExtendedAPIEnabled()), |
42 instant_unload_handler_(browser) { | 44 instant_unload_handler_(browser) { |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
144 void BrowserInstantController::ReplaceWebContentsAt( | 146 void BrowserInstantController::ReplaceWebContentsAt( |
145 int index, | 147 int index, |
146 scoped_ptr<content::WebContents> new_contents) { | 148 scoped_ptr<content::WebContents> new_contents) { |
147 DCHECK_NE(TabStripModel::kNoTab, index); | 149 DCHECK_NE(TabStripModel::kNoTab, index); |
148 scoped_ptr<content::WebContents> old_contents(browser_->tab_strip_model()-> | 150 scoped_ptr<content::WebContents> old_contents(browser_->tab_strip_model()-> |
149 ReplaceWebContentsAt(index, new_contents.release())); | 151 ReplaceWebContentsAt(index, new_contents.release())); |
150 instant_unload_handler_.RunUnloadListenersOrDestroy(old_contents.Pass(), | 152 instant_unload_handler_.RunUnloadListenersOrDestroy(old_contents.Pass(), |
151 index); | 153 index); |
152 } | 154 } |
153 | 155 |
154 void BrowserInstantController::FocusOmnibox(bool caret_visibility) { | 156 void BrowserInstantController::FocusOmnibox( |
157 const content::WebContents* contents, | |
158 OmniboxFocusState state) { | |
155 OmniboxView* omnibox_view = browser_->window()->GetLocationBar()-> | 159 OmniboxView* omnibox_view = browser_->window()->GetLocationBar()-> |
156 GetLocationEntry(); | 160 GetLocationEntry(); |
157 omnibox_view->SetFocus(); | 161 |
158 omnibox_view->model()->SetCaretVisibility(caret_visibility); | 162 // Do not add a default case in the switch block for the following reasons: |
159 if (!caret_visibility) { | 163 // (1) Explicitly handle the new states. If new states are added in the |
160 // If the user clicked on the fakebox, any text already in the omnibox | 164 // OmniboxFocusState, the compiler will warn the developer to handle the new |
161 // should get cleared when they start typing. Selecting all the existing | 165 // states. |
162 // text is a convenient way to accomplish this. It also gives a slight | 166 // (2) An attacker may control the renderer and sends the browser process a |
163 // visual cue to users who really understand selection state about what will | 167 // malformed IPC. This function responds to the invalid |state| values by |
164 // happen if they start typing. | 168 // doing nothing instead of crashing the browser process (intentional no-op). |
165 omnibox_view->SelectAll(false); | 169 switch (state) { |
170 case OMNIBOX_FOCUS_VISIBLE: | |
171 omnibox_view->SetFocus(); | |
172 omnibox_view->model()->SetCaretVisibility(true); | |
173 break; | |
174 case OMNIBOX_FOCUS_INVISIBLE: | |
175 omnibox_view->SetFocus(); | |
176 omnibox_view->model()->SetCaretVisibility(false); | |
177 // If the user clicked on the fakebox, any text already in the omnibox | |
178 // should get cleared when they start typing. Selecting all the existing | |
179 // text is a convenient way to accomplish this. It also gives a slight | |
180 // visual cue to users who really understand selection state about what | |
181 // will happen if they start typing. | |
182 omnibox_view->SelectAll(false); | |
183 break; | |
184 case OMNIBOX_FOCUS_NONE: | |
185 // Remove focus only if the popup is closed. This will prevent someone | |
186 // from changing the omnibox value and closing the popup without user | |
187 // interaction. | |
188 if (!omnibox_view->model()->popup_model()->IsOpen()) | |
189 contents->GetView()->Focus(); | |
samarth
2013/07/31 01:44:37
Instead of passing in |contents| here you can get
jfweitz
2013/08/03 00:02:00
Done.
| |
190 break; | |
166 } | 191 } |
167 } | 192 } |
168 | 193 |
169 content::WebContents* BrowserInstantController::GetActiveWebContents() const { | 194 content::WebContents* BrowserInstantController::GetActiveWebContents() const { |
170 return browser_->tab_strip_model()->GetActiveWebContents(); | 195 return browser_->tab_strip_model()->GetActiveWebContents(); |
171 } | 196 } |
172 | 197 |
173 void BrowserInstantController::ActiveTabChanged() { | 198 void BrowserInstantController::ActiveTabChanged() { |
174 instant_.ActiveTabChanged(); | 199 instant_.ActiveTabChanged(); |
175 } | 200 } |
176 | 201 |
177 void BrowserInstantController::TabDeactivated(content::WebContents* contents) { | 202 void BrowserInstantController::TabDeactivated(content::WebContents* contents) { |
178 instant_.TabDeactivated(contents); | 203 instant_.TabDeactivated(contents); |
179 } | 204 } |
180 | 205 |
181 void BrowserInstantController::OpenURL( | 206 void BrowserInstantController::OpenURL( |
182 const GURL& url, | 207 const GURL& url, |
183 content::PageTransition transition, | 208 content::PageTransition transition, |
184 WindowOpenDisposition disposition) { | 209 WindowOpenDisposition disposition) { |
185 browser_->OpenURL(content::OpenURLParams(url, | 210 browser_->OpenURL(content::OpenURLParams(url, |
186 content::Referrer(), | 211 content::Referrer(), |
187 disposition, | 212 disposition, |
188 transition, | 213 transition, |
189 false)); | 214 false)); |
190 } | 215 } |
191 | 216 |
217 void BrowserInstantController::PasteIntoOmnibox() { | |
218 OmniboxView* omnibox_view = browser_->window()->GetLocationBar()-> | |
219 GetLocationEntry(); | |
220 string16 clipboardText = omnibox_view->GetClipboardText(); | |
221 | |
222 if (!clipboardText.empty()) { | |
223 if (!omnibox_view->model()->has_focus()) | |
224 omnibox_view->SetFocus(); | |
225 omnibox_view->OnBeforePossibleChange(); | |
226 omnibox_view->SetUserText(clipboardText); | |
227 omnibox_view->model()->on_paste(); | |
samarth
2013/07/31 01:44:37
This needs to be called before you SetUserText.
jfweitz
2013/08/03 00:02:00
Done.
| |
228 omnibox_view->OnAfterPossibleChange(); | |
229 } | |
230 } | |
231 | |
192 void BrowserInstantController::SetOmniboxBounds(const gfx::Rect& bounds) { | 232 void BrowserInstantController::SetOmniboxBounds(const gfx::Rect& bounds) { |
193 instant_.SetOmniboxBounds(bounds); | 233 instant_.SetOmniboxBounds(bounds); |
194 } | 234 } |
195 | 235 |
196 void BrowserInstantController::ToggleVoiceSearch() { | 236 void BrowserInstantController::ToggleVoiceSearch() { |
197 instant_.ToggleVoiceSearch(); | 237 instant_.ToggleVoiceSearch(); |
198 } | 238 } |
199 | 239 |
200 //////////////////////////////////////////////////////////////////////////////// | 240 //////////////////////////////////////////////////////////////////////////////// |
201 // BrowserInstantController, SearchModelObserver implementation: | 241 // BrowserInstantController, SearchModelObserver implementation: |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
254 | 294 |
255 if (!instant_service->IsInstantProcess( | 295 if (!instant_service->IsInstantProcess( |
256 contents->GetRenderProcessHost()->GetID())) | 296 contents->GetRenderProcessHost()->GetID())) |
257 continue; | 297 continue; |
258 | 298 |
259 // Reload the contents to ensure that it gets assigned to a non-priviledged | 299 // Reload the contents to ensure that it gets assigned to a non-priviledged |
260 // renderer. | 300 // renderer. |
261 contents->GetController().Reload(false); | 301 contents->GetController().Reload(false); |
262 } | 302 } |
263 } | 303 } |
OLD | NEW |