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.
5 #include "chrome/browser/apps/drive/drive_service_bridge.h"
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"
24 // Hosts DriveAPIService and DriveAppRegistry.
25 // TODO(xiyuan): Optimize to leverage chromeos::DriveIntegrationService.
26 class DriveServiceBridgeImpl : public DriveServiceBridge,
27 public drive::DriveServiceObserver,
28 public drive::DriveNotificationObserver {
30 explicit DriveServiceBridgeImpl(Profile* profile);
31 ~DriveServiceBridgeImpl() override;
35 // DriveServiceBridge:
36 drive::DriveAppRegistry* GetAppRegistry() override;
38 // drive::DriveServiceObserver:
39 void OnReadyToSendRequests() override;
41 // drive::DriveNotificationObserver:
42 void OnNotificationReceived() override;
43 void OnPushNotificationEnabled(bool enabled) override;
47 scoped_ptr<drive::DriveServiceInterface> drive_service_;
48 scoped_ptr<drive::DriveAppRegistry> drive_app_registry_;
50 DISALLOW_COPY_AND_ASSIGN(DriveServiceBridgeImpl);
53 DriveServiceBridgeImpl::DriveServiceBridgeImpl(Profile* profile)
58 DriveServiceBridgeImpl::~DriveServiceBridgeImpl() {
59 drive::DriveNotificationManager* drive_notification_manager =
60 drive::DriveNotificationManagerFactory::FindForBrowserContext(profile_);
61 if (drive_notification_manager)
62 drive_notification_manager->RemoveObserver(this);
64 drive_service_->RemoveObserver(this);
66 drive_app_registry_.reset();
67 drive_service_.reset();
70 void DriveServiceBridgeImpl::Initialize() {
71 scoped_refptr<base::SequencedWorkerPool> worker_pool(
72 content::BrowserThread::GetBlockingPool());
73 scoped_refptr<base::SequencedTaskRunner> drive_task_runner(
74 worker_pool->GetSequencedTaskRunnerWithShutdownBehavior(
75 worker_pool->GetSequenceToken(),
76 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN));
78 ProfileOAuth2TokenService* token_service =
79 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_);
80 drive_service_.reset(new drive::DriveAPIService(
82 profile_->GetRequestContext(),
83 drive_task_runner.get(),
84 GURL(google_apis::DriveApiUrlGenerator::kBaseUrlForProduction),
85 GURL(google_apis::DriveApiUrlGenerator::kBaseDownloadUrlForProduction),
86 GURL(google_apis::GDataWapiUrlGenerator::kBaseUrlForProduction),
87 std::string() /* custom_user_agent */));
88 SigninManagerBase* signin_manager =
89 SigninManagerFactory::GetForProfile(profile_);
90 drive_service_->Initialize(signin_manager->GetAuthenticatedAccountId());
91 drive_service_->AddObserver(this);
93 drive::DriveNotificationManager* drive_notification_manager =
94 drive::DriveNotificationManagerFactory::GetForBrowserContext(profile_);
95 if (drive_notification_manager)
96 drive_notification_manager->AddObserver(this);
98 drive_app_registry_.reset(new drive::DriveAppRegistry(drive_service_.get()));
99 if (drive_service_->CanSendRequest())
100 drive_app_registry_->Update();
103 drive::DriveAppRegistry* DriveServiceBridgeImpl::GetAppRegistry() {
104 return drive_app_registry_.get();
107 void DriveServiceBridgeImpl::OnReadyToSendRequests() {
108 drive_app_registry_->Update();
111 void DriveServiceBridgeImpl::OnNotificationReceived() {
112 if (drive_service_->CanSendRequest())
113 drive_app_registry_->Update();
116 void DriveServiceBridgeImpl::OnPushNotificationEnabled(bool enabled) {
117 if (enabled && drive_service_->CanSendRequest())
118 drive_app_registry_->Update();
124 scoped_ptr<DriveServiceBridge> DriveServiceBridge::Create(Profile* profile) {
125 scoped_ptr<DriveServiceBridgeImpl> bridge(
126 new DriveServiceBridgeImpl(profile));
127 bridge->Initialize();
128 return bridge.Pass();
132 void DriveServiceBridge::AppendDependsOnFactories(
133 std::set<BrowserContextKeyedServiceFactory*>* factories) {
135 factories->insert(ProfileOAuth2TokenServiceFactory::GetInstance());
136 factories->insert(SigninManagerFactory::GetInstance());
137 factories->insert(drive::DriveNotificationManagerFactory::GetInstance());