- add sources.
[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/omnibox/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_ntp.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 #include "content/public/browser/web_contents_view.h"
29
30 using content::UserMetricsAction;
31
32 ////////////////////////////////////////////////////////////////////////////////
33 // BrowserInstantController, public:
34
35 BrowserInstantController::BrowserInstantController(Browser* browser)
36     : browser_(browser),
37       instant_(this),
38       instant_unload_handler_(browser) {
39   browser_->search_model()->AddObserver(this);
40
41   InstantService* instant_service =
42       InstantServiceFactory::GetForProfile(profile());
43   instant_service->OnBrowserInstantControllerCreated();
44   instant_service->AddObserver(this);
45 }
46
47 BrowserInstantController::~BrowserInstantController() {
48   browser_->search_model()->RemoveObserver(this);
49
50   InstantService* instant_service =
51       InstantServiceFactory::GetForProfile(profile());
52   instant_service->RemoveObserver(this);
53   instant_service->OnBrowserInstantControllerDestroyed();
54 }
55
56 bool BrowserInstantController::MaybeSwapInInstantNTPContents(
57     const GURL& url,
58     content::WebContents* source_contents,
59     content::WebContents** target_contents) {
60   if (url != GURL(chrome::kChromeUINewTabURL))
61     return false;
62
63   GURL extension_url(url);
64   if (ExtensionWebUI::HandleChromeURLOverride(&extension_url, profile())) {
65     // If there is an extension overriding the NTP do not use the Instant NTP.
66     return false;
67   }
68
69   InstantService* instant_service =
70       InstantServiceFactory::GetForProfile(profile());
71   scoped_ptr<content::WebContents> instant_ntp =
72       instant_service->ReleaseNTPContents();
73   if (!instant_ntp)
74     return false;
75
76   *target_contents = instant_ntp.get();
77   if (source_contents) {
78     // If the Instant NTP hasn't yet committed an entry, we can't call
79     // CopyStateFromAndPrune.  Instead, load the Local NTP URL directly in the
80     // source contents.
81     // TODO(sreeram): Always using the local URL is wrong in the case of the
82     // first tab in a window where we might want to use the remote URL. Fix.
83     if (!instant_ntp->GetController().CanPruneAllButVisible()) {
84       source_contents->GetController().LoadURL(chrome::GetLocalInstantURL(
85           profile()), content::Referrer(), content::PAGE_TRANSITION_GENERATED,
86           std::string());
87       *target_contents = source_contents;
88     } else {
89       instant_ntp->GetController().CopyStateFromAndPrune(
90           &source_contents->GetController());
91       ReplaceWebContentsAt(
92           browser_->tab_strip_model()->GetIndexOfWebContents(source_contents),
93           instant_ntp.Pass());
94     }
95   } else {
96     // If the Instant NTP hasn't yet committed an entry, we can't call
97     // PruneAllButVisible.  In that case, there shouldn't be any entries to
98     // prune anyway.
99     if (instant_ntp->GetController().CanPruneAllButVisible())
100       instant_ntp->GetController().PruneAllButVisible();
101     else
102       CHECK(!instant_ntp->GetController().GetLastCommittedEntry());
103
104     // If |source_contents| is NULL, then the caller is responsible for
105     // inserting instant_ntp into the tabstrip and will take ownership.
106     ignore_result(instant_ntp.release());
107   }
108   return true;
109 }
110
111 bool BrowserInstantController::OpenInstant(WindowOpenDisposition disposition,
112                                            const GURL& url) {
113   // Unsupported dispositions.
114   if (disposition == NEW_BACKGROUND_TAB || disposition == NEW_WINDOW ||
115       disposition == NEW_FOREGROUND_TAB)
116     return false;
117
118   // The omnibox currently doesn't use other dispositions, so we don't attempt
119   // to handle them. If you hit this DCHECK file a bug and I'll (sky) add
120   // support for the new disposition.
121   DCHECK(disposition == CURRENT_TAB) << disposition;
122
123   // If we will not be replacing search terms from this URL, don't send to
124   // InstantController.
125   const string16& search_terms =
126       chrome::GetSearchTermsFromURL(browser_->profile(), url);
127   if (search_terms.empty())
128     return false;
129
130   return instant_.SubmitQuery(search_terms);
131 }
132
133 Profile* BrowserInstantController::profile() const {
134   return browser_->profile();
135 }
136
137 void BrowserInstantController::ReplaceWebContentsAt(
138     int index,
139     scoped_ptr<content::WebContents> new_contents) {
140   DCHECK_NE(TabStripModel::kNoTab, index);
141   scoped_ptr<content::WebContents> old_contents(browser_->tab_strip_model()->
142       ReplaceWebContentsAt(index, new_contents.release()));
143   instant_unload_handler_.RunUnloadListenersOrDestroy(old_contents.Pass(),
144                                                       index);
145 }
146
147 content::WebContents* BrowserInstantController::GetActiveWebContents() const {
148   return browser_->tab_strip_model()->GetActiveWebContents();
149 }
150
151 void BrowserInstantController::ActiveTabChanged() {
152   instant_.ActiveTabChanged();
153 }
154
155 void BrowserInstantController::TabDeactivated(content::WebContents* contents) {
156   instant_.TabDeactivated(contents);
157 }
158
159 void BrowserInstantController::SetOmniboxBounds(const gfx::Rect& bounds) {
160   instant_.SetOmniboxBounds(bounds);
161 }
162
163 void BrowserInstantController::ToggleVoiceSearch() {
164   instant_.ToggleVoiceSearch();
165 }
166
167 ////////////////////////////////////////////////////////////////////////////////
168 // BrowserInstantController, SearchModelObserver implementation:
169
170 void BrowserInstantController::ModelChanged(
171     const SearchModel::State& old_state,
172     const SearchModel::State& new_state) {
173   if (old_state.mode != new_state.mode) {
174     const SearchMode& new_mode = new_state.mode;
175
176     // Record some actions corresponding to the mode change. Note that to get
177     // the full story, it's necessary to look at other UMA actions as well,
178     // such as tab switches.
179     if (new_mode.is_search_results())
180       content::RecordAction(UserMetricsAction("InstantExtended.ShowSRP"));
181     else if (new_mode.is_ntp())
182       content::RecordAction(UserMetricsAction("InstantExtended.ShowNTP"));
183
184     instant_.SearchModeChanged(old_state.mode, new_mode);
185   }
186
187   if (old_state.instant_support != new_state.instant_support)
188     instant_.InstantSupportChanged(new_state.instant_support);
189 }
190
191 ////////////////////////////////////////////////////////////////////////////////
192 // BrowserInstantController, InstantServiceObserver implementation:
193
194 void BrowserInstantController::DefaultSearchProviderChanged() {
195   ReloadTabsInInstantProcess();
196 }
197
198 void BrowserInstantController::GoogleURLUpdated() {
199   ReloadTabsInInstantProcess();
200 }
201
202 void BrowserInstantController::ReloadTabsInInstantProcess() {
203   InstantService* instant_service =
204       InstantServiceFactory::GetForProfile(profile());
205   if (!instant_service)
206     return;
207
208   TabStripModel* tab_model = browser_->tab_strip_model();
209   int count = tab_model->count();
210   for (int index = 0; index < count; ++index) {
211     content::WebContents* contents = tab_model->GetWebContentsAt(index);
212     if (!contents)
213       continue;
214
215     // Send new search URLs to the renderer.
216     content::RenderProcessHost* rph = contents->GetRenderProcessHost();
217     instant_service->SendSearchURLsToRenderer(rph);
218
219     // Reload the contents to ensure that it gets assigned to a non-priviledged
220     // renderer.
221     if (!instant_service->IsInstantProcess(rph->GetID()))
222       continue;
223     contents->GetController().Reload(false);
224   }
225 }