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/search_engines/search_provider_install_data.h" | 5 #include "chrome/browser/search_engines/search_provider_install_data.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <functional> | 8 #include <functional> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/bind_helpers.h" |
13 #include "base/logging.h" | 14 #include "base/logging.h" |
14 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
15 #include "base/sequenced_task_runner_helpers.h" | 16 #include "base/sequenced_task_runner_helpers.h" |
16 #include "chrome/browser/chrome_notification_types.h" | 17 #include "chrome/browser/chrome_notification_types.h" |
17 #include "chrome/browser/google/google_url_tracker.h" | 18 #include "chrome/browser/google/google_url_tracker.h" |
18 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
19 #include "chrome/browser/search_engines/search_host_to_urls_map.h" | 20 #include "chrome/browser/search_engines/search_host_to_urls_map.h" |
20 #include "chrome/browser/search_engines/search_terms_data.h" | 21 #include "chrome/browser/search_engines/search_terms_data.h" |
21 #include "chrome/browser/search_engines/template_url.h" | 22 #include "chrome/browser/search_engines/template_url.h" |
22 #include "chrome/browser/search_engines/template_url_service.h" | 23 #include "chrome/browser/search_engines/template_url_service.h" |
| 24 #include "chrome/browser/search_engines/template_url_service_factory.h" |
23 #include "chrome/browser/search_engines/util.h" | 25 #include "chrome/browser/search_engines/util.h" |
24 #include "chrome/browser/webdata/web_data_service.h" | 26 #include "chrome/browser/webdata/web_data_service.h" |
25 #include "content/public/browser/browser_thread.h" | 27 #include "content/public/browser/browser_thread.h" |
26 #include "content/public/browser/notification_observer.h" | 28 #include "content/public/browser/notification_observer.h" |
27 #include "content/public/browser/notification_registrar.h" | 29 #include "content/public/browser/notification_registrar.h" |
28 #include "content/public/browser/notification_service.h" | 30 #include "content/public/browser/notification_service.h" |
29 #include "content/public/browser/notification_source.h" | 31 #include "content/public/browser/notification_source.h" |
30 #include "content/public/browser/render_process_host.h" | 32 #include "content/public/browser/render_process_host.h" |
31 #include "content/public/browser/render_process_host_observer.h" | 33 #include "content/public/browser/render_process_host_observer.h" |
32 | 34 |
33 using content::BrowserThread; | 35 using content::BrowserThread; |
34 | 36 |
35 typedef SearchHostToURLsMap::TemplateURLSet TemplateURLSet; | 37 typedef SearchHostToURLsMap::TemplateURLSet TemplateURLSet; |
36 | 38 |
37 namespace { | 39 namespace { |
38 | 40 |
| 41 void LoadDataOnUIThread(TemplateURLService* template_url_service, |
| 42 const base::Callback<void(ScopedVector<TemplateURL>, |
| 43 TemplateURL*)>& callback) { |
| 44 ScopedVector<TemplateURL> template_url_copies; |
| 45 TemplateURL* default_provider_copy = NULL; |
| 46 TemplateURLService::TemplateURLVector original_template_urls; |
| 47 TemplateURL* original_default_provider = |
| 48 template_url_service->GetDefaultSearchProvider(); |
| 49 for (TemplateURLService::TemplateURLVector::const_iterator it = |
| 50 original_template_urls.begin(); |
| 51 it != original_template_urls.end(); |
| 52 ++it) { |
| 53 template_url_copies.push_back(new TemplateURL(NULL, (*it)->data())); |
| 54 if (*it == original_default_provider) |
| 55 default_provider_copy = template_url_copies.back(); |
| 56 } |
| 57 BrowserThread::PostTask(BrowserThread::IO, |
| 58 FROM_HERE, |
| 59 base::Bind(callback, |
| 60 base::Passed(template_url_copies.Pass()), |
| 61 base::Unretained(default_provider_copy))); |
| 62 } |
| 63 |
39 // Implementation of SearchTermsData that may be used on the I/O thread. | 64 // Implementation of SearchTermsData that may be used on the I/O thread. |
40 class IOThreadSearchTermsData : public SearchTermsData { | 65 class IOThreadSearchTermsData : public SearchTermsData { |
41 public: | 66 public: |
42 explicit IOThreadSearchTermsData(const std::string& google_base_url); | 67 explicit IOThreadSearchTermsData(const std::string& google_base_url); |
43 | 68 |
44 // Implementation of SearchTermsData. | 69 // Implementation of SearchTermsData. |
45 virtual std::string GoogleBaseURLValue() const OVERRIDE; | 70 virtual std::string GoogleBaseURLValue() const OVERRIDE; |
46 | 71 |
47 private: | 72 private: |
48 std::string google_base_url_; | 73 std::string google_base_url_; |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 DCHECK(requested_origin == requested_origin.GetOrigin()); | 183 DCHECK(requested_origin == requested_origin.GetOrigin()); |
159 DCHECK(template_url->GetType() != TemplateURL::OMNIBOX_API_EXTENSION); | 184 DCHECK(template_url->GetType() != TemplateURL::OMNIBOX_API_EXTENSION); |
160 return requested_origin == | 185 return requested_origin == |
161 TemplateURLService::GenerateSearchURLUsingTermsData(template_url, | 186 TemplateURLService::GenerateSearchURLUsingTermsData(template_url, |
162 search_terms_data).GetOrigin(); | 187 search_terms_data).GetOrigin(); |
163 } | 188 } |
164 | 189 |
165 } // namespace | 190 } // namespace |
166 | 191 |
167 SearchProviderInstallData::SearchProviderInstallData( | 192 SearchProviderInstallData::SearchProviderInstallData( |
168 Profile* profile, content::RenderProcessHost* host) | 193 Profile* profile, |
169 : web_service_(WebDataService::FromBrowserContext(profile)), | 194 content::RenderProcessHost* host) |
170 load_handle_(0), | 195 : template_url_service_(TemplateURLServiceFactory::GetForProfile(profile)), |
171 google_base_url_(UIThreadSearchTermsData(profile).GoogleBaseURLValue()), | 196 google_base_url_(UIThreadSearchTermsData(profile).GoogleBaseURLValue()), |
172 weak_factory_(this) { | 197 weak_factory_(this) { |
173 // GoogleURLObserver is responsible for killing itself when | 198 // GoogleURLObserver is responsible for killing itself when |
174 // the given notification occurs. | 199 // the given notification occurs. |
175 new GoogleURLObserver(profile, | 200 new GoogleURLObserver(profile, |
176 new GoogleURLChangeNotifier(weak_factory_.GetWeakPtr()), | 201 new GoogleURLChangeNotifier(weak_factory_.GetWeakPtr()), |
177 host); | 202 host); |
178 } | 203 } |
179 | 204 |
180 SearchProviderInstallData::~SearchProviderInstallData() { | 205 SearchProviderInstallData::~SearchProviderInstallData() { |
181 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 206 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
182 | |
183 if (load_handle_) { | |
184 DCHECK(web_service_.get()); | |
185 web_service_->CancelRequest(load_handle_); | |
186 } | |
187 } | 207 } |
188 | 208 |
189 void SearchProviderInstallData::CallWhenLoaded(const base::Closure& closure) { | 209 void SearchProviderInstallData::CallWhenLoaded(const base::Closure& closure) { |
190 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 210 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
191 | 211 |
192 if (provider_map_.get()) { | 212 if (provider_map_.get()) { |
193 closure.Run(); | 213 closure.Run(); |
194 return; | 214 return; |
195 } | 215 } |
196 | 216 |
| 217 bool do_load = closure_queue_.empty(); |
197 closure_queue_.push_back(closure); | 218 closure_queue_.push_back(closure); |
198 if (load_handle_) | 219 |
| 220 // If the queue wasn't empty, there was already a load in progress. |
| 221 if (!do_load) |
199 return; | 222 return; |
200 | 223 |
201 if (web_service_.get()) | 224 if (template_url_service_) { |
202 load_handle_ = web_service_->GetKeywords(this); | 225 BrowserThread::PostTask( |
203 else | 226 BrowserThread::UI, |
| 227 FROM_HERE, |
| 228 base::Bind(&LoadDataOnUIThread, |
| 229 template_url_service_, |
| 230 base::Bind(&SearchProviderInstallData::OnTemplateURLsLoaded, |
| 231 weak_factory_.GetWeakPtr()))); |
| 232 } else { |
204 OnLoadFailed(); | 233 OnLoadFailed(); |
| 234 } |
205 } | 235 } |
206 | 236 |
207 SearchProviderInstallData::State SearchProviderInstallData::GetInstallState( | 237 SearchProviderInstallData::State SearchProviderInstallData::GetInstallState( |
208 const GURL& requested_origin) { | 238 const GURL& requested_origin) { |
209 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 239 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
210 DCHECK(provider_map_.get()); | 240 DCHECK(provider_map_.get()); |
211 | 241 |
212 // First check to see if the origin is the default search provider. | 242 // First check to see if the origin is the default search provider. |
213 if (requested_origin.spec() == default_search_origin_) | 243 if (requested_origin.spec() == default_search_origin_) |
214 return INSTALLED_AS_DEFAULT; | 244 return INSTALLED_AS_DEFAULT; |
(...skipping 11 matching lines...) Expand all Loading... |
226 return INSTALLED_BUT_NOT_DEFAULT; | 256 return INSTALLED_BUT_NOT_DEFAULT; |
227 } | 257 } |
228 return NOT_INSTALLED; | 258 return NOT_INSTALLED; |
229 } | 259 } |
230 | 260 |
231 void SearchProviderInstallData::OnGoogleURLChange( | 261 void SearchProviderInstallData::OnGoogleURLChange( |
232 const std::string& google_base_url) { | 262 const std::string& google_base_url) { |
233 google_base_url_ = google_base_url; | 263 google_base_url_ = google_base_url; |
234 } | 264 } |
235 | 265 |
236 void SearchProviderInstallData::OnWebDataServiceRequestDone( | 266 void SearchProviderInstallData::OnTemplateURLsLoaded( |
237 WebDataService::Handle h, | 267 ScopedVector<TemplateURL> template_urls, |
238 const WDTypedResult* result) { | 268 TemplateURL* default_provider) { |
239 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 269 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
240 | 270 |
241 // Reset the load_handle so that we don't try and cancel the load in | 271 template_urls_ = template_urls.Pass(); |
242 // the destructor. | |
243 load_handle_ = 0; | |
244 | 272 |
245 if (!result) { | |
246 // Results are null if the database went away or (most likely) wasn't | |
247 // loaded. | |
248 OnLoadFailed(); | |
249 return; | |
250 } | |
251 | |
252 TemplateURL* default_search_provider = NULL; | |
253 int new_resource_keyword_version = 0; | |
254 std::vector<TemplateURL*> extracted_template_urls; | |
255 GetSearchProvidersUsingKeywordResult(*result, NULL, NULL, | |
256 &extracted_template_urls, &default_search_provider, | |
257 &new_resource_keyword_version, NULL); | |
258 template_urls_.get().insert(template_urls_.get().begin(), | |
259 extracted_template_urls.begin(), | |
260 extracted_template_urls.end()); | |
261 IOThreadSearchTermsData search_terms_data(google_base_url_); | 273 IOThreadSearchTermsData search_terms_data(google_base_url_); |
262 provider_map_.reset(new SearchHostToURLsMap()); | 274 provider_map_.reset(new SearchHostToURLsMap()); |
263 provider_map_->Init(template_urls_.get(), search_terms_data); | 275 provider_map_->Init(template_urls_.get(), search_terms_data); |
264 SetDefault(default_search_provider); | 276 SetDefault(default_provider); |
265 NotifyLoaded(); | 277 NotifyLoaded(); |
266 } | 278 } |
267 | 279 |
268 void SearchProviderInstallData::SetDefault(const TemplateURL* template_url) { | 280 void SearchProviderInstallData::SetDefault(const TemplateURL* template_url) { |
269 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 281 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
270 | 282 |
271 if (!template_url) { | 283 if (!template_url) { |
272 default_search_origin_.clear(); | 284 default_search_origin_.clear(); |
273 return; | 285 return; |
274 } | 286 } |
(...skipping 28 matching lines...) Expand all Loading... |
303 | 315 |
304 std::for_each(closure_queue.begin(), | 316 std::for_each(closure_queue.begin(), |
305 closure_queue.end(), | 317 closure_queue.end(), |
306 std::mem_fun_ref(&base::Closure::Run)); | 318 std::mem_fun_ref(&base::Closure::Run)); |
307 | 319 |
308 // Since we expect this request to be rare, clear out the information. This | 320 // Since we expect this request to be rare, clear out the information. This |
309 // also keeps the responses current as the search providers change. | 321 // also keeps the responses current as the search providers change. |
310 provider_map_.reset(); | 322 provider_map_.reset(); |
311 SetDefault(NULL); | 323 SetDefault(NULL); |
312 } | 324 } |
OLD | NEW |