Adding instrumentation to locate the source of jankiness
[chromium-blink-merge.git] / chrome / browser / ui / browser_instant_controller.cc
blob73d08d516b0779a5144175adc05ae11f8e2e6ae3
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/omnibox/omnibox_popup_model.h"
17 #include "chrome/browser/ui/omnibox/omnibox_view.h"
18 #include "chrome/browser/ui/search/instant_search_prerenderer.h"
19 #include "chrome/browser/ui/search/search_model.h"
20 #include "chrome/browser/ui/search/search_tab_helper.h"
21 #include "chrome/browser/ui/tabs/tab_strip_model.h"
22 #include "chrome/common/url_constants.h"
23 #include "content/public/browser/render_process_host.h"
24 #include "content/public/browser/user_metrics.h"
25 #include "content/public/browser/web_contents.h"
28 // Helpers --------------------------------------------------------------------
30 namespace {
32 InstantSearchPrerenderer* GetInstantSearchPrerenderer(Profile* profile) {
33 DCHECK(profile);
34 InstantService* instant_service =
35 InstantServiceFactory::GetForProfile(profile);
36 return instant_service ? instant_service->instant_search_prerenderer() : NULL;
39 } // namespace
42 // BrowserInstantController ---------------------------------------------------
44 BrowserInstantController::BrowserInstantController(Browser* browser)
45 : browser_(browser),
46 instant_(this) {
47 browser_->search_model()->AddObserver(this);
49 InstantService* instant_service =
50 InstantServiceFactory::GetForProfile(profile());
51 instant_service->AddObserver(this);
54 BrowserInstantController::~BrowserInstantController() {
55 browser_->search_model()->RemoveObserver(this);
57 InstantService* instant_service =
58 InstantServiceFactory::GetForProfile(profile());
59 instant_service->RemoveObserver(this);
62 bool BrowserInstantController::OpenInstant(WindowOpenDisposition disposition,
63 const GURL& url) {
64 // Unsupported dispositions.
65 if (disposition == NEW_BACKGROUND_TAB || disposition == NEW_WINDOW ||
66 disposition == NEW_FOREGROUND_TAB)
67 return false;
69 // The omnibox currently doesn't use other dispositions, so we don't attempt
70 // to handle them. If you hit this DCHECK file a bug and I'll (sky) add
71 // support for the new disposition.
72 DCHECK(disposition == CURRENT_TAB) << disposition;
74 const base::string16& search_terms =
75 chrome::ExtractSearchTermsFromURL(profile(), url);
76 if (search_terms.empty())
77 return false;
79 InstantSearchPrerenderer* prerenderer =
80 GetInstantSearchPrerenderer(profile());
81 if (prerenderer) {
82 if (prerenderer->CanCommitQuery(GetActiveWebContents(), search_terms)) {
83 // Submit query to render the prefetched results. Browser will swap the
84 // prerendered contents with the active tab contents.
85 prerenderer->Commit(search_terms);
86 return false;
87 } else {
88 prerenderer->Cancel();
92 // If we will not be replacing search terms from this URL, don't send to
93 // InstantController.
94 if (!chrome::IsQueryExtractionAllowedForURL(profile(), url))
95 return false;
97 return instant_.SubmitQuery(search_terms);
100 Profile* BrowserInstantController::profile() const {
101 return browser_->profile();
104 content::WebContents* BrowserInstantController::GetActiveWebContents() const {
105 return browser_->tab_strip_model()->GetActiveWebContents();
108 void BrowserInstantController::ActiveTabChanged() {
109 instant_.ActiveTabChanged();
112 void BrowserInstantController::TabDeactivated(content::WebContents* contents) {
113 instant_.TabDeactivated(contents);
115 InstantSearchPrerenderer* prerenderer =
116 GetInstantSearchPrerenderer(profile());
117 if (prerenderer)
118 prerenderer->Cancel();
121 void BrowserInstantController::ModelChanged(
122 const SearchModel::State& old_state,
123 const SearchModel::State& new_state) {
124 if (old_state.mode != new_state.mode) {
125 const SearchMode& new_mode = new_state.mode;
127 // Record some actions corresponding to the mode change. Note that to get
128 // the full story, it's necessary to look at other UMA actions as well,
129 // such as tab switches.
130 if (new_mode.is_search_results())
131 content::RecordAction(base::UserMetricsAction("InstantExtended.ShowSRP"));
132 else if (new_mode.is_ntp())
133 content::RecordAction(base::UserMetricsAction("InstantExtended.ShowNTP"));
135 instant_.SearchModeChanged(old_state.mode, new_mode);
138 if (old_state.instant_support != new_state.instant_support)
139 instant_.InstantSupportChanged(new_state.instant_support);
142 void BrowserInstantController::DefaultSearchProviderChanged() {
143 InstantService* instant_service =
144 InstantServiceFactory::GetForProfile(profile());
145 if (!instant_service)
146 return;
148 TabStripModel* tab_model = browser_->tab_strip_model();
149 int count = tab_model->count();
150 for (int index = 0; index < count; ++index) {
151 content::WebContents* contents = tab_model->GetWebContentsAt(index);
152 if (!contents)
153 continue;
155 // Send new search URLs to the renderer.
156 content::RenderProcessHost* rph = contents->GetRenderProcessHost();
157 instant_service->SendSearchURLsToRenderer(rph);
159 // Reload the contents to ensure that it gets assigned to a non-priviledged
160 // renderer.
161 if (!instant_service->IsInstantProcess(rph->GetID()))
162 continue;
164 contents->GetController().Reload(false);
166 // As the reload was not triggered by the user we don't want to close any
167 // infobars. We have to tell the InfoBarService after the reload, otherwise
168 // it would ignore this call when
169 // WebContentsObserver::DidStartNavigationToPendingEntry is invoked.
170 InfoBarService::FromWebContents(contents)->set_ignore_next_reload();