sync_file_system_service_factory.cc revision 58537e28ecd584eab876aee8be7156509866d23a
1// Copyright (c) 2013 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/sync_file_system/sync_file_system_service_factory.h"
6
7#include "base/command_line.h"
8#include "base/threading/sequenced_worker_pool.h"
9#include "chrome/browser/drive/drive_api_service.h"
10#include "chrome/browser/drive/drive_notification_manager_factory.h"
11#include "chrome/browser/extensions/extension_system_factory.h"
12#include "chrome/browser/google_apis/drive_api_url_generator.h"
13#include "chrome/browser/google_apis/gdata_wapi_url_generator.h"
14#include "chrome/browser/profiles/profile.h"
15#include "chrome/browser/signin/profile_oauth2_token_service.h"
16#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
17#include "chrome/browser/sync/profile_sync_service.h"
18#include "chrome/browser/sync_file_system/drive_backend/sync_engine.h"
19#include "chrome/browser/sync_file_system/drive_backend_v1/drive_file_sync_service.h"
20#include "chrome/browser/sync_file_system/sync_file_system_service.h"
21#include "chrome/browser/sync_file_system/syncable_file_system_util.h"
22#include "components/browser_context_keyed_service/browser_context_dependency_manager.h"
23
24namespace sync_file_system {
25
26namespace {
27const char kDisableLastWriteWin[] = "disable-syncfs-last-write-win";
28const char kEnableSyncFileSystemV2[] = "enable-syncfs-v2";
29}
30
31// static
32SyncFileSystemService* SyncFileSystemServiceFactory::GetForProfile(
33    Profile* profile) {
34  return static_cast<SyncFileSystemService*>(
35      GetInstance()->GetServiceForBrowserContext(profile, true));
36}
37
38// static
39SyncFileSystemServiceFactory* SyncFileSystemServiceFactory::GetInstance() {
40  return Singleton<SyncFileSystemServiceFactory>::get();
41}
42
43void SyncFileSystemServiceFactory::set_mock_remote_file_service(
44    scoped_ptr<RemoteFileSyncService> mock_remote_service) {
45  mock_remote_file_service_ = mock_remote_service.Pass();
46}
47
48SyncFileSystemServiceFactory::SyncFileSystemServiceFactory()
49    : BrowserContextKeyedServiceFactory(
50        "SyncFileSystemService",
51        BrowserContextDependencyManager::GetInstance()) {
52  DependsOn(drive::DriveNotificationManagerFactory::GetInstance());
53  DependsOn(ProfileOAuth2TokenServiceFactory::GetInstance());
54  DependsOn(extensions::ExtensionSystemFactory::GetInstance());
55}
56
57SyncFileSystemServiceFactory::~SyncFileSystemServiceFactory() {}
58
59BrowserContextKeyedService*
60SyncFileSystemServiceFactory::BuildServiceInstanceFor(
61    content::BrowserContext* context) const {
62  Profile* profile = Profile::FromBrowserContext(context);
63
64  SyncFileSystemService* service = new SyncFileSystemService(profile);
65
66  scoped_ptr<LocalFileSyncService> local_file_service(
67      new LocalFileSyncService(profile));
68
69  scoped_ptr<RemoteFileSyncService> remote_file_service;
70  if (mock_remote_file_service_) {
71    remote_file_service = mock_remote_file_service_.Pass();
72  } else if (CommandLine::ForCurrentProcess()->HasSwitch(
73      kEnableSyncFileSystemV2)) {
74    RegisterSyncableFileSystem();
75
76    GURL base_drive_url(
77        google_apis::DriveApiUrlGenerator::kBaseUrlForProduction);
78    GURL base_download_url(
79        google_apis::DriveApiUrlGenerator::kBaseDownloadUrlForProduction);
80    GURL wapi_base_url(
81        google_apis::GDataWapiUrlGenerator::kBaseUrlForProduction);
82
83    scoped_refptr<base::SequencedWorkerPool> worker_pool(
84        content::BrowserThread::GetBlockingPool());
85
86    scoped_ptr<drive::DriveAPIService> drive_api_service(
87        new drive::DriveAPIService(
88            ProfileOAuth2TokenServiceFactory::GetForProfile(profile),
89            context->GetRequestContext(),
90            worker_pool.get(),
91            base_drive_url, base_download_url, wapi_base_url,
92            std::string() /* custom_user_agent */));
93
94    drive::DriveNotificationManager* notification_manager =
95        drive::DriveNotificationManagerFactory::GetForBrowserContext(profile);
96    ExtensionService* extension_service =
97        extensions::ExtensionSystem::Get(profile)->extension_service();
98
99    scoped_refptr<base::SequencedTaskRunner> task_runner(
100        worker_pool->GetSequencedTaskRunnerWithShutdownBehavior(
101            worker_pool->GetSequenceToken(),
102            base::SequencedWorkerPool::SKIP_ON_SHUTDOWN));
103
104    scoped_ptr<drive_backend::SyncEngine> sync_engine(
105        new drive_backend::SyncEngine(
106            context->GetPath(),
107            task_runner.get(),
108            drive_api_service.Pass(),
109            notification_manager,
110            extension_service));
111
112    sync_engine->Initialize();
113    remote_file_service = sync_engine.PassAs<RemoteFileSyncService>();
114  } else {
115    // FileSystem needs to be registered before DriveFileSyncService runs
116    // its initialization code.
117    RegisterSyncableFileSystem();
118    remote_file_service =
119        DriveFileSyncService::Create(profile).PassAs<RemoteFileSyncService>();
120  }
121
122  if (CommandLine::ForCurrentProcess()->HasSwitch(kDisableLastWriteWin)) {
123    remote_file_service->SetConflictResolutionPolicy(
124        CONFLICT_RESOLUTION_POLICY_MANUAL);
125  }
126
127  service->Initialize(local_file_service.Pass(),
128                      remote_file_service.Pass());
129  return service;
130}
131
132}  // namespace sync_file_system
133