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