OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/search/search_tab_helper.h" | 5 #include "chrome/browser/ui/search/search_tab_helper.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <set> | 8 #include <set> |
9 | 9 |
10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
11 #include "base/metrics/histogram_macros.h" | 11 #include "base/metrics/histogram_macros.h" |
12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
13 #include "build/build_config.h" | 13 #include "build/build_config.h" |
14 #include "chrome/browser/chrome_notification_types.h" | 14 #include "chrome/browser/chrome_notification_types.h" |
15 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
16 #include "chrome/browser/search/instant_service.h" | 16 #include "chrome/browser/search/instant_service.h" |
17 #include "chrome/browser/search/instant_service_factory.h" | 17 #include "chrome/browser/search/instant_service_factory.h" |
18 #include "chrome/browser/search/search.h" | 18 #include "chrome/browser/search/search.h" |
19 #include "chrome/browser/signin/signin_manager_factory.h" | 19 #include "chrome/browser/signin/signin_manager_factory.h" |
20 #include "chrome/browser/sync/profile_sync_service_factory.h" | 20 #include "chrome/browser/sync/profile_sync_service_factory.h" |
21 #include "chrome/browser/ui/browser_finder.h" | 21 #include "chrome/browser/ui/browser_finder.h" |
22 #include "chrome/browser/ui/browser_window.h" | 22 #include "chrome/browser/ui/browser_window.h" |
23 #include "chrome/browser/ui/location_bar/location_bar.h" | 23 #include "chrome/browser/ui/location_bar/location_bar.h" |
24 #include "chrome/browser/ui/omnibox/clipboard_utils.h" | 24 #include "chrome/browser/ui/omnibox/clipboard_utils.h" |
25 #include "chrome/browser/ui/search/instant_tab.h" | |
26 #include "chrome/browser/ui/search/search_ipc_router_policy_impl.h" | 25 #include "chrome/browser/ui/search/search_ipc_router_policy_impl.h" |
27 #include "chrome/browser/ui/tab_contents/core_tab_helper.h" | 26 #include "chrome/browser/ui/tab_contents/core_tab_helper.h" |
28 #include "chrome/browser/ui/webui/ntp/ntp_user_data_logger.h" | 27 #include "chrome/browser/ui/webui/ntp/ntp_user_data_logger.h" |
29 #include "chrome/common/url_constants.h" | 28 #include "chrome/common/url_constants.h" |
30 #include "chrome/grit/generated_resources.h" | 29 #include "chrome/grit/generated_resources.h" |
31 #include "components/browser_sync/profile_sync_service.h" | 30 #include "components/browser_sync/profile_sync_service.h" |
32 #include "components/google/core/browser/google_util.h" | 31 #include "components/google/core/browser/google_util.h" |
33 #include "components/omnibox/browser/omnibox_edit_model.h" | 32 #include "components/omnibox/browser/omnibox_edit_model.h" |
34 #include "components/omnibox/browser/omnibox_popup_model.h" | 33 #include "components/omnibox/browser/omnibox_popup_model.h" |
35 #include "components/omnibox/browser/omnibox_view.h" | 34 #include "components/omnibox/browser/omnibox_view.h" |
36 #include "components/search/search.h" | 35 #include "components/search/search.h" |
37 #include "components/signin/core/browser/signin_manager.h" | 36 #include "components/signin/core/browser/signin_manager.h" |
38 #include "components/strings/grit/components_strings.h" | 37 #include "components/strings/grit/components_strings.h" |
39 #include "content/public/browser/navigation_details.h" | 38 #include "content/public/browser/navigation_details.h" |
40 #include "content/public/browser/navigation_entry.h" | 39 #include "content/public/browser/navigation_entry.h" |
41 #include "content/public/browser/navigation_handle.h" | 40 #include "content/public/browser/navigation_handle.h" |
42 #include "content/public/browser/notification_service.h" | 41 #include "content/public/browser/notification_service.h" |
43 #include "content/public/browser/notification_source.h" | 42 #include "content/public/browser/notification_source.h" |
44 #include "content/public/browser/render_frame_host.h" | 43 #include "content/public/browser/render_frame_host.h" |
45 #include "content/public/browser/render_process_host.h" | 44 #include "content/public/browser/render_process_host.h" |
46 #include "content/public/browser/web_contents.h" | 45 #include "content/public/browser/web_contents.h" |
47 #include "content/public/common/browser_side_navigation_policy.h" | 46 #include "content/public/common/browser_side_navigation_policy.h" |
48 #include "content/public/common/referrer.h" | |
49 #include "google_apis/gaia/gaia_auth_util.h" | 47 #include "google_apis/gaia/gaia_auth_util.h" |
50 #include "net/base/net_errors.h" | 48 #include "net/base/net_errors.h" |
51 #include "ui/base/l10n/l10n_util.h" | 49 #include "ui/base/l10n/l10n_util.h" |
52 #include "ui/base/page_transition_types.h" | |
53 #include "url/gurl.h" | 50 #include "url/gurl.h" |
54 | 51 |
55 DEFINE_WEB_CONTENTS_USER_DATA_KEY(SearchTabHelper); | 52 DEFINE_WEB_CONTENTS_USER_DATA_KEY(SearchTabHelper); |
56 | 53 |
57 namespace { | 54 namespace { |
58 | 55 |
59 bool IsCacheableNTP(const content::WebContents* contents) { | 56 bool IsCacheableNTP(const content::WebContents* contents) { |
60 const content::NavigationEntry* entry = | 57 const content::NavigationEntry* entry = |
61 contents->GetController().GetLastCommittedEntry(); | 58 contents->GetController().GetLastCommittedEntry(); |
62 return search::NavEntryIsInstantNTP(contents, entry) && | 59 return search::NavEntryIsInstantNTP(contents, entry) && |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 return sync && | 118 return sync && |
122 sync->GetPreferredDataTypes().Has(syncer::HISTORY_DELETE_DIRECTIVES); | 119 sync->GetPreferredDataTypes().Has(syncer::HISTORY_DELETE_DIRECTIVES); |
123 } | 120 } |
124 | 121 |
125 } // namespace | 122 } // namespace |
126 | 123 |
127 SearchTabHelper::SearchTabHelper(content::WebContents* web_contents) | 124 SearchTabHelper::SearchTabHelper(content::WebContents* web_contents) |
128 : WebContentsObserver(web_contents), | 125 : WebContentsObserver(web_contents), |
129 is_search_enabled_(search::IsInstantExtendedAPIEnabled()), | 126 is_search_enabled_(search::IsInstantExtendedAPIEnabled()), |
130 web_contents_(web_contents), | 127 web_contents_(web_contents), |
131 ipc_router_( | 128 ipc_router_(web_contents, |
132 web_contents, | 129 this, |
133 this, | 130 base::MakeUnique<SearchIPCRouterPolicyImpl>(web_contents)), |
134 base::WrapUnique(new SearchIPCRouterPolicyImpl(web_contents))), | |
135 instant_service_(nullptr) { | 131 instant_service_(nullptr) { |
136 if (!is_search_enabled_) | 132 if (!is_search_enabled_) |
137 return; | 133 return; |
138 | 134 |
139 instant_service_ = | 135 instant_service_ = |
140 InstantServiceFactory::GetForProfile( | 136 InstantServiceFactory::GetForProfile( |
141 Profile::FromBrowserContext(web_contents_->GetBrowserContext())); | 137 Profile::FromBrowserContext(web_contents_->GetBrowserContext())); |
142 if (instant_service_) | 138 if (instant_service_) |
143 instant_service_->AddObserver(this); | 139 instant_service_->AddObserver(this); |
144 } | 140 } |
145 | 141 |
146 SearchTabHelper::~SearchTabHelper() { | 142 SearchTabHelper::~SearchTabHelper() { |
147 if (instant_service_) | 143 if (instant_service_) |
148 instant_service_->RemoveObserver(this); | 144 instant_service_->RemoveObserver(this); |
149 } | 145 } |
150 | 146 |
151 void SearchTabHelper::OmniboxInputStateChanged() { | 147 void SearchTabHelper::OmniboxInputStateChanged() { |
152 if (!is_search_enabled_) | 148 if (!is_search_enabled_) |
153 return; | 149 return; |
154 | 150 |
155 UpdateMode(false); | 151 UpdateMode(/*update_origin=*/false); |
156 } | 152 } |
157 | 153 |
158 void SearchTabHelper::OmniboxFocusChanged(OmniboxFocusState state, | 154 void SearchTabHelper::OmniboxFocusChanged(OmniboxFocusState state, |
159 OmniboxFocusChangeReason reason) { | 155 OmniboxFocusChangeReason reason) { |
160 content::NotificationService::current()->Notify( | 156 content::NotificationService::current()->Notify( |
161 chrome::NOTIFICATION_OMNIBOX_FOCUS_CHANGED, | 157 chrome::NOTIFICATION_OMNIBOX_FOCUS_CHANGED, |
162 content::Source<SearchTabHelper>(this), | 158 content::Source<SearchTabHelper>(this), |
163 content::NotificationService::NoDetails()); | 159 content::NotificationService::NoDetails()); |
164 | 160 |
165 ipc_router_.OmniboxFocusChanged(state, reason); | 161 ipc_router_.OmniboxFocusChanged(state, reason); |
166 | 162 |
167 // Don't send oninputstart/oninputend updates in response to focus changes | 163 // Don't send oninputstart/oninputend updates in response to focus changes |
168 // if there's a navigation in progress. This prevents Chrome from sending | 164 // if there's a navigation in progress. This prevents Chrome from sending |
169 // a spurious oninputend when the user accepts a match in the omnibox. | 165 // a spurious oninputend when the user accepts a match in the omnibox. |
170 if (web_contents_->GetController().GetPendingEntry() == nullptr) | 166 if (web_contents_->GetController().GetPendingEntry() == nullptr) |
171 ipc_router_.SetInputInProgress(IsInputInProgress()); | 167 ipc_router_.SetInputInProgress(IsInputInProgress()); |
172 } | 168 } |
173 | 169 |
174 void SearchTabHelper::NavigationEntryUpdated() { | 170 void SearchTabHelper::NavigationEntryUpdated() { |
175 if (!is_search_enabled_) | 171 if (!is_search_enabled_) |
176 return; | 172 return; |
177 | 173 |
178 UpdateMode(false); | 174 UpdateMode(/*update_origin=*/false); |
179 } | 175 } |
180 | 176 |
181 void SearchTabHelper::SetSuggestionToPrefetch( | 177 void SearchTabHelper::SetSuggestionToPrefetch( |
182 const InstantSuggestion& suggestion) { | 178 const InstantSuggestion& suggestion) { |
183 ipc_router_.SetSuggestionToPrefetch(suggestion); | 179 ipc_router_.SetSuggestionToPrefetch(suggestion); |
184 } | 180 } |
185 | 181 |
186 void SearchTabHelper::Submit(const base::string16& text, | 182 void SearchTabHelper::Submit(const base::string16& text, |
187 const EmbeddedSearchRequestParams& params) { | 183 const EmbeddedSearchRequestParams& params) { |
188 ipc_router_.Submit(text, params); | 184 ipc_router_.Submit(text, params); |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
296 } | 292 } |
297 | 293 |
298 void SearchTabHelper::NavigationEntryCommitted( | 294 void SearchTabHelper::NavigationEntryCommitted( |
299 const content::LoadCommittedDetails& load_details) { | 295 const content::LoadCommittedDetails& load_details) { |
300 if (!is_search_enabled_) | 296 if (!is_search_enabled_) |
301 return; | 297 return; |
302 | 298 |
303 if (!load_details.is_main_frame) | 299 if (!load_details.is_main_frame) |
304 return; | 300 return; |
305 | 301 |
306 UpdateMode(true); | 302 UpdateMode(/*update_origin=*/true); |
307 | 303 |
308 content::NavigationEntry* entry = | 304 InstantSupportChanged(InInstantProcess(profile(), web_contents_)); |
309 web_contents_->GetController().GetVisibleEntry(); | |
310 DCHECK(entry); | |
311 | |
312 model_.SetInstantSupportState(INSTANT_SUPPORT_UNKNOWN); | |
313 | 305 |
314 if (InInstantProcess(profile(), web_contents_)) | 306 if (InInstantProcess(profile(), web_contents_)) |
315 ipc_router_.OnNavigationEntryCommitted(); | 307 ipc_router_.OnNavigationEntryCommitted(); |
316 } | 308 } |
317 | 309 |
318 void SearchTabHelper::OnInstantSupportDetermined(bool supports_instant) { | |
319 InstantSupportChanged(supports_instant); | |
320 } | |
321 | |
322 void SearchTabHelper::ThemeInfoChanged(const ThemeBackgroundInfo& theme_info) { | 310 void SearchTabHelper::ThemeInfoChanged(const ThemeBackgroundInfo& theme_info) { |
323 ipc_router_.SendThemeBackgroundInfo(theme_info); | 311 ipc_router_.SendThemeBackgroundInfo(theme_info); |
324 } | 312 } |
325 | 313 |
326 void SearchTabHelper::MostVisitedItemsChanged( | 314 void SearchTabHelper::MostVisitedItemsChanged( |
327 const std::vector<InstantMostVisitedItem>& items) { | 315 const std::vector<InstantMostVisitedItem>& items) { |
328 ipc_router_.SendMostVisitedItems(items); | 316 ipc_router_.SendMostVisitedItems(items); |
329 } | 317 } |
330 | 318 |
331 void SearchTabHelper::FocusOmnibox(OmniboxFocusState state) { | 319 void SearchTabHelper::FocusOmnibox(OmniboxFocusState state) { |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
512 return Profile::FromBrowserContext(web_contents_->GetBrowserContext()); | 500 return Profile::FromBrowserContext(web_contents_->GetBrowserContext()); |
513 } | 501 } |
514 | 502 |
515 bool SearchTabHelper::IsInputInProgress() const { | 503 bool SearchTabHelper::IsInputInProgress() const { |
516 if (model_.mode().is_ntp()) | 504 if (model_.mode().is_ntp()) |
517 return false; | 505 return false; |
518 const OmniboxView* omnibox_view = GetOmniboxView(); | 506 const OmniboxView* omnibox_view = GetOmniboxView(); |
519 return omnibox_view && | 507 return omnibox_view && |
520 omnibox_view->model()->focus_state() == OMNIBOX_FOCUS_VISIBLE; | 508 omnibox_view->model()->focus_state() == OMNIBOX_FOCUS_VISIBLE; |
521 } | 509 } |
OLD | NEW |