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" | |
13 #include "chrome/common/render_messages.h" | 12 #include "chrome/common/render_messages.h" |
14 #include "chrome/common/url_constants.h" | 13 #include "chrome/common/url_constants.h" |
15 #include "components/search_engines/template_url.h" | 14 #include "components/search_engines/template_url.h" |
16 #include "components/search_engines/template_url_fetcher.h" | 15 #include "components/search_engines/template_url_fetcher.h" |
17 #include "components/search_engines/template_url_service.h" | 16 #include "components/search_engines/template_url_service.h" |
18 #include "content/public/browser/favicon_status.h" | 17 #include "content/public/browser/favicon_status.h" |
19 #include "content/public/browser/navigation_controller.h" | 18 #include "content/public/browser/navigation_controller.h" |
20 #include "content/public/browser/navigation_entry.h" | 19 #include "content/public/browser/navigation_entry.h" |
21 #include "content/public/browser/render_frame_host.h" | 20 #include "content/public/browser/render_frame_host.h" |
22 #include "content/public/browser/render_process_host.h" | 21 #include "content/public/browser/render_process_host.h" |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 return handled; | 98 return handled; |
100 } | 99 } |
101 | 100 |
102 bool SearchEngineTabHelper::OnMessageReceived( | 101 bool SearchEngineTabHelper::OnMessageReceived( |
103 const IPC::Message& message, | 102 const IPC::Message& message, |
104 content::RenderFrameHost* render_frame_host) { | 103 content::RenderFrameHost* render_frame_host) { |
105 return OnMessageReceived(message); | 104 return OnMessageReceived(message); |
106 } | 105 } |
107 | 106 |
108 SearchEngineTabHelper::SearchEngineTabHelper(WebContents* web_contents) | 107 SearchEngineTabHelper::SearchEngineTabHelper(WebContents* web_contents) |
109 : content::WebContentsObserver(web_contents), | 108 : content::WebContentsObserver(web_contents) { |
110 delegate_(nullptr), | |
111 weak_ptr_factory_(this) { | |
112 DCHECK(web_contents); | 109 DCHECK(web_contents); |
113 } | 110 } |
114 | 111 |
115 void SearchEngineTabHelper::OnPageHasOSDD( | 112 void SearchEngineTabHelper::OnPageHasOSDD( |
116 const GURL& page_url, | 113 const GURL& page_url, |
117 const GURL& osdd_url, | 114 const GURL& osdd_url) { |
118 const search_provider::OSDDType& msg_provider_type) { | |
119 // Checks to see if we should generate a keyword based on the OSDD, and if | 115 // Checks to see if we should generate a keyword based on the OSDD, and if |
120 // necessary uses TemplateURLFetcher to download the OSDD and create a | 116 // necessary uses TemplateURLFetcher to download the OSDD and create a |
121 // keyword. | 117 // keyword. |
122 | 118 |
123 TemplateURLFetcher::ProviderType provider_type = | |
124 (msg_provider_type == search_provider::AUTODETECTED_PROVIDER) | |
125 ? TemplateURLFetcher::AUTODETECTED_PROVIDER | |
126 : TemplateURLFetcher::EXPLICIT_PROVIDER; | |
127 | |
128 if (provider_type == TemplateURLFetcher::EXPLICIT_PROVIDER) { | |
129 UMA_HISTOGRAM_ENUMERATION( | |
130 "Search.AddSearchProvider2", | |
131 EditSearchEngineController::ADD_SEARCH_PROVIDER_CALLED, | |
132 EditSearchEngineController::NUM_EDIT_SEARCH_ENGINE_ACTIONS); | |
133 } | |
134 | |
135 // Make sure that the page is the current page and other basic checks. | 119 // Make sure that the page is the current page and other basic checks. |
136 // When |page_url| has file: scheme, this method doesn't work because of | 120 // When |page_url| has file: scheme, this method doesn't work because of |
137 // http://b/issue?id=863583. For that reason, this doesn't check and allow | 121 // http://b/issue?id=863583. For that reason, this doesn't check and allow |
138 // urls referring to osdd urls with same schemes. | 122 // urls referring to osdd urls with same schemes. |
139 if (!osdd_url.is_valid() || !osdd_url.SchemeIsHTTPOrHTTPS()) | 123 if (!osdd_url.is_valid() || !osdd_url.SchemeIsHTTPOrHTTPS()) |
140 return; | 124 return; |
141 | 125 |
142 Profile* profile = | 126 Profile* profile = |
143 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | 127 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
144 if (page_url != web_contents()->GetLastCommittedURL() || | 128 if (page_url != web_contents()->GetLastCommittedURL() || |
145 !TemplateURLFetcherFactory::GetForProfile(profile) || | 129 !TemplateURLFetcherFactory::GetForProfile(profile) || |
146 profile->IsOffTheRecord()) | 130 profile->IsOffTheRecord()) |
147 return; | 131 return; |
148 | 132 |
149 // If the current page is a form submit, find the last page that was not a | 133 // If the current page is a form submit, find the last page that was not a |
150 // form submit and use its url to generate the keyword from. | 134 // form submit and use its url to generate the keyword from. |
151 const NavigationController& controller = web_contents()->GetController(); | 135 const NavigationController& controller = web_contents()->GetController(); |
152 const NavigationEntry* entry = controller.GetLastCommittedEntry(); | 136 const NavigationEntry* entry = controller.GetLastCommittedEntry(); |
153 for (int index = controller.GetLastCommittedEntryIndex(); | 137 for (int index = controller.GetLastCommittedEntryIndex(); |
154 (index > 0) && IsFormSubmit(entry); | 138 (index > 0) && IsFormSubmit(entry); |
155 entry = controller.GetEntryAtIndex(index)) | 139 entry = controller.GetEntryAtIndex(index)) |
156 --index; | 140 --index; |
157 if (!entry || IsFormSubmit(entry)) | 141 if (!entry || IsFormSubmit(entry)) |
158 return; | 142 return; |
159 | 143 |
160 // 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 |
161 // generate a keyword later after fetching the OSDD. | 145 // generate a keyword later after fetching the OSDD. |
162 base::string16 keyword; | 146 base::string16 keyword = GenerateKeywordFromNavigationEntry(entry); |
163 if (provider_type == TemplateURLFetcher::AUTODETECTED_PROVIDER) { | 147 if (keyword.empty()) |
164 keyword = GenerateKeywordFromNavigationEntry(entry); | 148 return; |
165 if (keyword.empty()) | |
166 return; | |
167 } | |
168 | 149 |
169 // Download the OpenSearch description document. If this is successful, a | 150 // Download the OpenSearch description document. If this is successful, a |
170 // new keyword will be created when done. | 151 // new keyword will be created when done. |
171 TemplateURLFetcherFactory::GetForProfile(profile)->ScheduleDownload( | 152 TemplateURLFetcherFactory::GetForProfile(profile)->ScheduleDownload( |
172 keyword, osdd_url, entry->GetFavicon().url, | 153 keyword, osdd_url, entry->GetFavicon().url, |
173 base::Bind(&AssociateURLFetcherWithWebContents, web_contents()), | 154 base::Bind(&AssociateURLFetcherWithWebContents, web_contents())); |
174 base::Bind(&SearchEngineTabHelper::OnDownloadedOSDD, | |
175 weak_ptr_factory_.GetWeakPtr()), | |
176 provider_type); | |
177 } | |
178 | |
179 void SearchEngineTabHelper::OnDownloadedOSDD( | |
180 std::unique_ptr<TemplateURL> template_url) { | |
181 Profile* profile = | |
182 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | |
183 delegate_->ConfirmAddSearchProvider(template_url.release(), profile); | |
184 } | 155 } |
185 | 156 |
186 void SearchEngineTabHelper::GenerateKeywordIfNecessary( | 157 void SearchEngineTabHelper::GenerateKeywordIfNecessary( |
187 const content::FrameNavigateParams& params) { | 158 const content::FrameNavigateParams& params) { |
188 if (!params.searchable_form_url.is_valid()) | 159 if (!params.searchable_form_url.is_valid()) |
189 return; | 160 return; |
190 | 161 |
191 Profile* profile = | 162 Profile* profile = |
192 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | 163 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
193 if (profile->IsOffTheRecord()) | 164 if (profile->IsOffTheRecord()) |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 // the favicon url wasn't obtained before the load started. This assumes the | 213 // the favicon url wasn't obtained before the load started. This assumes the |
243 // latter. | 214 // latter. |
244 // TODO(sky): Need a way to set the favicon that doesn't involve generating | 215 // TODO(sky): Need a way to set the favicon that doesn't involve generating |
245 // its url. | 216 // its url. |
246 data.favicon_url = current_favicon.is_valid() ? | 217 data.favicon_url = current_favicon.is_valid() ? |
247 current_favicon : TemplateURL::GenerateFaviconURL(params.referrer.url); | 218 current_favicon : TemplateURL::GenerateFaviconURL(params.referrer.url); |
248 data.safe_for_autoreplace = true; | 219 data.safe_for_autoreplace = true; |
249 data.input_encodings.push_back(params.searchable_form_encoding); | 220 data.input_encodings.push_back(params.searchable_form_encoding); |
250 url_service->Add(new TemplateURL(data)); | 221 url_service->Add(new TemplateURL(data)); |
251 } | 222 } |
OLD | NEW |