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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
// Copyright (c) 2012 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.
#ifndef CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_SYSTEM_SERVICE_H_
#define CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_SYSTEM_SERVICE_H_
#include <string>
#include "base/callback.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/singleton.h"
#include "base/memory/weak_ptr.h"
#include "base/threading/sequenced_worker_pool.h"
#include "chrome/browser/chromeos/drive/drive_file_error.h"
#include "chrome/browser/profiles/profile_keyed_service.h"
#include "chrome/browser/profiles/profile_keyed_service_factory.h"
#include "sync/notifier/invalidation_handler.h"
class FilePath;
namespace google_apis {
class DriveServiceInterface;
class DriveUploader;
}
namespace drive {
class DriveCache;
class DriveDownloadHandler;
class DriveFileSystemInterface;
class DriveWebAppsRegistry;
class DriveSyncClient;
class DrivePrefetcher;
class EventLogger;
class FileWriteHelper;
class StaleCacheFilesRemover;
// DriveSystemService runs the Drive system, including the Drive file system
// implementation for the file manager, and some other sub systems.
//
// The class is essentially a container that manages lifetime of the objects
// that are used to run the Drive system. The DriveSystemService object is
// created per-profile.
class DriveSystemService : public ProfileKeyedService,
public syncer::InvalidationHandler {
public:
// test_drive_service, test_cache_root and test_file_system are used by tests
// to inject customized instances.
// Pass NULL or the empty value when not interested.
DriveSystemService(Profile* profile,
google_apis::DriveServiceInterface* test_drive_service,
const FilePath& test_cache_root,
DriveFileSystemInterface* test_file_system);
virtual ~DriveSystemService();
// Initializes the object. This function should be called before any
// other functions.
void Initialize();
// ProfileKeyedService override:
virtual void Shutdown() OVERRIDE;
google_apis::DriveServiceInterface* drive_service() {
return drive_service_.get();
}
DriveCache* cache() { return cache_.get(); }
DriveFileSystemInterface* file_system() { return file_system_.get(); }
FileWriteHelper* file_write_helper() { return file_write_helper_.get(); }
DriveDownloadHandler* download_handler() { return download_handler_.get(); }
google_apis::DriveUploader* uploader() { return uploader_.get(); }
DriveWebAppsRegistry* webapps_registry() { return webapps_registry_.get(); }
EventLogger* event_logger() { return event_logger_.get(); }
// Clears all the local cache files and in-memory data, and remounts the
// file system. |callback| is called with true when this operation is done
// successfully. Otherwise, |callback| is called with false.
// |callback| must not be null.
void ClearCacheAndRemountFileSystem(
const base::Callback<void(bool)>& callback);
// Reloads and remounts the file system.
void ReloadAndRemountFileSystem();
// syncer::InvalidationHandler implementation.
virtual void OnInvalidatorStateChange(
syncer::InvalidatorState state) OVERRIDE;
virtual void OnIncomingInvalidation(
const syncer::ObjectIdInvalidationMap& invalidation_map) OVERRIDE;
private:
// Used to destroy DriveCache with scoped_ptr_malloc_free.
struct ScopedPtrMallocDestroyCache {
public:
void operator()(DriveCache* cache) const;
};
// Returns true if Drive is enabled.
// Must be called on UI thread.
bool IsDriveEnabled();
// Registers remote file system proxy for drive mount point.
void AddDriveMountPoint();
// Unregisters drive mount point from File API.
void RemoveDriveMountPoint();
// Adds back the drive mount point. Used to implement ClearCache().
void AddBackDriveMountPoint(const base::Callback<void(bool)>& callback,
bool success);
// Called when cache initialization is done. Continues initialization if
// the cache initialization is successful.
void OnCacheInitialized(bool success);
// Disables Drive. Used to disable Drive when needed (ex. initialization of
// the Drive cache failed).
// Must be called on UI thread.
void DisableDrive();
friend class DriveSystemServiceFactory;
Profile* profile_;
// True if Drive is disabled due to initialization errors.
bool drive_disabled_;
// True once this is registered to listen to the Drive updates.
bool push_notification_registered_;
scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
scoped_ptr<EventLogger> event_logger_;
scoped_ptr_malloc<DriveCache, ScopedPtrMallocDestroyCache> cache_;
scoped_ptr<google_apis::DriveServiceInterface> drive_service_;
scoped_ptr<google_apis::DriveUploader> uploader_;
scoped_ptr<DriveWebAppsRegistry> webapps_registry_;
scoped_ptr<DriveFileSystemInterface> file_system_;
scoped_ptr<FileWriteHelper> file_write_helper_;
scoped_ptr<DriveDownloadHandler> download_handler_;
scoped_ptr<DriveSyncClient> sync_client_;
scoped_ptr<DrivePrefetcher> prefetcher_;
scoped_ptr<StaleCacheFilesRemover> stale_cache_files_remover_;
// Note: This should remain the last member so it'll be destroyed and
// invalidate its weak pointers before any other members are destroyed.
base::WeakPtrFactory<DriveSystemService> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(DriveSystemService);
};
// Singleton that owns all DriveSystemServices and associates them with
// Profiles.
class DriveSystemServiceFactory : public ProfileKeyedServiceFactory {
public:
// Factory function used by tests.
typedef base::Callback<DriveSystemService*(Profile* profile)> FactoryCallback;
// Returns the DriveSystemService for |profile|, creating it if it is not
// yet created.
//
// This function starts returning NULL if Drive is disabled, even if this
// function previously returns a non-NULL object. In other words, clients
// can assume that Drive is enabled if this function returns a non-NULL
// object.
static DriveSystemService* GetForProfile(Profile* profile);
// Similar to GetForProfile(), but returns the instance regardless of if Drive
// is enabled/disabled.
static DriveSystemService* GetForProfileRegardlessOfStates(Profile* profile);
// Returns the DriveSystemService that is already associated with |profile|,
// if it is not yet created it will return NULL.
//
// This function starts returning NULL if Drive is disabled. See also the
// comment at GetForProfile().
static DriveSystemService* FindForProfile(Profile* profile);
// Similar to FindForProfile(), but returns the instance regardless of if
// Drive is enabled/disabled.
static DriveSystemService* FindForProfileRegardlessOfStates(Profile* profile);
// Returns the DriveSystemServiceFactory instance.
static DriveSystemServiceFactory* GetInstance();
// Sets a factory function for tests.
static void SetFactoryForTest(const FactoryCallback& factory_for_test);
private:
friend struct DefaultSingletonTraits<DriveSystemServiceFactory>;
DriveSystemServiceFactory();
virtual ~DriveSystemServiceFactory();
// ProfileKeyedServiceFactory:
virtual ProfileKeyedService* BuildServiceInstanceFor(
Profile* profile) const OVERRIDE;
FactoryCallback factory_for_test_;
};
} // namespace drive
#endif // CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_SYSTEM_SERVICE_H_
|