OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/apps/drive/drive_service_bridge.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "chrome/browser/drive/drive_api_service.h" |
| 11 #include "chrome/browser/drive/drive_app_registry.h" |
| 12 #include "chrome/browser/drive/drive_notification_manager.h" |
| 13 #include "chrome/browser/drive/drive_notification_manager_factory.h" |
| 14 #include "chrome/browser/drive/drive_notification_observer.h" |
| 15 #include "chrome/browser/profiles/profile.h" |
| 16 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| 17 #include "chrome/browser/signin/signin_manager_factory.h" |
| 18 #include "components/signin/core/browser/profile_oauth2_token_service.h" |
| 19 #include "components/signin/core/browser/signin_manager.h" |
| 20 #include "content/public/browser/browser_thread.h" |
| 21 |
| 22 namespace extensions { |
| 23 |
| 24 namespace { |
| 25 |
| 26 // Hosts DriveAPIService and DriveAppRegistry. |
| 27 // TODO(xiyuan): Optimize to leverage chromeos::DriveIntegrationService. |
| 28 class DriveServiceBridgeImpl : public DriveServiceBridge, |
| 29 public drive::DriveServiceObserver, |
| 30 public drive::DriveNotificationObserver { |
| 31 public: |
| 32 explicit DriveServiceBridgeImpl(Profile* profile); |
| 33 virtual ~DriveServiceBridgeImpl(); |
| 34 |
| 35 void Initialize(); |
| 36 |
| 37 // DriveServiceBridge: |
| 38 virtual drive::DriveAppRegistry* GetAppRegistry() OVERRIDE; |
| 39 |
| 40 // drive::DriveServiceObserver: |
| 41 virtual void OnReadyToSendRequests() OVERRIDE; |
| 42 |
| 43 // drive::DriveNotificationObserver: |
| 44 virtual void OnNotificationReceived() OVERRIDE; |
| 45 virtual void OnPushNotificationEnabled(bool enabled) OVERRIDE; |
| 46 |
| 47 private: |
| 48 Profile* profile_; |
| 49 scoped_ptr<drive::DriveServiceInterface> drive_service_; |
| 50 scoped_ptr<drive::DriveAppRegistry> drive_app_registry_; |
| 51 |
| 52 DISALLOW_COPY_AND_ASSIGN(DriveServiceBridgeImpl); |
| 53 }; |
| 54 |
| 55 DriveServiceBridgeImpl::DriveServiceBridgeImpl(Profile* profile) |
| 56 : profile_(profile) { |
| 57 DCHECK(profile_); |
| 58 } |
| 59 |
| 60 DriveServiceBridgeImpl::~DriveServiceBridgeImpl() { |
| 61 drive::DriveNotificationManager* drive_notification_manager = |
| 62 drive::DriveNotificationManagerFactory::FindForBrowserContext(profile_); |
| 63 if (drive_notification_manager) |
| 64 drive_notification_manager->RemoveObserver(this); |
| 65 |
| 66 drive_service_->RemoveObserver(this); |
| 67 |
| 68 drive_app_registry_.reset(); |
| 69 drive_service_.reset(); |
| 70 } |
| 71 |
| 72 void DriveServiceBridgeImpl::Initialize() { |
| 73 scoped_refptr<base::SequencedWorkerPool> worker_pool( |
| 74 content::BrowserThread::GetBlockingPool()); |
| 75 scoped_refptr<base::SequencedTaskRunner> drive_task_runner( |
| 76 worker_pool->GetSequencedTaskRunnerWithShutdownBehavior( |
| 77 worker_pool->GetSequenceToken(), |
| 78 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)); |
| 79 |
| 80 ProfileOAuth2TokenService* token_service = |
| 81 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); |
| 82 drive_service_.reset(new drive::DriveAPIService( |
| 83 token_service, |
| 84 profile_->GetRequestContext(), |
| 85 drive_task_runner.get(), |
| 86 GURL(google_apis::DriveApiUrlGenerator::kBaseUrlForProduction), |
| 87 GURL(google_apis::DriveApiUrlGenerator::kBaseDownloadUrlForProduction), |
| 88 GURL(google_apis::GDataWapiUrlGenerator::kBaseUrlForProduction), |
| 89 std::string() /* custom_user_agent */)); |
| 90 SigninManagerBase* signin_manager = |
| 91 SigninManagerFactory::GetForProfile(profile_); |
| 92 drive_service_->Initialize(signin_manager->GetAuthenticatedAccountId()); |
| 93 drive_service_->AddObserver(this); |
| 94 |
| 95 drive::DriveNotificationManager* drive_notification_manager = |
| 96 drive::DriveNotificationManagerFactory::GetForBrowserContext(profile_); |
| 97 if (drive_notification_manager) |
| 98 drive_notification_manager->AddObserver(this); |
| 99 |
| 100 drive_app_registry_.reset(new drive::DriveAppRegistry(drive_service_.get())); |
| 101 if (drive_service_->CanSendRequest()) |
| 102 drive_app_registry_->Update(); |
| 103 } |
| 104 |
| 105 drive::DriveAppRegistry* DriveServiceBridgeImpl::GetAppRegistry() { |
| 106 return drive_app_registry_.get(); |
| 107 } |
| 108 |
| 109 void DriveServiceBridgeImpl::OnReadyToSendRequests() { |
| 110 drive_app_registry_->Update(); |
| 111 } |
| 112 |
| 113 void DriveServiceBridgeImpl::OnNotificationReceived() { |
| 114 if (drive_service_->CanSendRequest()) |
| 115 drive_app_registry_->Update(); |
| 116 } |
| 117 |
| 118 void DriveServiceBridgeImpl::OnPushNotificationEnabled(bool enabled) { |
| 119 if (enabled && drive_service_->CanSendRequest()) |
| 120 drive_app_registry_->Update(); |
| 121 } |
| 122 |
| 123 } // namespace |
| 124 |
| 125 // static |
| 126 scoped_ptr<DriveServiceBridge> DriveServiceBridge::Create(Profile* profile) { |
| 127 scoped_ptr<DriveServiceBridgeImpl> bridge( |
| 128 new DriveServiceBridgeImpl(profile)); |
| 129 bridge->Initialize(); |
| 130 return bridge.PassAs<DriveServiceBridge>(); |
| 131 } |
| 132 |
| 133 // static |
| 134 void DriveServiceBridge::AppendDependsOnFactories( |
| 135 std::set<BrowserContextKeyedServiceFactory*>* factories) { |
| 136 DCHECK(factories); |
| 137 factories->insert(ProfileOAuth2TokenServiceFactory::GetInstance()); |
| 138 factories->insert(SigninManagerFactory::GetInstance()); |
| 139 factories->insert(drive::DriveNotificationManagerFactory::GetInstance()); |
| 140 } |
| 141 |
| 142 } // namespace extensions |
OLD | NEW |