Chromium Code Reviews| 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 "chrome/browser/profiles/profile.h" | 7 #include "chrome/browser/profiles/profile.h" |
| 8 #include "chrome/browser/search_engines/template_url_fetcher_factory.h" | 8 #include "chrome/browser/search_engines/template_url_fetcher_factory.h" |
| 9 #include "chrome/browser/search_engines/template_url_service_factory.h" | 9 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 10 #include "chrome/browser/ui/search_engines/search_engine_tab_helper_delegate.h" | 10 #include "chrome/browser/ui/search_engines/search_engine_tab_helper_delegate.h" |
| 11 #include "chrome/common/render_messages.h" | 11 #include "chrome/common/render_messages.h" |
| 12 #include "chrome/common/url_constants.h" | 12 #include "chrome/common/url_constants.h" |
| 13 #include "components/search_engines/search_terms_data.h" | |
| 13 #include "components/search_engines/template_url.h" | 14 #include "components/search_engines/template_url.h" |
| 14 #include "components/search_engines/template_url_fetcher.h" | 15 #include "components/search_engines/template_url_fetcher.h" |
| 15 #include "components/search_engines/template_url_service.h" | 16 #include "components/search_engines/template_url_service.h" |
| 16 #include "content/public/browser/favicon_status.h" | 17 #include "content/public/browser/favicon_status.h" |
| 17 #include "content/public/browser/navigation_controller.h" | 18 #include "content/public/browser/navigation_controller.h" |
| 18 #include "content/public/browser/navigation_entry.h" | 19 #include "content/public/browser/navigation_entry.h" |
| 19 #include "content/public/browser/render_frame_host.h" | 20 #include "content/public/browser/render_frame_host.h" |
| 20 #include "content/public/browser/render_process_host.h" | 21 #include "content/public/browser/render_process_host.h" |
| 21 #include "content/public/browser/web_contents.h" | 22 #include "content/public/browser/web_contents.h" |
| 22 #include "content/public/common/frame_navigate_params.h" | 23 #include "content/public/common/frame_navigate_params.h" |
| 23 #include "content/public/common/url_fetcher.h" | 24 #include "content/public/common/url_fetcher.h" |
| 24 | 25 |
| 25 using content::NavigationController; | 26 using content::NavigationController; |
| 26 using content::NavigationEntry; | 27 using content::NavigationEntry; |
| 27 using content::WebContents; | 28 using content::WebContents; |
| 28 | 29 |
| 29 DEFINE_WEB_CONTENTS_USER_DATA_KEY(SearchEngineTabHelper); | 30 DEFINE_WEB_CONTENTS_USER_DATA_KEY(SearchEngineTabHelper); |
| 30 | 31 |
| 31 namespace { | 32 namespace { |
| 32 | 33 |
| 33 // Returns true if the entry's transition type is FORM_SUBMIT. | 34 // Returns true if the entry's transition type is FORM_SUBMIT. |
| 34 bool IsFormSubmit(const NavigationEntry* entry) { | 35 bool IsFormSubmit(const NavigationEntry* entry) { |
| 35 return (ui::PageTransitionStripQualifier(entry->GetTransitionType()) == | 36 return (ui::PageTransitionStripQualifier(entry->GetTransitionType()) == |
| 36 ui::PAGE_TRANSITION_FORM_SUBMIT); | 37 ui::PAGE_TRANSITION_FORM_SUBMIT); |
| 37 } | 38 } |
| 38 | 39 |
| 39 base::string16 GenerateKeywordFromNavigationEntry( | 40 base::string16 GenerateKeywordFromNavigationEntry( |
| 40 const NavigationEntry* entry) { | 41 const NavigationEntry* entry, |
| 42 const std::string& accept_languages) { | |
| 41 // 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 |
| 42 // submissions. | 44 // submissions. |
| 43 if (IsFormSubmit(entry)) | 45 if (IsFormSubmit(entry)) |
| 44 return base::string16(); | 46 return base::string16(); |
| 45 | 47 |
| 46 // 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 |
| 47 // 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. |
| 48 GURL url = entry->GetUserTypedURL(); | 50 GURL url = entry->GetUserTypedURL(); |
| 49 if (!url.is_valid()) { | 51 if (!url.is_valid()) { |
| 50 url = entry->GetURL(); | 52 url = entry->GetURL(); |
| 51 if (!url.is_valid()) | 53 if (!url.is_valid()) |
| 52 return base::string16(); | 54 return base::string16(); |
| 53 } | 55 } |
| 54 | 56 |
| 55 // Don't autogenerate keywords for referrers that are anything other than HTTP | 57 // Don't autogenerate keywords for referrers that are anything other than HTTP |
| 56 // or have a path. | 58 // or have a path. |
| 57 // | 59 // |
| 58 // If we relax the path constraint, we need to be sure to sanitize the path | 60 // If we relax the path constraint, we need to be sure to sanitize the path |
| 59 // elements and update AutocompletePopup to look for keywords using the path. | 61 // elements and update AutocompletePopup to look for keywords using the path. |
| 60 // See http://b/issue?id=863583. | 62 // See http://b/issue?id=863583. |
| 61 if (!url.SchemeIs(url::kHttpScheme) || (url.path().length() > 1)) | 63 if (!url.SchemeIs(url::kHttpScheme) || (url.path().length() > 1)) |
| 62 return base::string16(); | 64 return base::string16(); |
| 63 | 65 |
| 64 return TemplateURL::GenerateKeyword(url); | 66 return TemplateURL::GenerateKeyword(url, accept_languages); |
| 65 } | 67 } |
| 66 | 68 |
| 67 void AssociateURLFetcherWithWebContents(content::WebContents* web_contents, | 69 void AssociateURLFetcherWithWebContents(content::WebContents* web_contents, |
| 68 net::URLFetcher* url_fetcher) { | 70 net::URLFetcher* url_fetcher) { |
| 69 content::AssociateURLFetcherWithRenderFrame( | 71 content::AssociateURLFetcherWithRenderFrame( |
| 70 url_fetcher, | 72 url_fetcher, |
| 71 web_contents->GetURL(), | 73 web_contents->GetURL(), |
| 72 web_contents->GetRenderProcessHost()->GetID(), | 74 web_contents->GetRenderProcessHost()->GetID(), |
| 73 web_contents->GetMainFrame()->GetRoutingID()); | 75 web_contents->GetMainFrame()->GetRoutingID()); |
| 74 } | 76 } |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 136 (index > 0) && IsFormSubmit(entry); | 138 (index > 0) && IsFormSubmit(entry); |
| 137 entry = controller.GetEntryAtIndex(index)) | 139 entry = controller.GetEntryAtIndex(index)) |
| 138 --index; | 140 --index; |
| 139 if (!entry || IsFormSubmit(entry)) | 141 if (!entry || IsFormSubmit(entry)) |
| 140 return; | 142 return; |
| 141 | 143 |
| 142 // Autogenerate a keyword for the autodetected case; in the other cases we'll | 144 // Autogenerate a keyword for the autodetected case; in the other cases we'll |
| 143 // generate a keyword later after fetching the OSDD. | 145 // generate a keyword later after fetching the OSDD. |
| 144 base::string16 keyword; | 146 base::string16 keyword; |
| 145 if (provider_type == TemplateURLFetcher::AUTODETECTED_PROVIDER) { | 147 if (provider_type == TemplateURLFetcher::AUTODETECTED_PROVIDER) { |
| 146 keyword = GenerateKeywordFromNavigationEntry(entry); | 148 std::string accept_languages; |
| 149 if (TemplateURLService* url_service = | |
| 150 TemplateURLServiceFactory::GetForProfile(profile)) { | |
|
Peter Kasting
2015/07/20 19:30:51
Don't put a statement with side effects in a condi
alshabalin
2015/07/21 08:44:19
Done.
Although I somewhat disagree. In my brain a
| |
| 151 accept_languages = | |
| 152 url_service->search_terms_data().GetAcceptedLanguages(); | |
| 153 } | |
| 154 keyword = GenerateKeywordFromNavigationEntry(entry, accept_languages); | |
| 147 if (keyword.empty()) | 155 if (keyword.empty()) |
| 148 return; | 156 return; |
| 149 } | 157 } |
| 150 | 158 |
| 151 // Download the OpenSearch description document. If this is successful, a | 159 // Download the OpenSearch description document. If this is successful, a |
| 152 // new keyword will be created when done. | 160 // new keyword will be created when done. |
| 153 TemplateURLFetcherFactory::GetForProfile(profile)->ScheduleDownload( | 161 TemplateURLFetcherFactory::GetForProfile(profile)->ScheduleDownload( |
| 154 keyword, osdd_url, entry->GetFavicon().url, | 162 keyword, osdd_url, entry->GetFavicon().url, |
| 155 base::Bind(&AssociateURLFetcherWithWebContents, web_contents()), | 163 base::Bind(&AssociateURLFetcherWithWebContents, web_contents()), |
| 156 base::Bind(&SearchEngineTabHelper::OnDownloadedOSDD, | 164 base::Bind(&SearchEngineTabHelper::OnDownloadedOSDD, |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 177 | 185 |
| 178 const NavigationController& controller = web_contents()->GetController(); | 186 const NavigationController& controller = web_contents()->GetController(); |
| 179 int last_index = controller.GetLastCommittedEntryIndex(); | 187 int last_index = controller.GetLastCommittedEntryIndex(); |
| 180 // When there was no previous page, the last index will be 0. This is | 188 // When there was no previous page, the last index will be 0. This is |
| 181 // normally due to a form submit that opened in a new tab. | 189 // normally due to a form submit that opened in a new tab. |
| 182 // TODO(brettw) bug 916126: we should support keywords when form submits | 190 // TODO(brettw) bug 916126: we should support keywords when form submits |
| 183 // happen in new tabs. | 191 // happen in new tabs. |
| 184 if (last_index <= 0) | 192 if (last_index <= 0) |
| 185 return; | 193 return; |
| 186 | 194 |
| 187 base::string16 keyword(GenerateKeywordFromNavigationEntry( | |
| 188 controller.GetEntryAtIndex(last_index - 1))); | |
| 189 if (keyword.empty()) | |
| 190 return; | |
| 191 | |
| 192 TemplateURLService* url_service = | 195 TemplateURLService* url_service = |
| 193 TemplateURLServiceFactory::GetForProfile(profile); | 196 TemplateURLServiceFactory::GetForProfile(profile); |
| 194 if (!url_service) | 197 if (!url_service) |
| 195 return; | 198 return; |
| 196 | 199 |
| 200 base::string16 keyword(GenerateKeywordFromNavigationEntry( | |
| 201 controller.GetEntryAtIndex(last_index - 1), | |
| 202 url_service->search_terms_data().GetAcceptedLanguages())); | |
|
Peter Kasting
2015/07/20 19:30:51
Again, get the languages off the profile prefs dir
| |
| 203 if (keyword.empty()) | |
| 204 return; | |
| 205 | |
| 197 if (!url_service->loaded()) { | 206 if (!url_service->loaded()) { |
| 198 url_service->Load(); | 207 url_service->Load(); |
| 199 return; | 208 return; |
| 200 } | 209 } |
| 201 | 210 |
| 202 TemplateURL* current_url; | 211 TemplateURL* current_url; |
| 203 GURL url = params.searchable_form_url; | 212 GURL url = params.searchable_form_url; |
| 204 if (!url_service->CanAddAutogeneratedKeyword(keyword, url, ¤t_url)) | 213 if (!url_service->CanAddAutogeneratedKeyword(keyword, url, ¤t_url)) |
| 205 return; | 214 return; |
| 206 | 215 |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 224 // the favicon url wasn't obtained before the load started. This assumes the | 233 // the favicon url wasn't obtained before the load started. This assumes the |
| 225 // latter. | 234 // latter. |
| 226 // TODO(sky): Need a way to set the favicon that doesn't involve generating | 235 // TODO(sky): Need a way to set the favicon that doesn't involve generating |
| 227 // its url. | 236 // its url. |
| 228 data.favicon_url = current_favicon.is_valid() ? | 237 data.favicon_url = current_favicon.is_valid() ? |
| 229 current_favicon : TemplateURL::GenerateFaviconURL(params.referrer.url); | 238 current_favicon : TemplateURL::GenerateFaviconURL(params.referrer.url); |
| 230 data.safe_for_autoreplace = true; | 239 data.safe_for_autoreplace = true; |
| 231 data.input_encodings.push_back(params.searchable_form_encoding); | 240 data.input_encodings.push_back(params.searchable_form_encoding); |
| 232 url_service->Add(new TemplateURL(data)); | 241 url_service->Add(new TemplateURL(data)); |
| 233 } | 242 } |
| OLD | NEW |