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