OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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/chromeos/gdata/gdata_system_service.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/bind_helpers.h" | |
9 #include "chrome/browser/browser_process.h" | |
10 #include "chrome/browser/chromeos/gdata/drive_api_service.h" | |
11 #include "chrome/browser/chromeos/gdata/drive_file_system.h" | |
12 #include "chrome/browser/chromeos/gdata/drive_file_system_proxy.h" | |
13 #include "chrome/browser/chromeos/gdata/drive_webapps_registry.h" | |
14 #include "chrome/browser/chromeos/gdata/file_write_helper.h" | |
15 #include "chrome/browser/chromeos/gdata/gdata_download_observer.h" | |
16 #include "chrome/browser/chromeos/gdata/gdata_sync_client.h" | |
17 #include "chrome/browser/chromeos/gdata/gdata_uploader.h" | |
18 #include "chrome/browser/chromeos/gdata/gdata_util.h" | |
19 #include "chrome/browser/chromeos/gdata/gdata_wapi_service.h" | |
20 #include "chrome/browser/download/download_service.h" | |
21 #include "chrome/browser/download/download_service_factory.h" | |
22 #include "chrome/browser/profiles/profile.h" | |
23 #include "chrome/browser/profiles/profile_dependency_manager.h" | |
24 #include "content/public/browser/browser_context.h" | |
25 #include "content/public/browser/browser_thread.h" | |
26 #include "webkit/fileapi/file_system_context.h" | |
27 #include "webkit/fileapi/file_system_mount_point_provider.h" | |
28 | |
29 using content::BrowserContext; | |
30 using content::BrowserThread; | |
31 | |
32 namespace gdata { | |
33 namespace { | |
34 | |
35 // Used in test to setup system service. | |
36 DriveServiceInterface* g_test_drive_service = NULL; | |
37 const std::string* g_test_cache_root = NULL; | |
38 | |
39 } // namespace | |
40 | |
41 GDataSystemService::GDataSystemService(Profile* profile) | |
42 : profile_(profile), | |
43 cache_(NULL), | |
44 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { | |
45 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
46 base::SequencedWorkerPool* blocking_pool = BrowserThread::GetBlockingPool(); | |
47 blocking_task_runner_ = blocking_pool->GetSequencedTaskRunner( | |
48 blocking_pool->GetSequenceToken()); | |
49 } | |
50 | |
51 GDataSystemService::~GDataSystemService() { | |
52 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
53 cache_->DestroyOnUIThread(); | |
54 } | |
55 | |
56 void GDataSystemService::Initialize( | |
57 DriveServiceInterface* drive_service, | |
58 const FilePath& cache_root) { | |
59 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
60 | |
61 drive_service_.reset(drive_service); | |
62 cache_ = DriveCache::CreateDriveCacheOnUIThread( | |
63 cache_root, | |
64 blocking_task_runner_); | |
65 uploader_.reset(new GDataUploader(drive_service_.get())); | |
66 webapps_registry_.reset(new DriveWebAppsRegistry); | |
67 file_system_.reset(new DriveFileSystem(profile_, | |
68 cache(), | |
69 drive_service_.get(), | |
70 uploader(), | |
71 webapps_registry(), | |
72 blocking_task_runner_)); | |
73 file_write_helper_.reset(new FileWriteHelper(file_system())); | |
74 download_observer_.reset(new GDataDownloadObserver(uploader(), | |
75 file_system())); | |
76 sync_client_.reset(new GDataSyncClient(profile_, file_system(), cache())); | |
77 | |
78 sync_client_->Initialize(); | |
79 file_system_->Initialize(); | |
80 cache_->RequestInitializeOnUIThread(); | |
81 | |
82 content::DownloadManager* download_manager = | |
83 g_browser_process->download_status_updater() ? | |
84 BrowserContext::GetDownloadManager(profile_) : NULL; | |
85 download_observer_->Initialize( | |
86 download_manager, | |
87 cache_->GetCacheDirectoryPath( | |
88 DriveCache::CACHE_TYPE_TMP_DOWNLOADS)); | |
89 | |
90 AddDriveMountPoint(); | |
91 } | |
92 | |
93 void GDataSystemService::Shutdown() { | |
94 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
95 RemoveDriveMountPoint(); | |
96 | |
97 // Shut down the member objects in the reverse order of creation. | |
98 sync_client_.reset(); | |
99 download_observer_.reset(); | |
100 file_write_helper_.reset(); | |
101 file_system_.reset(); | |
102 webapps_registry_.reset(); | |
103 uploader_.reset(); | |
104 drive_service_.reset(); | |
105 } | |
106 | |
107 void GDataSystemService::ClearCacheAndRemountFileSystem( | |
108 const base::Callback<void(bool)>& callback) { | |
109 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
110 | |
111 RemoveDriveMountPoint(); | |
112 drive_service()->CancelAll(); | |
113 cache_->ClearAllOnUIThread( | |
114 base::Bind(&GDataSystemService::AddBackDriveMountPoint, | |
115 weak_ptr_factory_.GetWeakPtr(), | |
116 callback)); | |
117 } | |
118 | |
119 void GDataSystemService::AddBackDriveMountPoint( | |
120 const base::Callback<void(bool)>& callback, | |
121 DriveFileError error, | |
122 const FilePath& file_path) { | |
123 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
124 | |
125 AddDriveMountPoint(); | |
126 | |
127 if (!callback.is_null()) | |
128 callback.Run(error == DRIVE_FILE_OK); | |
129 } | |
130 | |
131 void GDataSystemService::AddDriveMountPoint() { | |
132 if (!gdata::util::IsGDataAvailable(profile_)) | |
133 return; | |
134 | |
135 const FilePath mount_point = gdata::util::GetGDataMountPointPath(); | |
136 fileapi::ExternalFileSystemMountPointProvider* provider = | |
137 BrowserContext::GetFileSystemContext(profile_)->external_provider(); | |
138 if (provider && !provider->HasMountPoint(mount_point)) { | |
139 provider->AddRemoteMountPoint( | |
140 mount_point, | |
141 new DriveFileSystemProxy(file_system_.get())); | |
142 } | |
143 | |
144 file_system_->Initialize(); | |
145 file_system_->NotifyFileSystemMounted(); | |
146 } | |
147 | |
148 void GDataSystemService::RemoveDriveMountPoint() { | |
149 file_system_->NotifyFileSystemToBeUnmounted(); | |
150 file_system_->StopUpdates(); | |
151 | |
152 const FilePath mount_point = gdata::util::GetGDataMountPointPath(); | |
153 fileapi::ExternalFileSystemMountPointProvider* provider = | |
154 BrowserContext::GetFileSystemContext(profile_)->external_provider(); | |
155 if (provider && provider->HasMountPoint(mount_point)) | |
156 provider->RemoveMountPoint(mount_point); | |
157 } | |
158 | |
159 //===================== GDataSystemServiceFactory ============================= | |
160 | |
161 // static | |
162 GDataSystemService* GDataSystemServiceFactory::GetForProfile( | |
163 Profile* profile) { | |
164 return static_cast<GDataSystemService*>( | |
165 GetInstance()->GetServiceForProfile(profile, true)); | |
166 } | |
167 | |
168 // static | |
169 GDataSystemService* GDataSystemServiceFactory::FindForProfile( | |
170 Profile* profile) { | |
171 return static_cast<GDataSystemService*>( | |
172 GetInstance()->GetServiceForProfile(profile, false)); | |
173 } | |
174 | |
175 // static | |
176 GDataSystemServiceFactory* GDataSystemServiceFactory::GetInstance() { | |
177 return Singleton<GDataSystemServiceFactory>::get(); | |
178 } | |
179 | |
180 GDataSystemServiceFactory::GDataSystemServiceFactory() | |
181 : ProfileKeyedServiceFactory("GDataSystemService", | |
182 ProfileDependencyManager::GetInstance()) { | |
183 DependsOn(DownloadServiceFactory::GetInstance()); | |
184 } | |
185 | |
186 GDataSystemServiceFactory::~GDataSystemServiceFactory() { | |
187 } | |
188 | |
189 // static | |
190 void GDataSystemServiceFactory::set_drive_service_for_test( | |
191 DriveServiceInterface* drive_service) { | |
192 if (g_test_drive_service) | |
193 delete g_test_drive_service; | |
194 g_test_drive_service = drive_service; | |
195 } | |
196 | |
197 // static | |
198 void GDataSystemServiceFactory::set_cache_root_for_test( | |
199 const std::string& cache_root) { | |
200 if (g_test_cache_root) | |
201 delete g_test_cache_root; | |
202 g_test_cache_root = !cache_root.empty() ? new std::string(cache_root) : NULL; | |
203 } | |
204 | |
205 ProfileKeyedService* GDataSystemServiceFactory::BuildServiceInstanceFor( | |
206 Profile* profile) const { | |
207 GDataSystemService* service = new GDataSystemService(profile); | |
208 | |
209 DriveServiceInterface* drive_service = g_test_drive_service; | |
210 g_test_drive_service = NULL; | |
211 if (!drive_service) { | |
212 if (util::IsDriveV2ApiEnabled()) | |
213 drive_service = new DriveAPIService(); | |
214 else | |
215 drive_service = new GDataWapiService(); | |
216 } | |
217 | |
218 FilePath cache_root = | |
219 g_test_cache_root ? FilePath(*g_test_cache_root) : | |
220 DriveCache::GetCacheRootPath(profile); | |
221 delete g_test_cache_root; | |
222 g_test_cache_root = NULL; | |
223 | |
224 service->Initialize(drive_service, cache_root); | |
225 return service; | |
226 } | |
227 | |
228 } // namespace gdata | |
OLD | NEW |