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_local_file_service(
32    scoped_ptr<LocalFileSyncService> mock_local_service) {
33  mock_local_file_service_ = mock_local_service.Pass();
34}
35
36void SyncFileSystemServiceFactory::set_mock_remote_file_service(
37    scoped_ptr<RemoteFileSyncService> mock_remote_service) {
38  mock_remote_file_service_ = mock_remote_service.Pass();
39}
40
41SyncFileSystemServiceFactory::SyncFileSystemServiceFactory()
42    : BrowserContextKeyedServiceFactory(
43        "SyncFileSystemService",
44        BrowserContextDependencyManager::GetInstance()) {
45  typedef std::set<BrowserContextKeyedServiceFactory*> FactorySet;
46  FactorySet factories;
47  factories.insert(extensions::ExtensionRegistryFactory::GetInstance());
48  RemoteFileSyncService::AppendDependsOnFactories(&factories);
49  for (FactorySet::iterator iter = factories.begin();
50       iter != factories.end();
51       ++iter) {
52    DependsOn(*iter);
53  }
54}
55
56SyncFileSystemServiceFactory::~SyncFileSystemServiceFactory() {}
57
58KeyedService* SyncFileSystemServiceFactory::BuildServiceInstanceFor(
59    content::BrowserContext* context) const {
60  Profile* profile = Profile::FromBrowserContext(context);
61
62  SyncFileSystemService* service = new SyncFileSystemService(profile);
63
64  scoped_ptr<LocalFileSyncService> local_file_service;
65  if (mock_local_file_service_)
66    local_file_service = mock_local_file_service_.Pass();
67  else
68    local_file_service = LocalFileSyncService::Create(profile);
69
70  scoped_ptr<RemoteFileSyncService> remote_file_service;
71  if (mock_remote_file_service_) {
72    remote_file_service = mock_remote_file_service_.Pass();
73  } else {
74    remote_file_service = RemoteFileSyncService::CreateForBrowserContext(
75        context, service->task_logger());
76  }
77
78  service->Initialize(local_file_service.Pass(),
79                      remote_file_service.Pass());
80  return service;
81}
82
83}  // namespace sync_file_system
84