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