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 template_url_service->GetTemplateURLs(); |
| 48 TemplateURL* original_default_provider = |
| 49 template_url_service->GetDefaultSearchProvider(); |
| 50 for (TemplateURLService::TemplateURLVector::const_iterator it = |
| 51 original_template_urls.begin(); |
| 52 it != original_template_urls.end(); |
| 53 ++it) { |
| 54 template_url_copies.push_back(new TemplateURL(NULL, (*it)->data())); |
| 55 if (*it == original_default_provider) |
| 56 default_provider_copy = template_url_copies.back(); |
| 57 } |
| 58 BrowserThread::PostTask(BrowserThread::IO, |
| 59 FROM_HERE, |
| 60 base::Bind(callback, |
| 61 base::Passed(template_url_copies.Pass()), |
| 62 base::Unretained(default_provider_copy))); |
| 63 } |
| 64 |
39 // Implementation of SearchTermsData that may be used on the I/O thread. | 65 // Implementation of SearchTermsData that may be used on the I/O thread. |
40 class IOThreadSearchTermsData : public SearchTermsData { | 66 class IOThreadSearchTermsData : public SearchTermsData { |
41 public: | 67 public: |
42 explicit IOThreadSearchTermsData(const std::string& google_base_url); | 68 explicit IOThreadSearchTermsData(const std::string& google_base_url); |
43 | 69 |
44 // Implementation of SearchTermsData. | 70 // Implementation of SearchTermsData. |
45 virtual std::string GoogleBaseURLValue() const OVERRIDE; | 71 virtual std::string GoogleBaseURLValue() const OVERRIDE; |
46 | 72 |
47 private: | 73 private: |
48 std::string google_base_url_; | 74 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()); | 184 DCHECK(requested_origin == requested_origin.GetOrigin()); |
159 DCHECK(template_url->GetType() != TemplateURL::OMNIBOX_API_EXTENSION); | 185 DCHECK(template_url->GetType() != TemplateURL::OMNIBOX_API_EXTENSION); |
160 return requested_origin == | 186 return requested_origin == |
161 TemplateURLService::GenerateSearchURLUsingTermsData(template_url, | 187 TemplateURLService::GenerateSearchURLUsingTermsData(template_url, |
162 search_terms_data).GetOrigin(); | 188 search_terms_data).GetOrigin(); |
163 } | 189 } |
164 | 190 |
165 } // namespace | 191 } // namespace |
166 | 192 |
167 SearchProviderInstallData::SearchProviderInstallData( | 193 SearchProviderInstallData::SearchProviderInstallData( |
168 Profile* profile, content::RenderProcessHost* host) | 194 Profile* profile, |
169 : web_service_(WebDataService::FromBrowserContext(profile)), | 195 content::RenderProcessHost* host) |
170 load_handle_(0), | 196 : template_url_service_(TemplateURLServiceFactory::GetForProfile(profile)), |
171 google_base_url_(UIThreadSearchTermsData(profile).GoogleBaseURLValue()), | 197 google_base_url_(UIThreadSearchTermsData(profile).GoogleBaseURLValue()), |
172 weak_factory_(this) { | 198 weak_factory_(this) { |
173 // GoogleURLObserver is responsible for killing itself when | 199 // GoogleURLObserver is responsible for killing itself when |
174 // the given notification occurs. | 200 // the given notification occurs. |
175 new GoogleURLObserver(profile, | 201 new GoogleURLObserver(profile, |
176 new GoogleURLChangeNotifier(weak_factory_.GetWeakPtr()), | 202 new GoogleURLChangeNotifier(weak_factory_.GetWeakPtr()), |
177 host); | 203 host); |
178 } | 204 } |
179 | 205 |
180 SearchProviderInstallData::~SearchProviderInstallData() { | 206 SearchProviderInstallData::~SearchProviderInstallData() { |
181 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 207 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
182 | |
183 if (load_handle_) { | |
184 DCHECK(web_service_.get()); | |
185 web_service_->CancelRequest(load_handle_); | |
186 } | |
187 } | 208 } |
188 | 209 |
189 void SearchProviderInstallData::CallWhenLoaded(const base::Closure& closure) { | 210 void SearchProviderInstallData::CallWhenLoaded(const base::Closure& closure) { |
190 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 211 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
191 | 212 |
192 if (provider_map_.get()) { | 213 if (provider_map_.get()) { |
193 closure.Run(); | 214 closure.Run(); |
194 return; | 215 return; |
195 } | 216 } |
196 | 217 |
| 218 bool do_load = closure_queue_.empty(); |
197 closure_queue_.push_back(closure); | 219 closure_queue_.push_back(closure); |
198 if (load_handle_) | 220 |
| 221 // If the queue wasn't empty, there was already a load in progress. |
| 222 if (!do_load) |
199 return; | 223 return; |
200 | 224 |
201 if (web_service_.get()) | 225 if (template_url_service_) { |
202 load_handle_ = web_service_->GetKeywords(this); | 226 BrowserThread::PostTask( |
203 else | 227 BrowserThread::UI, |
| 228 FROM_HERE, |
| 229 base::Bind(&LoadDataOnUIThread, |
| 230 template_url_service_, |
| 231 base::Bind(&SearchProviderInstallData::OnTemplateURLsLoaded, |
| 232 weak_factory_.GetWeakPtr()))); |
| 233 } else { |
204 OnLoadFailed(); | 234 OnLoadFailed(); |
| 235 } |
205 } | 236 } |
206 | 237 |
207 SearchProviderInstallData::State SearchProviderInstallData::GetInstallState( | 238 SearchProviderInstallData::State SearchProviderInstallData::GetInstallState( |
208 const GURL& requested_origin) { | 239 const GURL& requested_origin) { |
209 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 240 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
210 DCHECK(provider_map_.get()); | 241 DCHECK(provider_map_.get()); |
211 | 242 |
212 // First check to see if the origin is the default search provider. | 243 // First check to see if the origin is the default search provider. |
213 if (requested_origin.spec() == default_search_origin_) | 244 if (requested_origin.spec() == default_search_origin_) |
214 return INSTALLED_AS_DEFAULT; | 245 return INSTALLED_AS_DEFAULT; |
(...skipping 11 matching lines...) Expand all Loading... |
226 return INSTALLED_BUT_NOT_DEFAULT; | 257 return INSTALLED_BUT_NOT_DEFAULT; |
227 } | 258 } |
228 return NOT_INSTALLED; | 259 return NOT_INSTALLED; |
229 } | 260 } |
230 | 261 |
231 void SearchProviderInstallData::OnGoogleURLChange( | 262 void SearchProviderInstallData::OnGoogleURLChange( |
232 const std::string& google_base_url) { | 263 const std::string& google_base_url) { |
233 google_base_url_ = google_base_url; | 264 google_base_url_ = google_base_url; |
234 } | 265 } |
235 | 266 |
236 void SearchProviderInstallData::OnWebDataServiceRequestDone( | 267 void SearchProviderInstallData::OnTemplateURLsLoaded( |
237 WebDataService::Handle h, | 268 ScopedVector<TemplateURL> template_urls, |
238 const WDTypedResult* result) { | 269 TemplateURL* default_provider) { |
239 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 270 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
240 | 271 |
241 // Reset the load_handle so that we don't try and cancel the load in | 272 template_urls_ = template_urls.Pass(); |
242 // the destructor. | |
243 load_handle_ = 0; | |
244 | 273 |
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_); | 274 IOThreadSearchTermsData search_terms_data(google_base_url_); |
262 provider_map_.reset(new SearchHostToURLsMap()); | 275 provider_map_.reset(new SearchHostToURLsMap()); |
263 provider_map_->Init(template_urls_.get(), search_terms_data); | 276 provider_map_->Init(template_urls_.get(), search_terms_data); |
264 SetDefault(default_search_provider); | 277 SetDefault(default_provider); |
265 NotifyLoaded(); | 278 NotifyLoaded(); |
266 } | 279 } |
267 | 280 |
268 void SearchProviderInstallData::SetDefault(const TemplateURL* template_url) { | 281 void SearchProviderInstallData::SetDefault(const TemplateURL* template_url) { |
269 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 282 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
270 | 283 |
271 if (!template_url) { | 284 if (!template_url) { |
272 default_search_origin_.clear(); | 285 default_search_origin_.clear(); |
273 return; | 286 return; |
274 } | 287 } |
(...skipping 28 matching lines...) Expand all Loading... |
303 | 316 |
304 std::for_each(closure_queue.begin(), | 317 std::for_each(closure_queue.begin(), |
305 closure_queue.end(), | 318 closure_queue.end(), |
306 std::mem_fun_ref(&base::Closure::Run)); | 319 std::mem_fun_ref(&base::Closure::Run)); |
307 | 320 |
308 // Since we expect this request to be rare, clear out the information. This | 321 // Since we expect this request to be rare, clear out the information. This |
309 // also keeps the responses current as the search providers change. | 322 // also keeps the responses current as the search providers change. |
310 provider_map_.reset(); | 323 provider_map_.reset(); |
311 SetDefault(NULL); | 324 SetDefault(NULL); |
312 } | 325 } |
OLD | NEW |