Upstream version 10.38.220.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/infobars/infobar_service.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/search/instant_service.h"
13 #include "chrome/browser/search/instant_service_factory.h"
14 #include "chrome/browser/search/search.h"
15 #include "chrome/browser/ui/browser.h"
16 #include "chrome/browser/ui/browser_window.h"
17 #include "chrome/browser/ui/location_bar/location_bar.h"
18 #include "chrome/browser/ui/omnibox/omnibox_popup_model.h"
19 #include "chrome/browser/ui/omnibox/omnibox_view.h"
20 #include "chrome/browser/ui/search/instant_search_prerenderer.h"
21 #include "chrome/browser/ui/search/search_model.h"
22 #include "chrome/browser/ui/search/search_tab_helper.h"
23 #include "chrome/browser/ui/tabs/tab_strip_model.h"
24 #include "chrome/browser/ui/webui/ntp/app_launcher_handler.h"
25 #include "chrome/common/url_constants.h"
26 #include "content/public/browser/render_process_host.h"
27 #include "content/public/browser/user_metrics.h"
28 #include "content/public/browser/web_contents.h"
29
30
31 // Helpers --------------------------------------------------------------------
32
33 namespace {
34
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;
40 }
41
42 }  // namespace
43
44
45 // BrowserInstantController ---------------------------------------------------
46
47 BrowserInstantController::BrowserInstantController(Browser* browser)
48     : browser_(browser),
49       instant_(this) {
50   browser_->search_model()->AddObserver(this);
51
52   InstantService* instant_service =
53       InstantServiceFactory::GetForProfile(profile());
54   instant_service->AddObserver(this);
55 }
56
57 BrowserInstantController::~BrowserInstantController() {
58   browser_->search_model()->RemoveObserver(this);
59
60   InstantService* instant_service =
61       InstantServiceFactory::GetForProfile(profile());
62   instant_service->RemoveObserver(this);
63 }
64
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;
71
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;
76
77   const base::string16& search_terms =
78       chrome::ExtractSearchTermsFromURL(profile(), url);
79   if (search_terms.empty())
80     return false;
81
82   InstantSearchPrerenderer* prerenderer =
83       GetInstantSearchPrerenderer(profile());
84   if (prerenderer) {
85     if (prerenderer->CanCommitQuery(GetActiveWebContents(), search_terms)) {
86       // Submit query to render the prefetched results. Browser will swap the
87       // prerendered contents with the active tab contents.
88       prerenderer->Commit(search_terms);
89       return false;
90     } else {
91       prerenderer->Cancel();
92     }
93   }
94
95   // If we will not be replacing search terms from this URL, don't send to
96   // InstantController.
97   if (!chrome::IsQueryExtractionAllowedForURL(profile(), url))
98     return false;
99
100   return instant_.SubmitQuery(search_terms);
101 }
102
103 Profile* BrowserInstantController::profile() const {
104   return browser_->profile();
105 }
106
107 content::WebContents* BrowserInstantController::GetActiveWebContents() const {
108   return browser_->tab_strip_model()->GetActiveWebContents();
109 }
110
111 void BrowserInstantController::ActiveTabChanged() {
112   instant_.ActiveTabChanged();
113 }
114
115 void BrowserInstantController::TabDeactivated(content::WebContents* contents) {
116   instant_.TabDeactivated(contents);
117
118   InstantSearchPrerenderer* prerenderer =
119       GetInstantSearchPrerenderer(profile());
120   if (prerenderer)
121     prerenderer->Cancel();
122 }
123
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;
129
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"));
137
138     instant_.SearchModeChanged(old_state.mode, new_mode);
139   }
140
141   if (old_state.instant_support != new_state.instant_support)
142     instant_.InstantSupportChanged(new_state.instant_support);
143 }
144
145 void BrowserInstantController::DefaultSearchProviderChanged() {
146   InstantService* instant_service =
147       InstantServiceFactory::GetForProfile(profile());
148   if (!instant_service)
149     return;
150
151   TabStripModel* tab_model = browser_->tab_strip_model();
152   int count = tab_model->count();
153   for (int index = 0; index < count; ++index) {
154     content::WebContents* contents = tab_model->GetWebContentsAt(index);
155     if (!contents)
156       continue;
157
158     // Send new search URLs to the renderer.
159     content::RenderProcessHost* rph = contents->GetRenderProcessHost();
160     instant_service->SendSearchURLsToRenderer(rph);
161
162     // Reload the contents to ensure that it gets assigned to a non-priviledged
163     // renderer.
164     if (!instant_service->IsInstantProcess(rph->GetID()))
165       continue;
166
167     contents->GetController().Reload(false);
168
169     // As the reload was not triggered by the user we don't want to close any
170     // infobars. We have to tell the InfoBarService after the reload, otherwise
171     // it would ignore this call when
172     // WebContentsObserver::DidStartNavigationToPendingEntry is invoked.
173     InfoBarService::FromWebContents(contents)->set_ignore_next_reload();
174   }
175 }