sync_file_system_service_factory.cc revision 116680a4aac90f2aa7413d9095a592090648e557
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 <set>
8
9#include "base/command_line.h"
10#include "chrome/browser/profiles/profile.h"
11#include "chrome/browser/sync_file_system/local/local_file_sync_service.h"
12#include "chrome/browser/sync_file_system/sync_file_system_service.h"
13#include "chrome/browser/sync_file_system/syncable_file_system_util.h"
14#include "components/keyed_service/content/browser_context_dependency_manager.h"
15#include "extensions/browser/extension_registry_factory.h"
16
17namespace sync_file_system {
18
19// static
20SyncFileSystemService* SyncFileSystemServiceFactory::GetForProfile(
21    Profile* profile) {
22  return static_cast<SyncFileSystemService*>(
23      GetInstance()->GetServiceForBrowserContext(profile, true));
24}
25
26// static
27SyncFileSystemServiceFactory* SyncFileSystemServiceFactory::GetInstance() {
28  return Singleton<SyncFileSystemServiceFactory>::get();
29}
30
31void SyncFileSystemServiceFactory::set_mock_remote_file_service(
32    scoped_ptr<RemoteFileSyncService> mock_remote_service) {
33  mock_remote_file_service_ = mock_remote_service.Pass();
34}
35
36SyncFileSystemServiceFactory::SyncFileSystemServiceFactory()
37    : BrowserContextKeyedServiceFactory(
38        "SyncFileSystemService",
39        BrowserContextDependencyManager::GetInstance()) {
40  typedef std::set<BrowserContextKeyedServiceFactory*> FactorySet;
41  FactorySet factories;
42  factories.insert(extensions::ExtensionRegistryFactory::GetInstance());
43  RemoteFileSyncService::AppendDependsOnFactories(
44      RemoteFileSyncService::V1, &factories);
45  RemoteFileSyncService::AppendDependsOnFactories(
46      RemoteFileSyncService::V2, &factories);
47  for (FactorySet::iterator iter = factories.begin();
48       iter != factories.end();
49       ++iter) {
50    DependsOn(*iter);
51  }
52}
53
54SyncFileSystemServiceFactory::~SyncFileSystemServiceFactory() {}
55
56KeyedService* SyncFileSystemServiceFactory::BuildServiceInstanceFor(
57    content::BrowserContext* context) const {
58  Profile* profile = Profile::FromBrowserContext(context);
59
60  SyncFileSystemService* service = new SyncFileSystemService(profile);
61
62  scoped_ptr<LocalFileSyncService> local_file_service =
63      LocalFileSyncService::Create(profile);
64
65  scoped_ptr<RemoteFileSyncService> remote_file_service;
66  if (mock_remote_file_service_) {
67    remote_file_service = mock_remote_file_service_.Pass();
68  } else if (IsV2Enabled()) {
69    remote_file_service = RemoteFileSyncService::CreateForBrowserContext(
70        RemoteFileSyncService::V2, context, service->task_logger());
71  } else {
72    remote_file_service = RemoteFileSyncService::CreateForBrowserContext(
73        RemoteFileSyncService::V1, context, service->task_logger());
74  }
75
76  service->Initialize(local_file_service.Pass(),
77                      remote_file_service.Pass());
78  return service;
79}
80
81}  // namespace sync_file_system
82