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