| 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 |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 | 100 |
| 101 DISALLOW_COPY_AND_ASSIGN(GoogleURLChangeNotifier); | 101 DISALLOW_COPY_AND_ASSIGN(GoogleURLChangeNotifier); |
| 102 }; | 102 }; |
| 103 | 103 |
| 104 GoogleURLChangeNotifier::GoogleURLChangeNotifier( | 104 GoogleURLChangeNotifier::GoogleURLChangeNotifier( |
| 105 const base::WeakPtr<SearchProviderInstallData>& install_data) | 105 const base::WeakPtr<SearchProviderInstallData>& install_data) |
| 106 : install_data_(install_data) { | 106 : install_data_(install_data) { |
| 107 } | 107 } |
| 108 | 108 |
| 109 void GoogleURLChangeNotifier::OnChange(const std::string& google_base_url) { | 109 void GoogleURLChangeNotifier::OnChange(const std::string& google_base_url) { |
| 110 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 110 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 111 if (install_data_.get()) | 111 if (install_data_.get()) |
| 112 install_data_->OnGoogleURLChange(google_base_url); | 112 install_data_->OnGoogleURLChange(google_base_url); |
| 113 } | 113 } |
| 114 | 114 |
| 115 // Notices changes in the Google base URL and sends them along | 115 // Notices changes in the Google base URL and sends them along |
| 116 // to the SearchProviderInstallData on the I/O thread. | 116 // to the SearchProviderInstallData on the I/O thread. |
| 117 class GoogleURLObserver : public content::RenderProcessHostObserver { | 117 class GoogleURLObserver : public content::RenderProcessHostObserver { |
| 118 public: | 118 public: |
| 119 GoogleURLObserver(GoogleURLTracker* google_url_tracker, | 119 GoogleURLObserver(GoogleURLTracker* google_url_tracker, |
| 120 GoogleURLChangeNotifier* change_notifier, | 120 GoogleURLChangeNotifier* change_notifier, |
| (...skipping 15 matching lines...) Expand all Loading... |
| 136 | 136 |
| 137 DISALLOW_COPY_AND_ASSIGN(GoogleURLObserver); | 137 DISALLOW_COPY_AND_ASSIGN(GoogleURLObserver); |
| 138 }; | 138 }; |
| 139 | 139 |
| 140 GoogleURLObserver::GoogleURLObserver( | 140 GoogleURLObserver::GoogleURLObserver( |
| 141 GoogleURLTracker* google_url_tracker, | 141 GoogleURLTracker* google_url_tracker, |
| 142 GoogleURLChangeNotifier* change_notifier, | 142 GoogleURLChangeNotifier* change_notifier, |
| 143 content::RenderProcessHost* host) | 143 content::RenderProcessHost* host) |
| 144 : google_url_tracker_(google_url_tracker), | 144 : google_url_tracker_(google_url_tracker), |
| 145 change_notifier_(change_notifier) { | 145 change_notifier_(change_notifier) { |
| 146 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 146 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 147 google_url_updated_subscription_ = | 147 google_url_updated_subscription_ = |
| 148 google_url_tracker_->RegisterCallback(base::Bind( | 148 google_url_tracker_->RegisterCallback(base::Bind( |
| 149 &GoogleURLObserver::OnGoogleURLUpdated, base::Unretained(this))); | 149 &GoogleURLObserver::OnGoogleURLUpdated, base::Unretained(this))); |
| 150 host->AddObserver(this); | 150 host->AddObserver(this); |
| 151 } | 151 } |
| 152 | 152 |
| 153 void GoogleURLObserver::OnGoogleURLUpdated() { | 153 void GoogleURLObserver::OnGoogleURLUpdated() { |
| 154 BrowserThread::PostTask(BrowserThread::IO, | 154 BrowserThread::PostTask(BrowserThread::IO, |
| 155 FROM_HERE, | 155 FROM_HERE, |
| 156 base::Bind(&GoogleURLChangeNotifier::OnChange, | 156 base::Bind(&GoogleURLChangeNotifier::OnChange, |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 // GoogleURLObserver is responsible for killing itself when | 190 // GoogleURLObserver is responsible for killing itself when |
| 191 // the given notification occurs. | 191 // the given notification occurs. |
| 192 new GoogleURLObserver( | 192 new GoogleURLObserver( |
| 193 google_url_tracker, | 193 google_url_tracker, |
| 194 new GoogleURLChangeNotifier(weak_factory_.GetWeakPtr()), | 194 new GoogleURLChangeNotifier(weak_factory_.GetWeakPtr()), |
| 195 host); | 195 host); |
| 196 } | 196 } |
| 197 } | 197 } |
| 198 | 198 |
| 199 SearchProviderInstallData::~SearchProviderInstallData() { | 199 SearchProviderInstallData::~SearchProviderInstallData() { |
| 200 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 200 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 201 } | 201 } |
| 202 | 202 |
| 203 void SearchProviderInstallData::CallWhenLoaded(const base::Closure& closure) { | 203 void SearchProviderInstallData::CallWhenLoaded(const base::Closure& closure) { |
| 204 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 204 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 205 | 205 |
| 206 if (provider_map_.get()) { | 206 if (provider_map_.get()) { |
| 207 closure.Run(); | 207 closure.Run(); |
| 208 return; | 208 return; |
| 209 } | 209 } |
| 210 | 210 |
| 211 bool do_load = closure_queue_.empty(); | 211 bool do_load = closure_queue_.empty(); |
| 212 closure_queue_.push_back(closure); | 212 closure_queue_.push_back(closure); |
| 213 | 213 |
| 214 // If the queue wasn't empty, there was already a load in progress. | 214 // If the queue wasn't empty, there was already a load in progress. |
| 215 if (!do_load) | 215 if (!do_load) |
| 216 return; | 216 return; |
| 217 | 217 |
| 218 if (template_url_service_) { | 218 if (template_url_service_) { |
| 219 BrowserThread::PostTask( | 219 BrowserThread::PostTask( |
| 220 BrowserThread::UI, | 220 BrowserThread::UI, |
| 221 FROM_HERE, | 221 FROM_HERE, |
| 222 base::Bind(&LoadDataOnUIThread, | 222 base::Bind(&LoadDataOnUIThread, |
| 223 template_url_service_, | 223 template_url_service_, |
| 224 base::Bind(&SearchProviderInstallData::OnTemplateURLsLoaded, | 224 base::Bind(&SearchProviderInstallData::OnTemplateURLsLoaded, |
| 225 weak_factory_.GetWeakPtr()))); | 225 weak_factory_.GetWeakPtr()))); |
| 226 } else { | 226 } else { |
| 227 OnLoadFailed(); | 227 OnLoadFailed(); |
| 228 } | 228 } |
| 229 } | 229 } |
| 230 | 230 |
| 231 SearchProviderInstallData::State SearchProviderInstallData::GetInstallState( | 231 SearchProviderInstallData::State SearchProviderInstallData::GetInstallState( |
| 232 const GURL& requested_origin) { | 232 const GURL& requested_origin) { |
| 233 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 233 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 234 DCHECK(provider_map_.get()); | 234 DCHECK(provider_map_.get()); |
| 235 | 235 |
| 236 // First check to see if the origin is the default search provider. | 236 // First check to see if the origin is the default search provider. |
| 237 if (requested_origin.spec() == default_search_origin_) | 237 if (requested_origin.spec() == default_search_origin_) |
| 238 return INSTALLED_AS_DEFAULT; | 238 return INSTALLED_AS_DEFAULT; |
| 239 | 239 |
| 240 // Is the url any search provider? | 240 // Is the url any search provider? |
| 241 const TemplateURLSet* urls = provider_map_->GetURLsForHost( | 241 const TemplateURLSet* urls = provider_map_->GetURLsForHost( |
| 242 requested_origin.host()); | 242 requested_origin.host()); |
| 243 if (!urls) | 243 if (!urls) |
| 244 return NOT_INSTALLED; | 244 return NOT_INSTALLED; |
| 245 | 245 |
| 246 IOThreadSearchTermsData search_terms_data(google_base_url_); | 246 IOThreadSearchTermsData search_terms_data(google_base_url_); |
| 247 for (TemplateURLSet::const_iterator i = urls->begin(); | 247 for (TemplateURLSet::const_iterator i = urls->begin(); |
| 248 i != urls->end(); ++i) { | 248 i != urls->end(); ++i) { |
| 249 if (IsSameOrigin(requested_origin, *i, search_terms_data)) | 249 if (IsSameOrigin(requested_origin, *i, search_terms_data)) |
| 250 return INSTALLED_BUT_NOT_DEFAULT; | 250 return INSTALLED_BUT_NOT_DEFAULT; |
| 251 } | 251 } |
| 252 return NOT_INSTALLED; | 252 return NOT_INSTALLED; |
| 253 } | 253 } |
| 254 | 254 |
| 255 void SearchProviderInstallData::OnGoogleURLChange( | 255 void SearchProviderInstallData::OnGoogleURLChange( |
| 256 const std::string& google_base_url) { | 256 const std::string& google_base_url) { |
| 257 google_base_url_ = google_base_url; | 257 google_base_url_ = google_base_url; |
| 258 } | 258 } |
| 259 | 259 |
| 260 void SearchProviderInstallData::OnTemplateURLsLoaded( | 260 void SearchProviderInstallData::OnTemplateURLsLoaded( |
| 261 ScopedVector<TemplateURL> template_urls, | 261 ScopedVector<TemplateURL> template_urls, |
| 262 TemplateURL* default_provider) { | 262 TemplateURL* default_provider) { |
| 263 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 263 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 264 | 264 |
| 265 template_urls_ = template_urls.Pass(); | 265 template_urls_ = template_urls.Pass(); |
| 266 | 266 |
| 267 IOThreadSearchTermsData search_terms_data(google_base_url_); | 267 IOThreadSearchTermsData search_terms_data(google_base_url_); |
| 268 provider_map_.reset(new SearchHostToURLsMap()); | 268 provider_map_.reset(new SearchHostToURLsMap()); |
| 269 provider_map_->Init(template_urls_.get(), search_terms_data); | 269 provider_map_->Init(template_urls_.get(), search_terms_data); |
| 270 SetDefault(default_provider); | 270 SetDefault(default_provider); |
| 271 NotifyLoaded(); | 271 NotifyLoaded(); |
| 272 } | 272 } |
| 273 | 273 |
| 274 void SearchProviderInstallData::SetDefault(const TemplateURL* template_url) { | 274 void SearchProviderInstallData::SetDefault(const TemplateURL* template_url) { |
| 275 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 275 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 276 | 276 |
| 277 if (!template_url) { | 277 if (!template_url) { |
| 278 default_search_origin_.clear(); | 278 default_search_origin_.clear(); |
| 279 return; | 279 return; |
| 280 } | 280 } |
| 281 | 281 |
| 282 DCHECK(template_url->GetType() != TemplateURL::OMNIBOX_API_EXTENSION); | 282 DCHECK(template_url->GetType() != TemplateURL::OMNIBOX_API_EXTENSION); |
| 283 | 283 |
| 284 IOThreadSearchTermsData search_terms_data(google_base_url_); | 284 IOThreadSearchTermsData search_terms_data(google_base_url_); |
| 285 const GURL url(template_url->GenerateSearchURL(search_terms_data)); | 285 const GURL url(template_url->GenerateSearchURL(search_terms_data)); |
| 286 if (!url.is_valid() || !url.has_host()) { | 286 if (!url.is_valid() || !url.has_host()) { |
| 287 default_search_origin_.clear(); | 287 default_search_origin_.clear(); |
| 288 return; | 288 return; |
| 289 } | 289 } |
| 290 default_search_origin_ = url.GetOrigin().spec(); | 290 default_search_origin_ = url.GetOrigin().spec(); |
| 291 } | 291 } |
| 292 | 292 |
| 293 void SearchProviderInstallData::OnLoadFailed() { | 293 void SearchProviderInstallData::OnLoadFailed() { |
| 294 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 294 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 295 | 295 |
| 296 provider_map_.reset(new SearchHostToURLsMap()); | 296 provider_map_.reset(new SearchHostToURLsMap()); |
| 297 IOThreadSearchTermsData search_terms_data(google_base_url_); | 297 IOThreadSearchTermsData search_terms_data(google_base_url_); |
| 298 provider_map_->Init(template_urls_.get(), search_terms_data); | 298 provider_map_->Init(template_urls_.get(), search_terms_data); |
| 299 SetDefault(NULL); | 299 SetDefault(NULL); |
| 300 NotifyLoaded(); | 300 NotifyLoaded(); |
| 301 } | 301 } |
| 302 | 302 |
| 303 void SearchProviderInstallData::NotifyLoaded() { | 303 void SearchProviderInstallData::NotifyLoaded() { |
| 304 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 304 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 305 | 305 |
| 306 std::vector<base::Closure> closure_queue; | 306 std::vector<base::Closure> closure_queue; |
| 307 closure_queue.swap(closure_queue_); | 307 closure_queue.swap(closure_queue_); |
| 308 | 308 |
| 309 std::for_each(closure_queue.begin(), | 309 std::for_each(closure_queue.begin(), |
| 310 closure_queue.end(), | 310 closure_queue.end(), |
| 311 std::mem_fun_ref(&base::Closure::Run)); | 311 std::mem_fun_ref(&base::Closure::Run)); |
| 312 | 312 |
| 313 // Since we expect this request to be rare, clear out the information. This | 313 // Since we expect this request to be rare, clear out the information. This |
| 314 // also keeps the responses current as the search providers change. | 314 // also keeps the responses current as the search providers change. |
| 315 provider_map_.reset(); | 315 provider_map_.reset(); |
| 316 SetDefault(NULL); | 316 SetDefault(NULL); |
| 317 } | 317 } |
| OLD | NEW |