Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(42)

Side by Side Diff: chrome/browser/chrome_to_mobile_service.cc

Issue 15580002: Make use of InvalidationService (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/chrome_to_mobile_service.h" 5 #include "chrome/browser/chrome_to_mobile_service.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/command_line.h" 8 #include "base/command_line.h"
9 #include "base/file_util.h" 9 #include "base/file_util.h"
10 #include "base/guid.h" 10 #include "base/guid.h"
11 #include "base/json/json_reader.h" 11 #include "base/json/json_reader.h"
12 #include "base/json/json_writer.h" 12 #include "base/json/json_writer.h"
13 #include "base/metrics/histogram.h" 13 #include "base/metrics/histogram.h"
14 #include "base/prefs/pref_service.h" 14 #include "base/prefs/pref_service.h"
15 #include "base/strings/utf_string_conversions.h" 15 #include "base/strings/utf_string_conversions.h"
16 #include "chrome/app/chrome_command_ids.h" 16 #include "chrome/app/chrome_command_ids.h"
17 #include "chrome/browser/chrome_to_mobile_service_factory.h" 17 #include "chrome/browser/chrome_to_mobile_service_factory.h"
18 #include "chrome/browser/invalidation/invalidation_service.h"
19 #include "chrome/browser/invalidation/invalidation_service_factory.h"
18 #include "chrome/browser/printing/cloud_print/cloud_print_url.h" 20 #include "chrome/browser/printing/cloud_print/cloud_print_url.h"
19 #include "chrome/browser/profiles/profile.h" 21 #include "chrome/browser/profiles/profile.h"
20 #include "chrome/browser/signin/token_service.h" 22 #include "chrome/browser/signin/token_service.h"
21 #include "chrome/browser/signin/token_service_factory.h" 23 #include "chrome/browser/signin/token_service_factory.h"
22 #include "chrome/browser/sync/profile_sync_service.h"
23 #include "chrome/browser/sync/profile_sync_service_factory.h"
24 #include "chrome/browser/ui/browser.h" 24 #include "chrome/browser/ui/browser.h"
25 #include "chrome/browser/ui/browser_command_controller.h" 25 #include "chrome/browser/ui/browser_command_controller.h"
26 #include "chrome/browser/ui/browser_finder.h" 26 #include "chrome/browser/ui/browser_finder.h"
27 #include "chrome/browser/ui/browser_navigator.h" 27 #include "chrome/browser/ui/browser_navigator.h"
28 #include "chrome/browser/ui/tabs/tab_strip_model.h" 28 #include "chrome/browser/ui/tabs/tab_strip_model.h"
29 #include "chrome/common/chrome_notification_types.h" 29 #include "chrome/common/chrome_notification_types.h"
30 #include "chrome/common/chrome_switches.h" 30 #include "chrome/common/chrome_switches.h"
31 #include "chrome/common/cloud_print/cloud_print_constants.h" 31 #include "chrome/common/cloud_print/cloud_print_constants.h"
32 #include "chrome/common/cloud_print/cloud_print_helpers.h" 32 #include "chrome/common/cloud_print/cloud_print_helpers.h"
33 #include "chrome/common/extensions/feature_switch.h" 33 #include "chrome/common/extensions/feature_switch.h"
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after
254 // static 254 // static
255 void ChromeToMobileService::RegisterUserPrefs( 255 void ChromeToMobileService::RegisterUserPrefs(
256 user_prefs::PrefRegistrySyncable* registry) { 256 user_prefs::PrefRegistrySyncable* registry) {
257 registry->RegisterListPref(prefs::kChromeToMobileDeviceList, 257 registry->RegisterListPref(prefs::kChromeToMobileDeviceList,
258 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); 258 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
259 } 259 }
260 260
261 ChromeToMobileService::ChromeToMobileService(Profile* profile) 261 ChromeToMobileService::ChromeToMobileService(Profile* profile)
262 : weak_ptr_factory_(this), 262 : weak_ptr_factory_(this),
263 profile_(profile), 263 profile_(profile),
264 sync_invalidation_enabled_(false) { 264 invalidation_enabled_(false) {
265 // TODO(msw): Unit tests do not provide profiles; see http://crbug.com/122183 265 // TODO(msw): Unit tests do not provide profiles; see http://crbug.com/122183
266 ProfileSyncService* profile_sync_service = 266
267 profile_ ? ProfileSyncServiceFactory::GetForProfile(profile_) : NULL; 267 invalidation::InvalidationService* invalidation_service = profile_ ?
268 if (profile_sync_service) { 268 invalidation::InvalidationServiceFactory::GetForProfile(profile_) : NULL;
269 if (invalidation_service) {
269 CloudPrintURL cloud_print_url(profile_); 270 CloudPrintURL cloud_print_url(profile_);
270 cloud_print_url_ = cloud_print_url.GetCloudPrintServiceURL(); 271 cloud_print_url_ = cloud_print_url.GetCloudPrintServiceURL();
271 sync_invalidation_enabled_ = 272 invalidation_enabled_ =
272 (profile_sync_service->GetInvalidatorState() == 273 (invalidation_service->GetInvalidatorState() ==
273 syncer::INVALIDATIONS_ENABLED); 274 syncer::INVALIDATIONS_ENABLED);
274 // Register for cloud print device list invalidation notifications. 275 // Register for cloud print device list invalidation notifications.
275 profile_sync_service->RegisterInvalidationHandler(this); 276 invalidation_service->RegisterInvalidationHandler(this);
276 syncer::ObjectIdSet ids; 277 syncer::ObjectIdSet ids;
277 ids.insert(invalidation::ObjectId( 278 ids.insert(invalidation::ObjectId(
278 ipc::invalidation::ObjectSource::CHROME_COMPONENTS, 279 ipc::invalidation::ObjectSource::CHROME_COMPONENTS,
279 kSyncInvalidationObjectIdChromeToMobileDeviceList)); 280 kSyncInvalidationObjectIdChromeToMobileDeviceList));
280 profile_sync_service->UpdateRegisteredInvalidationIds(this, ids); 281 invalidation_service->UpdateRegisteredInvalidationIds(this, ids);
281 } 282 }
282 } 283 }
283 284
284 ChromeToMobileService::~ChromeToMobileService() { 285 ChromeToMobileService::~ChromeToMobileService() {
285 while (!snapshots_.empty()) 286 while (!snapshots_.empty())
286 DeleteSnapshot(*snapshots_.begin()); 287 DeleteSnapshot(*snapshots_.begin());
287 } 288 }
288 289
289 bool ChromeToMobileService::HasMobiles() const { 290 bool ChromeToMobileService::HasMobiles() const {
290 const base::ListValue* mobiles = GetMobiles(); 291 const base::ListValue* mobiles = GetMobiles();
291 return mobiles && !mobiles->empty(); 292 return mobiles && !mobiles->empty();
292 } 293 }
293 294
294 const base::ListValue* ChromeToMobileService::GetMobiles() const { 295 const base::ListValue* ChromeToMobileService::GetMobiles() const {
295 return sync_invalidation_enabled_ ? 296 return invalidation_enabled_ ?
296 profile_->GetPrefs()->GetList(prefs::kChromeToMobileDeviceList) : NULL; 297 profile_->GetPrefs()->GetList(prefs::kChromeToMobileDeviceList) : NULL;
297 } 298 }
298 299
299 void ChromeToMobileService::GenerateSnapshot(Browser* browser, 300 void ChromeToMobileService::GenerateSnapshot(Browser* browser,
300 base::WeakPtr<Observer> observer) { 301 base::WeakPtr<Observer> observer) {
301 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 302 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
302 // Callback SnapshotFileCreated from CreateSnapshotFile to continue. 303 // Callback SnapshotFileCreated from CreateSnapshotFile to continue.
303 CreateSnapshotFileCallback callback = 304 CreateSnapshotFileCallback callback =
304 base::Bind(&ChromeToMobileService::SnapshotFileCreated, 305 base::Bind(&ChromeToMobileService::SnapshotFileCreated,
305 weak_ptr_factory_.GetWeakPtr(), observer, 306 weak_ptr_factory_.GetWeakPtr(), observer,
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
377 LogMetric(LEARN_MORE_CLICKED); 378 LogMetric(LEARN_MORE_CLICKED);
378 chrome::NavigateParams params(browser, 379 chrome::NavigateParams params(browser,
379 GURL(chrome::kChromeToMobileLearnMoreURL), content::PAGE_TRANSITION_LINK); 380 GURL(chrome::kChromeToMobileLearnMoreURL), content::PAGE_TRANSITION_LINK);
380 params.disposition = NEW_FOREGROUND_TAB; 381 params.disposition = NEW_FOREGROUND_TAB;
381 chrome::Navigate(&params); 382 chrome::Navigate(&params);
382 } 383 }
383 384
384 void ChromeToMobileService::Shutdown() { 385 void ChromeToMobileService::Shutdown() {
385 // TODO(msw): Unit tests do not provide profiles; see http://crbug.com/122183 386 // TODO(msw): Unit tests do not provide profiles; see http://crbug.com/122183
386 // Unregister for cloud print device list invalidation notifications. 387 // Unregister for cloud print device list invalidation notifications.
387 ProfileSyncService* profile_sync_service = 388 invalidation::InvalidationService* invalidation_service = profile_ ?
388 profile_ ? ProfileSyncServiceFactory::GetForProfile(profile_) : NULL; 389 invalidation::InvalidationServiceFactory::GetForProfile(profile_) : NULL;
389 if (profile_sync_service) 390 if (invalidation_service)
390 profile_sync_service->UnregisterInvalidationHandler(this); 391 invalidation_service->UnregisterInvalidationHandler(this);
391 } 392 }
392 393
393 void ChromeToMobileService::OnURLFetchComplete(const net::URLFetcher* source) { 394 void ChromeToMobileService::OnURLFetchComplete(const net::URLFetcher* source) {
394 if (source->GetOriginalURL() == GetSearchURL(cloud_print_url_)) 395 if (source->GetOriginalURL() == GetSearchURL(cloud_print_url_))
395 HandleSearchResponse(source); 396 HandleSearchResponse(source);
396 else if (source->GetOriginalURL() == GetSubmitURL(cloud_print_url_)) 397 else if (source->GetOriginalURL() == GetSubmitURL(cloud_print_url_))
397 HandleSubmitResponse(source); 398 HandleSubmitResponse(source);
398 else 399 else
399 NOTREACHED(); 400 NOTREACHED();
400 401
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
465 auth_retry_timer_.Start(FROM_HERE, delay, this, 466 auth_retry_timer_.Start(FROM_HERE, delay, this,
466 &ChromeToMobileService::RequestAccessToken); 467 &ChromeToMobileService::RequestAccessToken);
467 468
468 // Clear the mobile list, which may be (or become) out of date. 469 // Clear the mobile list, which may be (or become) out of date.
469 ListValue empty; 470 ListValue empty;
470 profile_->GetPrefs()->Set(prefs::kChromeToMobileDeviceList, empty); 471 profile_->GetPrefs()->Set(prefs::kChromeToMobileDeviceList, empty);
471 } 472 }
472 473
473 void ChromeToMobileService::OnInvalidatorStateChange( 474 void ChromeToMobileService::OnInvalidatorStateChange(
474 syncer::InvalidatorState state) { 475 syncer::InvalidatorState state) {
475 sync_invalidation_enabled_ = (state == syncer::INVALIDATIONS_ENABLED); 476 invalidation_enabled_ = (state == syncer::INVALIDATIONS_ENABLED);
476 } 477 }
477 478
478 void ChromeToMobileService::OnIncomingInvalidation( 479 void ChromeToMobileService::OnIncomingInvalidation(
479 const syncer::ObjectIdInvalidationMap& invalidation_map) { 480 const syncer::ObjectIdInvalidationMap& invalidation_map) {
480 DCHECK_EQ(1U, invalidation_map.size()); 481 DCHECK_EQ(1U, invalidation_map.size());
481 DCHECK_EQ(1U, invalidation_map.count(invalidation::ObjectId( 482 DCHECK_EQ(1U, invalidation_map.count(invalidation::ObjectId(
482 ipc::invalidation::ObjectSource::CHROME_COMPONENTS, 483 ipc::invalidation::ObjectSource::CHROME_COMPONENTS,
483 kSyncInvalidationObjectIdChromeToMobileDeviceList))); 484 kSyncInvalidationObjectIdChromeToMobileDeviceList)));
484 // TODO(msw): Unit tests do not provide profiles; see http://crbug.com/122183 485 // TODO(msw): Unit tests do not provide profiles; see http://crbug.com/122183
485 ProfileSyncService* profile_sync_service = 486 invalidation::InvalidationService* invalidation_service = profile_ ?
486 profile_ ? ProfileSyncServiceFactory::GetForProfile(profile_) : NULL; 487 invalidation::InvalidationServiceFactory::GetForProfile(profile_) : NULL;
487 if (profile_sync_service) { 488 if (invalidation_service) {
488 // TODO(dcheng): Only acknowledge the invalidation once the device search 489 // TODO(dcheng): Only acknowledge the invalidation once the device search
489 // has finished. http://crbug.com/156843. 490 // has finished. http://crbug.com/156843.
490 profile_sync_service->AcknowledgeInvalidation( 491 invalidation_service->AcknowledgeInvalidation(
491 invalidation_map.begin()->first, 492 invalidation_map.begin()->first,
492 invalidation_map.begin()->second.ack_handle); 493 invalidation_map.begin()->second.ack_handle);
493 } 494 }
494 RequestDeviceSearch(); 495 RequestDeviceSearch();
495 } 496 }
496 497
497 const std::string& ChromeToMobileService::GetAccessTokenForTest() const { 498 const std::string& ChromeToMobileService::GetAccessTokenForTest() const {
498 return access_token_; 499 return access_token_;
499 } 500 }
500 501
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after
754 755
755 // Report failure below and ignore the second response. 756 // Report failure below and ignore the second response.
756 request_observer_map_.erase(other); 757 request_observer_map_.erase(other);
757 break; 758 break;
758 } 759 }
759 } 760 }
760 761
761 if (observer.get()) 762 if (observer.get())
762 observer->OnSendComplete(success); 763 observer->OnSendComplete(success);
763 } 764 }
OLDNEW
« no previous file with comments | « chrome/browser/chrome_to_mobile_service.h ('k') | chrome/browser/chrome_to_mobile_service_factory.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698