| 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/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/utf_string_conversions.h" | 15 #include "base/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_service.h" |
| 19 #include "chrome/browser/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 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 252 | 252 |
| 253 // static | 253 // static |
| 254 void ChromeToMobileService::RegisterUserPrefs(PrefRegistrySyncable* registry) { | 254 void ChromeToMobileService::RegisterUserPrefs(PrefRegistrySyncable* registry) { |
| 255 registry->RegisterListPref(prefs::kChromeToMobileDeviceList, | 255 registry->RegisterListPref(prefs::kChromeToMobileDeviceList, |
| 256 PrefRegistrySyncable::UNSYNCABLE_PREF); | 256 PrefRegistrySyncable::UNSYNCABLE_PREF); |
| 257 } | 257 } |
| 258 | 258 |
| 259 ChromeToMobileService::ChromeToMobileService(Profile* profile) | 259 ChromeToMobileService::ChromeToMobileService(Profile* profile) |
| 260 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)), | 260 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)), |
| 261 profile_(profile), | 261 profile_(profile), |
| 262 sync_invalidation_enabled_(false) { | 262 invalidation_enabled_(false) { |
| 263 // TODO(msw): Unit tests do not provide profiles; see http://crbug.com/122183 | 263 // TODO(msw): Unit tests do not provide profiles; see http://crbug.com/122183 |
| 264 ProfileSyncService* profile_sync_service = | 264 InvalidationService* invalidation_service = |
| 265 profile_ ? ProfileSyncServiceFactory::GetForProfile(profile_) : NULL; | 265 profile_ ? InvalidationServiceFactory::GetForProfile(profile_) : NULL; |
| 266 if (profile_sync_service) { | 266 if (invalidation_service) { |
| 267 CloudPrintURL cloud_print_url(profile_); | 267 CloudPrintURL cloud_print_url(profile_); |
| 268 cloud_print_url_ = cloud_print_url.GetCloudPrintServiceURL(); | 268 cloud_print_url_ = cloud_print_url.GetCloudPrintServiceURL(); |
| 269 sync_invalidation_enabled_ = | 269 invalidation_enabled_ = |
| 270 (profile_sync_service->GetInvalidatorState() == | 270 (invalidation_service->GetInvalidatorState() == |
| 271 syncer::INVALIDATIONS_ENABLED); | 271 syncer::INVALIDATIONS_ENABLED); |
| 272 // Register for cloud print device list invalidation notifications. | 272 // Register for cloud print device list invalidation notifications. |
| 273 profile_sync_service->RegisterInvalidationHandler(this); | 273 invalidation_service->RegisterInvalidationHandler(this); |
| 274 syncer::ObjectIdSet ids; | 274 syncer::ObjectIdSet ids; |
| 275 ids.insert(invalidation::ObjectId( | 275 ids.insert(invalidation::ObjectId( |
| 276 ipc::invalidation::ObjectSource::CHROME_COMPONENTS, | 276 ipc::invalidation::ObjectSource::CHROME_COMPONENTS, |
| 277 kSyncInvalidationObjectIdChromeToMobileDeviceList)); | 277 kSyncInvalidationObjectIdChromeToMobileDeviceList)); |
| 278 profile_sync_service->UpdateRegisteredInvalidationIds(this, ids); | 278 invalidation_service->UpdateRegisteredInvalidationIds(this, ids); |
| 279 } | 279 } |
| 280 } | 280 } |
| 281 | 281 |
| 282 ChromeToMobileService::~ChromeToMobileService() { | 282 ChromeToMobileService::~ChromeToMobileService() { |
| 283 while (!snapshots_.empty()) | 283 while (!snapshots_.empty()) |
| 284 DeleteSnapshot(*snapshots_.begin()); | 284 DeleteSnapshot(*snapshots_.begin()); |
| 285 } | 285 } |
| 286 | 286 |
| 287 bool ChromeToMobileService::HasMobiles() const { | 287 bool ChromeToMobileService::HasMobiles() const { |
| 288 const base::ListValue* mobiles = GetMobiles(); | 288 const base::ListValue* mobiles = GetMobiles(); |
| 289 return mobiles && !mobiles->empty(); | 289 return mobiles && !mobiles->empty(); |
| 290 } | 290 } |
| 291 | 291 |
| 292 const base::ListValue* ChromeToMobileService::GetMobiles() const { | 292 const base::ListValue* ChromeToMobileService::GetMobiles() const { |
| 293 return sync_invalidation_enabled_ ? | 293 return invalidation_enabled_ ? |
| 294 profile_->GetPrefs()->GetList(prefs::kChromeToMobileDeviceList) : NULL; | 294 profile_->GetPrefs()->GetList(prefs::kChromeToMobileDeviceList) : NULL; |
| 295 } | 295 } |
| 296 | 296 |
| 297 void ChromeToMobileService::GenerateSnapshot(Browser* browser, | 297 void ChromeToMobileService::GenerateSnapshot(Browser* browser, |
| 298 base::WeakPtr<Observer> observer) { | 298 base::WeakPtr<Observer> observer) { |
| 299 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 299 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 300 // Callback SnapshotFileCreated from CreateSnapshotFile to continue. | 300 // Callback SnapshotFileCreated from CreateSnapshotFile to continue. |
| 301 CreateSnapshotFileCallback callback = | 301 CreateSnapshotFileCallback callback = |
| 302 base::Bind(&ChromeToMobileService::SnapshotFileCreated, | 302 base::Bind(&ChromeToMobileService::SnapshotFileCreated, |
| 303 weak_ptr_factory_.GetWeakPtr(), observer, | 303 weak_ptr_factory_.GetWeakPtr(), observer, |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 375 LogMetric(LEARN_MORE_CLICKED); | 375 LogMetric(LEARN_MORE_CLICKED); |
| 376 chrome::NavigateParams params(browser, | 376 chrome::NavigateParams params(browser, |
| 377 GURL(chrome::kChromeToMobileLearnMoreURL), content::PAGE_TRANSITION_LINK); | 377 GURL(chrome::kChromeToMobileLearnMoreURL), content::PAGE_TRANSITION_LINK); |
| 378 params.disposition = NEW_FOREGROUND_TAB; | 378 params.disposition = NEW_FOREGROUND_TAB; |
| 379 chrome::Navigate(¶ms); | 379 chrome::Navigate(¶ms); |
| 380 } | 380 } |
| 381 | 381 |
| 382 void ChromeToMobileService::Shutdown() { | 382 void ChromeToMobileService::Shutdown() { |
| 383 // TODO(msw): Unit tests do not provide profiles; see http://crbug.com/122183 | 383 // TODO(msw): Unit tests do not provide profiles; see http://crbug.com/122183 |
| 384 // Unregister for cloud print device list invalidation notifications. | 384 // Unregister for cloud print device list invalidation notifications. |
| 385 ProfileSyncService* profile_sync_service = | 385 InvalidationService* invalidation_service = |
| 386 profile_ ? ProfileSyncServiceFactory::GetForProfile(profile_) : NULL; | 386 profile_ ? InvalidationServiceFactory::GetForProfile(profile_) : NULL; |
| 387 if (profile_sync_service) | 387 if (invalidation_service) |
| 388 profile_sync_service->UnregisterInvalidationHandler(this); | 388 invalidation_service->UnregisterInvalidationHandler(this); |
| 389 } | 389 } |
| 390 | 390 |
| 391 void ChromeToMobileService::OnURLFetchComplete(const net::URLFetcher* source) { | 391 void ChromeToMobileService::OnURLFetchComplete(const net::URLFetcher* source) { |
| 392 if (source->GetOriginalURL() == GetSearchURL(cloud_print_url_)) | 392 if (source->GetOriginalURL() == GetSearchURL(cloud_print_url_)) |
| 393 HandleSearchResponse(source); | 393 HandleSearchResponse(source); |
| 394 else if (source->GetOriginalURL() == GetSubmitURL(cloud_print_url_)) | 394 else if (source->GetOriginalURL() == GetSubmitURL(cloud_print_url_)) |
| 395 HandleSubmitResponse(source); | 395 HandleSubmitResponse(source); |
| 396 else | 396 else |
| 397 NOTREACHED(); | 397 NOTREACHED(); |
| 398 | 398 |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 463 auth_retry_timer_.Start(FROM_HERE, delay, this, | 463 auth_retry_timer_.Start(FROM_HERE, delay, this, |
| 464 &ChromeToMobileService::RequestAccessToken); | 464 &ChromeToMobileService::RequestAccessToken); |
| 465 | 465 |
| 466 // Clear the mobile list, which may be (or become) out of date. | 466 // Clear the mobile list, which may be (or become) out of date. |
| 467 ListValue empty; | 467 ListValue empty; |
| 468 profile_->GetPrefs()->Set(prefs::kChromeToMobileDeviceList, empty); | 468 profile_->GetPrefs()->Set(prefs::kChromeToMobileDeviceList, empty); |
| 469 } | 469 } |
| 470 | 470 |
| 471 void ChromeToMobileService::OnInvalidatorStateChange( | 471 void ChromeToMobileService::OnInvalidatorStateChange( |
| 472 syncer::InvalidatorState state) { | 472 syncer::InvalidatorState state) { |
| 473 sync_invalidation_enabled_ = (state == syncer::INVALIDATIONS_ENABLED); | 473 invalidation_enabled_ = (state == syncer::INVALIDATIONS_ENABLED); |
| 474 } | 474 } |
| 475 | 475 |
| 476 void ChromeToMobileService::OnIncomingInvalidation( | 476 void ChromeToMobileService::OnIncomingInvalidation( |
| 477 const syncer::ObjectIdInvalidationMap& invalidation_map) { | 477 const syncer::ObjectIdInvalidationMap& invalidation_map) { |
| 478 DCHECK_EQ(1U, invalidation_map.size()); | 478 DCHECK_EQ(1U, invalidation_map.size()); |
| 479 DCHECK_EQ(1U, invalidation_map.count(invalidation::ObjectId( | 479 DCHECK_EQ(1U, invalidation_map.count(invalidation::ObjectId( |
| 480 ipc::invalidation::ObjectSource::CHROME_COMPONENTS, | 480 ipc::invalidation::ObjectSource::CHROME_COMPONENTS, |
| 481 kSyncInvalidationObjectIdChromeToMobileDeviceList))); | 481 kSyncInvalidationObjectIdChromeToMobileDeviceList))); |
| 482 // TODO(msw): Unit tests do not provide profiles; see http://crbug.com/122183 | 482 // TODO(msw): Unit tests do not provide profiles; see http://crbug.com/122183 |
| 483 ProfileSyncService* profile_sync_service = | 483 InvalidationService* invalidation_service = |
| 484 profile_ ? ProfileSyncServiceFactory::GetForProfile(profile_) : NULL; | 484 profile_ ? InvalidationServiceFactory::GetForProfile(profile_) : NULL; |
| 485 if (profile_sync_service) { | 485 if (invalidation_service) { |
| 486 // TODO(dcheng): Only acknowledge the invalidation once the device search | 486 // TODO(dcheng): Only acknowledge the invalidation once the device search |
| 487 // has finished. http://crbug.com/156843. | 487 // has finished. http://crbug.com/156843. |
| 488 profile_sync_service->AcknowledgeInvalidation( | 488 invalidation_service->AcknowledgeInvalidation( |
| 489 invalidation_map.begin()->first, | 489 invalidation_map.begin()->first, |
| 490 invalidation_map.begin()->second.ack_handle); | 490 invalidation_map.begin()->second.ack_handle); |
| 491 } | 491 } |
| 492 RequestDeviceSearch(); | 492 RequestDeviceSearch(); |
| 493 } | 493 } |
| 494 | 494 |
| 495 const std::string& ChromeToMobileService::GetAccessTokenForTest() const { | 495 const std::string& ChromeToMobileService::GetAccessTokenForTest() const { |
| 496 return access_token_; | 496 return access_token_; |
| 497 } | 497 } |
| 498 | 498 |
| (...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 752 | 752 |
| 753 // Report failure below and ignore the second response. | 753 // Report failure below and ignore the second response. |
| 754 request_observer_map_.erase(other); | 754 request_observer_map_.erase(other); |
| 755 break; | 755 break; |
| 756 } | 756 } |
| 757 } | 757 } |
| 758 | 758 |
| 759 if (observer.get()) | 759 if (observer.get()) |
| 760 observer->OnSendComplete(success); | 760 observer->OnSendComplete(success); |
| 761 } | 761 } |
| OLD | NEW |