OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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_engines/search_engine_tab_helper.h" | 5 #include "chrome/browser/ui/search_engines/search_engine_tab_helper.h" |
6 | 6 |
7 #include "base/metrics/histogram_macros.h" | 7 #include "base/metrics/histogram_macros.h" |
8 #include "chrome/browser/profiles/profile.h" | 8 #include "chrome/browser/profiles/profile.h" |
9 #include "chrome/browser/search_engines/template_url_fetcher_factory.h" | 9 #include "chrome/browser/search_engines/template_url_fetcher_factory.h" |
10 #include "chrome/browser/search_engines/template_url_service_factory.h" | 10 #include "chrome/browser/search_engines/template_url_service_factory.h" |
11 #include "chrome/browser/ui/search_engines/edit_search_engine_controller.h" | 11 #include "chrome/browser/ui/search_engines/edit_search_engine_controller.h" |
12 #include "chrome/browser/ui/search_engines/search_engine_tab_helper_delegate.h" | 12 #include "chrome/browser/ui/search_engines/search_engine_tab_helper_delegate.h" |
13 #include "chrome/common/pref_names.h" | |
14 #include "chrome/common/render_messages.h" | 13 #include "chrome/common/render_messages.h" |
15 #include "chrome/common/url_constants.h" | 14 #include "chrome/common/url_constants.h" |
16 #include "components/prefs/pref_service.h" | |
17 #include "components/search_engines/template_url.h" | 15 #include "components/search_engines/template_url.h" |
18 #include "components/search_engines/template_url_fetcher.h" | 16 #include "components/search_engines/template_url_fetcher.h" |
19 #include "components/search_engines/template_url_service.h" | 17 #include "components/search_engines/template_url_service.h" |
20 #include "content/public/browser/favicon_status.h" | 18 #include "content/public/browser/favicon_status.h" |
21 #include "content/public/browser/navigation_controller.h" | 19 #include "content/public/browser/navigation_controller.h" |
22 #include "content/public/browser/navigation_entry.h" | 20 #include "content/public/browser/navigation_entry.h" |
23 #include "content/public/browser/render_frame_host.h" | 21 #include "content/public/browser/render_frame_host.h" |
24 #include "content/public/browser/render_process_host.h" | 22 #include "content/public/browser/render_process_host.h" |
25 #include "content/public/browser/web_contents.h" | 23 #include "content/public/browser/web_contents.h" |
26 #include "content/public/common/frame_navigate_params.h" | 24 #include "content/public/common/frame_navigate_params.h" |
27 #include "content/public/common/url_fetcher.h" | 25 #include "content/public/common/url_fetcher.h" |
28 | 26 |
29 using content::NavigationController; | 27 using content::NavigationController; |
30 using content::NavigationEntry; | 28 using content::NavigationEntry; |
31 using content::WebContents; | 29 using content::WebContents; |
32 | 30 |
33 DEFINE_WEB_CONTENTS_USER_DATA_KEY(SearchEngineTabHelper); | 31 DEFINE_WEB_CONTENTS_USER_DATA_KEY(SearchEngineTabHelper); |
34 | 32 |
35 namespace { | 33 namespace { |
36 | 34 |
37 // Returns true if the entry's transition type is FORM_SUBMIT. | 35 // Returns true if the entry's transition type is FORM_SUBMIT. |
38 bool IsFormSubmit(const NavigationEntry* entry) { | 36 bool IsFormSubmit(const NavigationEntry* entry) { |
39 return (ui::PageTransitionStripQualifier(entry->GetTransitionType()) == | 37 return (ui::PageTransitionStripQualifier(entry->GetTransitionType()) == |
40 ui::PAGE_TRANSITION_FORM_SUBMIT); | 38 ui::PAGE_TRANSITION_FORM_SUBMIT); |
41 } | 39 } |
42 | 40 |
43 base::string16 GenerateKeywordFromNavigationEntry( | 41 base::string16 GenerateKeywordFromNavigationEntry( |
44 const NavigationEntry* entry, | 42 const NavigationEntry* entry) { |
45 const std::string& accept_languages) { | |
46 // Don't autogenerate keywords for pages that are the result of form | 43 // Don't autogenerate keywords for pages that are the result of form |
47 // submissions. | 44 // submissions. |
48 if (IsFormSubmit(entry)) | 45 if (IsFormSubmit(entry)) |
49 return base::string16(); | 46 return base::string16(); |
50 | 47 |
51 // We want to use the user typed URL if available since that represents what | 48 // We want to use the user typed URL if available since that represents what |
52 // the user typed to get here, and fall back on the regular URL if not. | 49 // the user typed to get here, and fall back on the regular URL if not. |
53 GURL url = entry->GetUserTypedURL(); | 50 GURL url = entry->GetUserTypedURL(); |
54 if (!url.is_valid()) { | 51 if (!url.is_valid()) { |
55 url = entry->GetURL(); | 52 url = entry->GetURL(); |
56 if (!url.is_valid()) | 53 if (!url.is_valid()) |
57 return base::string16(); | 54 return base::string16(); |
58 } | 55 } |
59 | 56 |
60 // Don't autogenerate keywords for referrers that | 57 // Don't autogenerate keywords for referrers that |
61 // a) are anything other than HTTP/HTTPS or | 58 // a) are anything other than HTTP/HTTPS or |
62 // b) have a path. | 59 // b) have a path. |
63 // | 60 // |
64 // If we relax the path constraint, we need to be sure to sanitize the path | 61 // If we relax the path constraint, we need to be sure to sanitize the path |
65 // elements and update AutocompletePopup to look for keywords using the path. | 62 // elements and update AutocompletePopup to look for keywords using the path. |
66 // See http://b/issue?id=863583. | 63 // See http://b/issue?id=863583. |
67 if (!(url.SchemeIs(url::kHttpScheme) || url.SchemeIs(url::kHttpsScheme)) || | 64 if (!(url.SchemeIs(url::kHttpScheme) || url.SchemeIs(url::kHttpsScheme)) || |
68 (url.path().length() > 1)) { | 65 (url.path().length() > 1)) { |
69 return base::string16(); | 66 return base::string16(); |
70 } | 67 } |
71 | 68 |
72 return TemplateURL::GenerateKeyword(url, accept_languages); | 69 return TemplateURL::GenerateKeyword(url); |
73 } | 70 } |
74 | 71 |
75 void AssociateURLFetcherWithWebContents(content::WebContents* web_contents, | 72 void AssociateURLFetcherWithWebContents(content::WebContents* web_contents, |
76 net::URLFetcher* url_fetcher) { | 73 net::URLFetcher* url_fetcher) { |
77 content::AssociateURLFetcherWithRenderFrame( | 74 content::AssociateURLFetcherWithRenderFrame( |
78 url_fetcher, | 75 url_fetcher, |
79 web_contents->GetURL(), | 76 web_contents->GetURL(), |
80 web_contents->GetRenderProcessHost()->GetID(), | 77 web_contents->GetRenderProcessHost()->GetID(), |
81 web_contents->GetMainFrame()->GetRoutingID()); | 78 web_contents->GetMainFrame()->GetRoutingID()); |
82 } | 79 } |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 (index > 0) && IsFormSubmit(entry); | 154 (index > 0) && IsFormSubmit(entry); |
158 entry = controller.GetEntryAtIndex(index)) | 155 entry = controller.GetEntryAtIndex(index)) |
159 --index; | 156 --index; |
160 if (!entry || IsFormSubmit(entry)) | 157 if (!entry || IsFormSubmit(entry)) |
161 return; | 158 return; |
162 | 159 |
163 // Autogenerate a keyword for the autodetected case; in the other cases we'll | 160 // Autogenerate a keyword for the autodetected case; in the other cases we'll |
164 // generate a keyword later after fetching the OSDD. | 161 // generate a keyword later after fetching the OSDD. |
165 base::string16 keyword; | 162 base::string16 keyword; |
166 if (provider_type == TemplateURLFetcher::AUTODETECTED_PROVIDER) { | 163 if (provider_type == TemplateURLFetcher::AUTODETECTED_PROVIDER) { |
167 keyword = GenerateKeywordFromNavigationEntry( | 164 keyword = GenerateKeywordFromNavigationEntry(entry); |
168 entry, profile->GetPrefs()->GetString(prefs::kAcceptLanguages)); | |
169 if (keyword.empty()) | 165 if (keyword.empty()) |
170 return; | 166 return; |
171 } | 167 } |
172 | 168 |
173 // Download the OpenSearch description document. If this is successful, a | 169 // Download the OpenSearch description document. If this is successful, a |
174 // new keyword will be created when done. | 170 // new keyword will be created when done. |
175 TemplateURLFetcherFactory::GetForProfile(profile)->ScheduleDownload( | 171 TemplateURLFetcherFactory::GetForProfile(profile)->ScheduleDownload( |
176 keyword, osdd_url, entry->GetFavicon().url, | 172 keyword, osdd_url, entry->GetFavicon().url, |
177 base::Bind(&AssociateURLFetcherWithWebContents, web_contents()), | 173 base::Bind(&AssociateURLFetcherWithWebContents, web_contents()), |
178 base::Bind(&SearchEngineTabHelper::OnDownloadedOSDD, | 174 base::Bind(&SearchEngineTabHelper::OnDownloadedOSDD, |
(...skipping 21 matching lines...) Expand all Loading... |
200 const NavigationController& controller = web_contents()->GetController(); | 196 const NavigationController& controller = web_contents()->GetController(); |
201 int last_index = controller.GetLastCommittedEntryIndex(); | 197 int last_index = controller.GetLastCommittedEntryIndex(); |
202 // When there was no previous page, the last index will be 0. This is | 198 // When there was no previous page, the last index will be 0. This is |
203 // normally due to a form submit that opened in a new tab. | 199 // normally due to a form submit that opened in a new tab. |
204 // TODO(brettw) bug 916126: we should support keywords when form submits | 200 // TODO(brettw) bug 916126: we should support keywords when form submits |
205 // happen in new tabs. | 201 // happen in new tabs. |
206 if (last_index <= 0) | 202 if (last_index <= 0) |
207 return; | 203 return; |
208 | 204 |
209 base::string16 keyword(GenerateKeywordFromNavigationEntry( | 205 base::string16 keyword(GenerateKeywordFromNavigationEntry( |
210 controller.GetEntryAtIndex(last_index - 1), | 206 controller.GetEntryAtIndex(last_index - 1))); |
211 profile->GetPrefs()->GetString(prefs::kAcceptLanguages))); | |
212 if (keyword.empty()) | 207 if (keyword.empty()) |
213 return; | 208 return; |
214 | 209 |
215 TemplateURLService* url_service = | 210 TemplateURLService* url_service = |
216 TemplateURLServiceFactory::GetForProfile(profile); | 211 TemplateURLServiceFactory::GetForProfile(profile); |
217 if (!url_service) | 212 if (!url_service) |
218 return; | 213 return; |
219 | 214 |
220 if (!url_service->loaded()) { | 215 if (!url_service->loaded()) { |
221 url_service->Load(); | 216 url_service->Load(); |
(...skipping 25 matching lines...) Expand all Loading... |
247 // the favicon url wasn't obtained before the load started. This assumes the | 242 // the favicon url wasn't obtained before the load started. This assumes the |
248 // latter. | 243 // latter. |
249 // TODO(sky): Need a way to set the favicon that doesn't involve generating | 244 // TODO(sky): Need a way to set the favicon that doesn't involve generating |
250 // its url. | 245 // its url. |
251 data.favicon_url = current_favicon.is_valid() ? | 246 data.favicon_url = current_favicon.is_valid() ? |
252 current_favicon : TemplateURL::GenerateFaviconURL(params.referrer.url); | 247 current_favicon : TemplateURL::GenerateFaviconURL(params.referrer.url); |
253 data.safe_for_autoreplace = true; | 248 data.safe_for_autoreplace = true; |
254 data.input_encodings.push_back(params.searchable_form_encoding); | 249 data.input_encodings.push_back(params.searchable_form_encoding); |
255 url_service->Add(new TemplateURL(data)); | 250 url_service->Add(new TemplateURL(data)); |
256 } | 251 } |
OLD | NEW |