Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / browser_instant_controller.cc
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.
4
5 #include "chrome/browser/ui/browser_instant_controller.h"
6
7 #include "base/bind.h"
8 #include "chrome/browser/extensions/extension_service.h"
9 #include "chrome/browser/extensions/extension_web_ui.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/search/instant_service.h"
12 #include "chrome/browser/search/instant_service_factory.h"
13 #include "chrome/browser/search/search.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/browser_window.h"
16 #include "chrome/browser/ui/location_bar/location_bar.h"
17 #include "chrome/browser/ui/omnibox/omnibox_popup_model.h"
18 #include "chrome/browser/ui/omnibox/omnibox_view.h"
19 #include "chrome/browser/ui/search/instant_search_prerenderer.h"
20 #include "chrome/browser/ui/search/search_model.h"
21 #include "chrome/browser/ui/search/search_tab_helper.h"
22 #include "chrome/browser/ui/tabs/tab_strip_model.h"
23 #include "chrome/browser/ui/webui/ntp/app_launcher_handler.h"
24 #include "chrome/common/url_constants.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
29
30 // Helpers --------------------------------------------------------------------
31
32 namespace {
33
34 InstantSearchPrerenderer* GetInstantSearchPrerenderer(Profile* profile) {
35   DCHECK(profile);
36   InstantService* instant_service =
37       InstantServiceFactory::GetForProfile(profile);
38   return instant_service ? instant_service->instant_search_prerenderer() : NULL;
39 }
40
41 }  // namespace
42
43
44 // BrowserInstantController ---------------------------------------------------
45
46 BrowserInstantController::BrowserInstantController(Browser* browser)
47     : browser_(browser),
48       instant_(this) {
49   browser_->search_model()->AddObserver(this);
50
51   InstantService* instant_service =
52       InstantServiceFactory::GetForProfile(profile());
53   instant_service->AddObserver(this);
54 }
55
56 BrowserInstantController::~BrowserInstantController() {
57   browser_->search_model()->RemoveObserver(this);
58
59   InstantService* instant_service =
60       InstantServiceFactory::GetForProfile(profile());
61   instant_service->RemoveObserver(this);
62 }
63
64 bool BrowserInstantController::OpenInstant(WindowOpenDisposition disposition,
65                                            const GURL& url) {
66   // Unsupported dispositions.
67   if (disposition == NEW_BACKGROUND_TAB || disposition == NEW_WINDOW ||
68       disposition == NEW_FOREGROUND_TAB)
69     return false;
70
71   // The omnibox currently doesn't use other dispositions, so we don't attempt
72   // to handle them. If you hit this DCHECK file a bug and I'll (sky) add
73   // support for the new disposition.
74   DCHECK(disposition == CURRENT_TAB) << disposition;
75
76   const base::string16& search_terms =
77       chrome::ExtractSearchTermsFromURL(profile(), url);
78   if (search_terms.empty())
79     return false;
80
81   InstantSearchPrerenderer* prerenderer =
82       GetInstantSearchPrerenderer(profile());
83   if (prerenderer) {
84     if (prerenderer->CanCommitQuery(GetActiveWebContents(), search_terms)) {
85       // Submit query to render the prefetched results. Browser will swap the
86       // prerendered contents with the active tab contents.
87       prerenderer->Commit(search_terms);
88       return false;
89     } else {
90       prerenderer->Cancel();
91     }
92   }
93
94   // If we will not be replacing search terms from this URL, don't send to
95   // InstantController.
96   if (!chrome::IsQueryExtractionAllowedForURL(profile(), url))
97     return false;
98
99   return instant_.SubmitQuery(search_terms);
100 }
101
102 Profile* BrowserInstantController::profile() const {
103   return browser_->profile();
104 }
105
106 content::WebContents* BrowserInstantController::GetActiveWebContents() const {
107   return browser_->tab_strip_model()->GetActiveWebContents();
108 }
109
110 void BrowserInstantController::ActiveTabChanged() {
111   instant_.ActiveTabChanged();
112 }
113
114 void BrowserInstantController::TabDeactivated(content::WebContents* contents) {
115   instant_.TabDeactivated(contents);
116
117   InstantSearchPrerenderer* prerenderer =
118       GetInstantSearchPrerenderer(profile());
119   if (prerenderer)
120     prerenderer->Cancel();
121 }
122
123 void BrowserInstantController::ModelChanged(
124     const SearchModel::State& old_state,
125     const SearchModel::State& new_state) {
126   if (old_state.mode != new_state.mode) {
127     const SearchMode& new_mode = new_state.mode;
128
129     // Record some actions corresponding to the mode change. Note that to get
130     // the full story, it's necessary to look at other UMA actions as well,
131     // such as tab switches.
132     if (new_mode.is_search_results())
133       content::RecordAction(base::UserMetricsAction("InstantExtended.ShowSRP"));
134     else if (new_mode.is_ntp())
135       content::RecordAction(base::UserMetricsAction("InstantExtended.ShowNTP"));
136
137     instant_.SearchModeChanged(old_state.mode, new_mode);
138   }
139
140   if (old_state.instant_support != new_state.instant_support)
141     instant_.InstantSupportChanged(new_state.instant_support);
142 }
143
144 void BrowserInstantController::DefaultSearchProviderChanged() {
145   InstantService* instant_service =
146       InstantServiceFactory::GetForProfile(profile());
147   if (!instant_service)
148     return;
149
150   TabStripModel* tab_model = browser_->tab_strip_model();
151   int count = tab_model->count();
152   for (int index = 0; index < count; ++index) {
153     content::WebContents* contents = tab_model->GetWebContentsAt(index);
154     if (!contents)
155       continue;
156
157     // Send new search URLs to the renderer.
158     content::RenderProcessHost* rph = contents->GetRenderProcessHost();
159     instant_service->SendSearchURLsToRenderer(rph);
160
161     // Reload the contents to ensure that it gets assigned to a non-priviledged
162     // renderer.
163     if (!instant_service->IsInstantProcess(rph->GetID()))
164       continue;
165     contents->GetController().Reload(false);
166   }
167 }