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_notification_manager_factory.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
13 #include "chrome/browser/signin/signin_manager_factory.h"
14 #include "components/drive/drive_app_registry.h"
15 #include "components/drive/drive_notification_manager.h"
16 #include "components/drive/drive_notification_observer.h"
17 #include "components/drive/service/drive_api_service.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 std::string() /* custom_user_agent */));
87 SigninManagerBase
* signin_manager
=
88 SigninManagerFactory::GetForProfile(profile_
);
89 drive_service_
->Initialize(signin_manager
->GetAuthenticatedAccountId());
90 drive_service_
->AddObserver(this);
92 drive::DriveNotificationManager
* drive_notification_manager
=
93 drive::DriveNotificationManagerFactory::GetForBrowserContext(profile_
);
94 if (drive_notification_manager
)
95 drive_notification_manager
->AddObserver(this);
97 drive_app_registry_
.reset(new drive::DriveAppRegistry(drive_service_
.get()));
98 if (drive_service_
->CanSendRequest())
99 drive_app_registry_
->Update();
102 drive::DriveAppRegistry
* DriveServiceBridgeImpl::GetAppRegistry() {
103 return drive_app_registry_
.get();
106 void DriveServiceBridgeImpl::OnReadyToSendRequests() {
107 drive_app_registry_
->Update();
110 void DriveServiceBridgeImpl::OnNotificationReceived() {
111 if (drive_service_
->CanSendRequest())
112 drive_app_registry_
->Update();
115 void DriveServiceBridgeImpl::OnPushNotificationEnabled(bool enabled
) {
116 if (enabled
&& drive_service_
->CanSendRequest())
117 drive_app_registry_
->Update();
123 scoped_ptr
<DriveServiceBridge
> DriveServiceBridge::Create(Profile
* profile
) {
124 scoped_ptr
<DriveServiceBridgeImpl
> bridge(
125 new DriveServiceBridgeImpl(profile
));
126 bridge
->Initialize();
127 return bridge
.Pass();
131 void DriveServiceBridge::AppendDependsOnFactories(
132 std::set
<BrowserContextKeyedServiceFactory
*>* factories
) {
134 factories
->insert(ProfileOAuth2TokenServiceFactory::GetInstance());
135 factories
->insert(SigninManagerFactory::GetInstance());
136 factories
->insert(drive::DriveNotificationManagerFactory::GetInstance());