BookmarkManager: Fix 'new folder text field size changes on clicking it' issue.
[chromium-blink-merge.git] / chrome / browser / ui / browser_instant_controller.cc
blobcd0f66ef39eab4170f54ccb3c64e3f6807d64de9
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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/browser_instant_controller.h"
7 #include "base/bind.h"
8 #include "chrome/browser/infobars/infobar_service.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/search/instant_service.h"
11 #include "chrome/browser/search/instant_service_factory.h"
12 #include "chrome/browser/search/search.h"
13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/browser_window.h"
15 #include "chrome/browser/ui/location_bar/location_bar.h"
16 #include "chrome/browser/ui/search/instant_search_prerenderer.h"
17 #include "chrome/browser/ui/search/search_model.h"
18 #include "chrome/browser/ui/search/search_tab_helper.h"
19 #include "chrome/browser/ui/tabs/tab_strip_model.h"
20 #include "chrome/common/instant_types.h"
21 #include "chrome/common/url_constants.h"
22 #include "components/omnibox/browser/omnibox_popup_model.h"
23 #include "components/omnibox/browser/omnibox_view.h"
24 #include "content/public/browser/navigation_controller.h"
25 #include "content/public/browser/render_process_host.h"
26 #include "content/public/browser/user_metrics.h"
27 #include "content/public/browser/web_contents.h"
28 #include "content/public/common/referrer.h"
29 #include "ui/base/page_transition_types.h"
31 // Helpers --------------------------------------------------------------------
33 namespace {
35 InstantSearchPrerenderer* GetInstantSearchPrerenderer(Profile* profile) {
36 DCHECK(profile);
37 InstantService* instant_service =
38 InstantServiceFactory::GetForProfile(profile);
39 return instant_service ? instant_service->instant_search_prerenderer() : NULL;
42 } // namespace
45 // BrowserInstantController ---------------------------------------------------
47 BrowserInstantController::BrowserInstantController(Browser* browser)
48 : browser_(browser),
49 instant_(this) {
50 browser_->search_model()->AddObserver(this);
52 InstantService* instant_service =
53 InstantServiceFactory::GetForProfile(profile());
54 instant_service->AddObserver(this);
57 BrowserInstantController::~BrowserInstantController() {
58 browser_->search_model()->RemoveObserver(this);
60 InstantService* instant_service =
61 InstantServiceFactory::GetForProfile(profile());
62 instant_service->RemoveObserver(this);
65 bool BrowserInstantController::OpenInstant(WindowOpenDisposition disposition,
66 const GURL& url) {
67 // Unsupported dispositions.
68 if (disposition == NEW_BACKGROUND_TAB || disposition == NEW_WINDOW ||
69 disposition == NEW_FOREGROUND_TAB)
70 return false;
72 // The omnibox currently doesn't use other dispositions, so we don't attempt
73 // to handle them. If you hit this DCHECK file a bug and I'll (sky) add
74 // support for the new disposition.
75 DCHECK(disposition == CURRENT_TAB) << disposition;
77 const base::string16& search_terms =
78 search::ExtractSearchTermsFromURL(profile(), url);
79 EmbeddedSearchRequestParams request_params(url);
80 if (search_terms.empty())
81 return false;
83 InstantSearchPrerenderer* prerenderer =
84 GetInstantSearchPrerenderer(profile());
85 if (prerenderer) {
86 if (prerenderer->CanCommitQuery(GetActiveWebContents(), search_terms)) {
87 // Submit query to render the prefetched results. Browser will swap the
88 // prerendered contents with the active tab contents.
89 prerenderer->Commit(search_terms, request_params);
90 return false;
91 } else {
92 prerenderer->Cancel();
96 // If we will not be replacing search terms from this URL, don't send to
97 // InstantController.
98 if (!search::IsQueryExtractionAllowedForURL(profile(), url))
99 return false;
100 return instant_.SubmitQuery(search_terms, request_params);
103 Profile* BrowserInstantController::profile() const {
104 return browser_->profile();
107 content::WebContents* BrowserInstantController::GetActiveWebContents() const {
108 return browser_->tab_strip_model()->GetActiveWebContents();
111 void BrowserInstantController::ActiveTabChanged() {
112 instant_.ActiveTabChanged();
115 void BrowserInstantController::TabDeactivated(content::WebContents* contents) {
116 instant_.TabDeactivated(contents);
118 InstantSearchPrerenderer* prerenderer =
119 GetInstantSearchPrerenderer(profile());
120 if (prerenderer)
121 prerenderer->Cancel();
124 void BrowserInstantController::ModelChanged(
125 const SearchModel::State& old_state,
126 const SearchModel::State& new_state) {
127 if (old_state.mode != new_state.mode) {
128 const SearchMode& new_mode = new_state.mode;
130 // Record some actions corresponding to the mode change. Note that to get
131 // the full story, it's necessary to look at other UMA actions as well,
132 // such as tab switches.
133 if (new_mode.is_search_results())
134 content::RecordAction(base::UserMetricsAction("InstantExtended.ShowSRP"));
135 else if (new_mode.is_ntp())
136 content::RecordAction(base::UserMetricsAction("InstantExtended.ShowNTP"));
138 instant_.SearchModeChanged(old_state.mode, new_mode);
141 if (old_state.instant_support != new_state.instant_support)
142 instant_.InstantSupportChanged(new_state.instant_support);
145 void BrowserInstantController::DefaultSearchProviderChanged(
146 bool google_base_url_domain_changed) {
147 InstantService* instant_service =
148 InstantServiceFactory::GetForProfile(profile());
149 if (!instant_service)
150 return;
152 TabStripModel* tab_model = browser_->tab_strip_model();
153 int count = tab_model->count();
154 for (int index = 0; index < count; ++index) {
155 content::WebContents* contents = tab_model->GetWebContentsAt(index);
156 if (!contents)
157 continue;
159 // Send new search URLs to the renderer.
160 content::RenderProcessHost* rph = contents->GetRenderProcessHost();
161 instant_service->SendSearchURLsToRenderer(rph);
163 if (!instant_service->IsInstantProcess(rph->GetID()))
164 continue;
166 if (google_base_url_domain_changed &&
167 SearchTabHelper::FromWebContents(contents)->model()->mode().is_ntp()) {
168 // Replace the server NTP with the local NTP.
169 content::NavigationController::LoadURLParams params(
170 search::GetLocalInstantURL(profile()));
171 params.should_replace_current_entry = true;
172 params.referrer = content::Referrer();
173 params.transition_type = ui::PAGE_TRANSITION_RELOAD;
174 contents->GetController().LoadURLWithParams(params);
175 } else {
176 // Reload the contents to ensure that it gets assigned to a
177 // non-priviledged renderer.
178 contents->GetController().Reload(false);
180 // As the reload was not triggered by the user we don't want to close any
181 // infobars. We have to tell the InfoBarService after the reload,
182 // otherwise it would ignore this call when
183 // WebContentsObserver::DidStartNavigationToPendingEntry is invoked.
184 InfoBarService::FromWebContents(contents)->set_ignore_next_reload();