OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/drive/drive_system_service.h" | 5 #include "chrome/browser/chromeos/drive/drive_system_service.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
10 #include "chrome/browser/browser_process.h" | 10 #include "chrome/browser/browser_process.h" |
11 #include "chrome/browser/chromeos/drive/drive_cache.h" | 11 #include "chrome/browser/chromeos/drive/drive_cache.h" |
12 #include "chrome/browser/chromeos/drive/drive_download_handler.h" | 12 #include "chrome/browser/chromeos/drive/drive_download_handler.h" |
13 #include "chrome/browser/chromeos/drive/drive_file_system.h" | 13 #include "chrome/browser/chromeos/drive/drive_file_system.h" |
14 #include "chrome/browser/chromeos/drive/drive_file_system_proxy.h" | 14 #include "chrome/browser/chromeos/drive/drive_file_system_proxy.h" |
15 #include "chrome/browser/chromeos/drive/drive_file_system_util.h" | 15 #include "chrome/browser/chromeos/drive/drive_file_system_util.h" |
16 #include "chrome/browser/chromeos/drive/drive_prefetcher.h" | 16 #include "chrome/browser/chromeos/drive/drive_prefetcher.h" |
17 #include "chrome/browser/chromeos/drive/drive_webapps_registry.h" | 17 #include "chrome/browser/chromeos/drive/drive_webapps_registry.h" |
18 #include "chrome/browser/chromeos/drive/file_write_helper.h" | 18 #include "chrome/browser/chromeos/drive/file_write_helper.h" |
19 #include "chrome/browser/chromeos/drive/stale_cache_files_remover.h" | 19 #include "chrome/browser/chromeos/drive/stale_cache_files_remover.h" |
20 #include "chrome/browser/chromeos/drive/sync_client.h" | 20 #include "chrome/browser/chromeos/drive/sync_client.h" |
21 #include "chrome/browser/download/download_service.h" | 21 #include "chrome/browser/download/download_service.h" |
22 #include "chrome/browser/download/download_service_factory.h" | 22 #include "chrome/browser/download/download_service_factory.h" |
23 #include "chrome/browser/download/download_util.h" | 23 #include "chrome/browser/download/download_util.h" |
24 #include "chrome/browser/google_apis/auth_service.h" | 24 #include "chrome/browser/google_apis/auth_service.h" |
25 #include "chrome/browser/google_apis/drive_api_service.h" | 25 #include "chrome/browser/google_apis/drive_api_service.h" |
26 #include "chrome/browser/google_apis/drive_api_util.h" | 26 #include "chrome/browser/google_apis/drive_api_util.h" |
| 27 #include "chrome/browser/google_apis/drive_notification_manager.h" |
| 28 #include "chrome/browser/google_apis/drive_notification_manager_factory.h" |
27 #include "chrome/browser/google_apis/event_logger.h" | 29 #include "chrome/browser/google_apis/event_logger.h" |
28 #include "chrome/browser/google_apis/gdata_wapi_service.h" | 30 #include "chrome/browser/google_apis/gdata_wapi_service.h" |
29 #include "chrome/browser/google_apis/gdata_wapi_url_generator.h" | 31 #include "chrome/browser/google_apis/gdata_wapi_url_generator.h" |
30 #include "chrome/browser/profiles/profile.h" | 32 #include "chrome/browser/profiles/profile.h" |
31 #include "chrome/browser/profiles/profile_dependency_manager.h" | 33 #include "chrome/browser/profiles/profile_dependency_manager.h" |
32 #include "chrome/browser/sync/profile_sync_service.h" | |
33 #include "chrome/browser/sync/profile_sync_service_factory.h" | 34 #include "chrome/browser/sync/profile_sync_service_factory.h" |
34 #include "chrome/common/chrome_switches.h" | 35 #include "chrome/common/chrome_switches.h" |
35 #include "chrome/common/chrome_version_info.h" | 36 #include "chrome/common/chrome_version_info.h" |
36 #include "chrome/common/pref_names.h" | 37 #include "chrome/common/pref_names.h" |
37 #include "content/public/browser/browser_context.h" | 38 #include "content/public/browser/browser_context.h" |
38 #include "content/public/browser/browser_thread.h" | 39 #include "content/public/browser/browser_thread.h" |
39 #include "content/public/browser/storage_partition.h" | 40 #include "content/public/browser/storage_partition.h" |
40 #include "google/cacheinvalidation/types.pb.h" | 41 #include "google/cacheinvalidation/types.pb.h" |
41 #include "webkit/fileapi/external_mount_points.h" | 42 #include "webkit/fileapi/external_mount_points.h" |
42 #include "webkit/user_agent/user_agent_util.h" | 43 #include "webkit/user_agent/user_agent_util.h" |
43 | 44 |
44 using content::BrowserContext; | 45 using content::BrowserContext; |
45 using content::BrowserThread; | 46 using content::BrowserThread; |
46 | 47 |
47 namespace drive { | 48 namespace drive { |
48 namespace { | 49 namespace { |
49 | 50 |
50 static const size_t kEventLogHistorySize = 100; | 51 static const size_t kEventLogHistorySize = 100; |
51 | 52 |
52 // The sync invalidation object ID for Google Drive. | |
53 const char kDriveInvalidationObjectId[] = "CHANGELOG"; | |
54 | |
55 // Returns true if Drive is enabled for the given Profile. | 53 // Returns true if Drive is enabled for the given Profile. |
56 bool IsDriveEnabledForProfile(Profile* profile) { | 54 bool IsDriveEnabledForProfile(Profile* profile) { |
57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 55 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
58 | 56 |
59 if (!google_apis::AuthService::CanAuthenticate(profile)) | 57 if (!google_apis::AuthService::CanAuthenticate(profile)) |
60 return false; | 58 return false; |
61 | 59 |
62 // Disable Drive if preference is set. This can happen with commandline flag | 60 // Disable Drive if preference is set. This can happen with commandline flag |
63 // --disable-gdata or enterprise policy, or probably with user settings too | 61 // --disable-gdata or enterprise policy, or probably with user settings too |
64 // in the future. | 62 // in the future. |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 | 97 |
100 } // namespace | 98 } // namespace |
101 | 99 |
102 DriveSystemService::DriveSystemService( | 100 DriveSystemService::DriveSystemService( |
103 Profile* profile, | 101 Profile* profile, |
104 google_apis::DriveServiceInterface* test_drive_service, | 102 google_apis::DriveServiceInterface* test_drive_service, |
105 const base::FilePath& test_cache_root, | 103 const base::FilePath& test_cache_root, |
106 DriveFileSystemInterface* test_file_system) | 104 DriveFileSystemInterface* test_file_system) |
107 : profile_(profile), | 105 : profile_(profile), |
108 drive_disabled_(false), | 106 drive_disabled_(false), |
109 push_notification_registered_(false), | |
110 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { | 107 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
111 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 108 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
112 base::SequencedWorkerPool* blocking_pool = BrowserThread::GetBlockingPool(); | 109 base::SequencedWorkerPool* blocking_pool = BrowserThread::GetBlockingPool(); |
113 blocking_task_runner_ = blocking_pool->GetSequencedTaskRunner( | 110 blocking_task_runner_ = blocking_pool->GetSequencedTaskRunner( |
114 blocking_pool->GetSequenceToken()); | 111 blocking_pool->GetSequenceToken()); |
115 | 112 |
116 event_logger_.reset(new google_apis::EventLogger(kEventLogHistorySize)); | 113 event_logger_.reset(new google_apis::EventLogger(kEventLogHistorySize)); |
117 if (test_drive_service) { | 114 if (test_drive_service) { |
118 drive_service_.reset(test_drive_service); | 115 drive_service_.reset(test_drive_service); |
119 } else if (google_apis::util::IsDriveV2ApiEnabled()) { | 116 } else if (google_apis::util::IsDriveV2ApiEnabled()) { |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 drive_service_->Initialize(profile_); | 166 drive_service_->Initialize(profile_); |
170 file_system_->Initialize(); | 167 file_system_->Initialize(); |
171 cache_->RequestInitialize( | 168 cache_->RequestInitialize( |
172 base::Bind(&DriveSystemService::InitializeAfterCacheInitialized, | 169 base::Bind(&DriveSystemService::InitializeAfterCacheInitialized, |
173 weak_ptr_factory_.GetWeakPtr())); | 170 weak_ptr_factory_.GetWeakPtr())); |
174 } | 171 } |
175 | 172 |
176 void DriveSystemService::Shutdown() { | 173 void DriveSystemService::Shutdown() { |
177 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 174 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
178 | 175 |
179 ProfileSyncService* profile_sync_service = | 176 google_apis::DriveNotificationManager* drive_notification_manager = |
180 profile_ ? ProfileSyncServiceFactory::GetForProfile(profile_) : NULL; | 177 google_apis::DriveNotificationManagerFactory::GetForProfile(profile_); |
181 if (profile_sync_service && push_notification_registered_) { | 178 if (drive_notification_manager) |
182 // TODO(kochi): Once DriveSystemService gets started / stopped at runtime, | 179 drive_notification_manager->RemoveObserver(this); |
183 // this ID needs to be unregistered *before* the handler is unregistered | |
184 // as ID persists across browser restarts. | |
185 if (!IsDriveEnabledForProfile(profile_)) { | |
186 profile_sync_service->UpdateRegisteredInvalidationIds( | |
187 this, syncer::ObjectIdSet()); | |
188 } | |
189 profile_sync_service->UnregisterInvalidationHandler(this); | |
190 push_notification_registered_ = false; | |
191 } | |
192 | 180 |
193 RemoveDriveMountPoint(); | 181 RemoveDriveMountPoint(); |
194 } | 182 } |
195 | 183 |
196 void DriveSystemService::AddObserver(DriveSystemServiceObserver* observer) { | 184 void DriveSystemService::AddObserver(DriveSystemServiceObserver* observer) { |
197 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 185 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
198 observers_.AddObserver(observer); | 186 observers_.AddObserver(observer); |
199 } | 187 } |
200 | 188 |
201 void DriveSystemService::RemoveObserver(DriveSystemServiceObserver* observer) { | 189 void DriveSystemService::RemoveObserver(DriveSystemServiceObserver* observer) { |
202 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 190 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
203 observers_.RemoveObserver(observer); | 191 observers_.RemoveObserver(observer); |
204 } | 192 } |
205 | 193 |
| 194 void DriveSystemService::OnNotificationReceived() { |
| 195 file_system_->CheckForUpdates(); |
| 196 } |
| 197 |
206 bool DriveSystemService::IsDriveEnabled() { | 198 bool DriveSystemService::IsDriveEnabled() { |
207 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 199 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
208 | 200 |
209 if (!IsDriveEnabledForProfile(profile_)) | 201 if (!IsDriveEnabledForProfile(profile_)) |
210 return false; | 202 return false; |
211 | 203 |
212 // Drive may be disabled for cache initialization failure, etc. | 204 // Drive may be disabled for cache initialization failure, etc. |
213 if (drive_disabled_) | 205 if (drive_disabled_) |
214 return false; | 206 return false; |
215 | 207 |
216 return true; | 208 return true; |
217 } | 209 } |
218 | 210 |
219 void DriveSystemService::OnInvalidatorStateChange( | |
220 syncer::InvalidatorState state) { | |
221 DVLOG(1) << "InvalidatorState changed to " << state; | |
222 } | |
223 | |
224 void DriveSystemService::OnIncomingInvalidation( | |
225 const syncer::ObjectIdInvalidationMap& invalidation_map) { | |
226 DCHECK_EQ(1U, invalidation_map.size()); | |
227 const invalidation::ObjectId object_id( | |
228 ipc::invalidation::ObjectSource::COSMO_CHANGELOG, | |
229 kDriveInvalidationObjectId); | |
230 DCHECK_EQ(1U, invalidation_map.count(object_id)); | |
231 | |
232 file_system_->CheckForUpdates(); | |
233 } | |
234 | |
235 void DriveSystemService::ClearCacheAndRemountFileSystem( | 211 void DriveSystemService::ClearCacheAndRemountFileSystem( |
236 const base::Callback<void(bool)>& callback) { | 212 const base::Callback<void(bool)>& callback) { |
237 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 213 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
238 DCHECK(!callback.is_null()); | 214 DCHECK(!callback.is_null()); |
239 | 215 |
240 RemoveDriveMountPoint(); | 216 RemoveDriveMountPoint(); |
241 cache_->ClearAll(base::Bind( | 217 cache_->ClearAll(base::Bind( |
242 &DriveSystemService::ReinitializeResourceMetadataAfterClearCache, | 218 &DriveSystemService::ReinitializeResourceMetadataAfterClearCache, |
243 weak_ptr_factory_.GetWeakPtr(), | 219 weak_ptr_factory_.GetWeakPtr(), |
244 callback)); | 220 callback)); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
280 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 256 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
281 | 257 |
282 RemoveDriveMountPoint(); | 258 RemoveDriveMountPoint(); |
283 file_system_->Reload(); | 259 file_system_->Reload(); |
284 | 260 |
285 // Reload() is asynchronous. But we can add back the mount point right away | 261 // Reload() is asynchronous. But we can add back the mount point right away |
286 // because every operation waits until loading is complete. | 262 // because every operation waits until loading is complete. |
287 AddDriveMountPoint(); | 263 AddDriveMountPoint(); |
288 } | 264 } |
289 | 265 |
290 bool DriveSystemService::PushNotificationEnabled() { | |
291 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
292 DCHECK(profile_); | |
293 | |
294 ProfileSyncService* profile_sync_service = | |
295 ProfileSyncServiceFactory::GetForProfile(profile_); | |
296 if (!profile_sync_service) | |
297 return false; | |
298 | |
299 return (profile_sync_service->GetInvalidatorState() == | |
300 syncer::INVALIDATIONS_ENABLED); | |
301 } | |
302 | |
303 void DriveSystemService::AddDriveMountPoint() { | 266 void DriveSystemService::AddDriveMountPoint() { |
304 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 267 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
305 DCHECK(!file_system_proxy_.get()); | 268 DCHECK(!file_system_proxy_.get()); |
306 | 269 |
307 const base::FilePath drive_mount_point = util::GetDriveMountPointPath(); | 270 const base::FilePath drive_mount_point = util::GetDriveMountPointPath(); |
308 fileapi::ExternalMountPoints* mount_points = | 271 fileapi::ExternalMountPoints* mount_points = |
309 BrowserContext::GetMountPoints(profile_); | 272 BrowserContext::GetMountPoints(profile_); |
310 DCHECK(mount_points); | 273 DCHECK(mount_points); |
311 | 274 |
312 file_system_proxy_ = new DriveFileSystemProxy(file_system_.get()); | 275 file_system_proxy_ = new DriveFileSystemProxy(file_system_.get()); |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
372 } | 335 } |
373 | 336 |
374 content::DownloadManager* download_manager = | 337 content::DownloadManager* download_manager = |
375 g_browser_process->download_status_updater() ? | 338 g_browser_process->download_status_updater() ? |
376 BrowserContext::GetDownloadManager(profile_) : NULL; | 339 BrowserContext::GetDownloadManager(profile_) : NULL; |
377 download_handler_->Initialize( | 340 download_handler_->Initialize( |
378 download_manager, | 341 download_manager, |
379 cache_->GetCacheDirectoryPath(DriveCache::CACHE_TYPE_TMP_DOWNLOADS)); | 342 cache_->GetCacheDirectoryPath(DriveCache::CACHE_TYPE_TMP_DOWNLOADS)); |
380 | 343 |
381 // Register for Google Drive invalidation notifications. | 344 // Register for Google Drive invalidation notifications. |
382 ProfileSyncService* profile_sync_service = | 345 google_apis::DriveNotificationManager* drive_notification_manager = |
383 profile_ ? ProfileSyncServiceFactory::GetForProfile(profile_) : NULL; | 346 google_apis::DriveNotificationManagerFactory::GetForProfile(profile_); |
384 if (profile_sync_service) { | 347 if (drive_notification_manager) |
385 DCHECK(!push_notification_registered_); | 348 drive_notification_manager->AddObserver(this); |
386 profile_sync_service->RegisterInvalidationHandler(this); | |
387 syncer::ObjectIdSet ids; | |
388 ids.insert(invalidation::ObjectId( | |
389 ipc::invalidation::ObjectSource::COSMO_CHANGELOG, | |
390 kDriveInvalidationObjectId)); | |
391 profile_sync_service->UpdateRegisteredInvalidationIds(this, ids); | |
392 push_notification_registered_ = true; | |
393 } | |
394 | 349 |
395 AddDriveMountPoint(); | 350 AddDriveMountPoint(); |
396 } | 351 } |
397 | 352 |
398 void DriveSystemService::DisableDrive() { | 353 void DriveSystemService::DisableDrive() { |
399 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 354 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
400 | 355 |
401 drive_disabled_ = true; | 356 drive_disabled_ = true; |
402 // Change the download directory to the default value if the download | 357 // Change the download directory to the default value if the download |
403 // destination is set to under Drive mount point. | 358 // destination is set to under Drive mount point. |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
452 | 407 |
453 // static | 408 // static |
454 void DriveSystemServiceFactory::SetFactoryForTest( | 409 void DriveSystemServiceFactory::SetFactoryForTest( |
455 const FactoryCallback& factory_for_test) { | 410 const FactoryCallback& factory_for_test) { |
456 GetInstance()->factory_for_test_ = factory_for_test; | 411 GetInstance()->factory_for_test_ = factory_for_test; |
457 } | 412 } |
458 | 413 |
459 DriveSystemServiceFactory::DriveSystemServiceFactory() | 414 DriveSystemServiceFactory::DriveSystemServiceFactory() |
460 : ProfileKeyedServiceFactory("DriveSystemService", | 415 : ProfileKeyedServiceFactory("DriveSystemService", |
461 ProfileDependencyManager::GetInstance()) { | 416 ProfileDependencyManager::GetInstance()) { |
462 DependsOn(ProfileSyncServiceFactory::GetInstance()); | 417 DependsOn(google_apis::DriveNotificationManagerFactory::GetInstance()); |
463 DependsOn(DownloadServiceFactory::GetInstance()); | 418 DependsOn(DownloadServiceFactory::GetInstance()); |
464 } | 419 } |
465 | 420 |
466 DriveSystemServiceFactory::~DriveSystemServiceFactory() { | 421 DriveSystemServiceFactory::~DriveSystemServiceFactory() { |
467 } | 422 } |
468 | 423 |
469 ProfileKeyedService* DriveSystemServiceFactory::BuildServiceInstanceFor( | 424 ProfileKeyedService* DriveSystemServiceFactory::BuildServiceInstanceFor( |
470 Profile* profile) const { | 425 Profile* profile) const { |
471 DriveSystemService* service = NULL; | 426 DriveSystemService* service = NULL; |
472 if (factory_for_test_.is_null()) | 427 if (factory_for_test_.is_null()) |
473 service = new DriveSystemService(profile, NULL, base::FilePath(), NULL); | 428 service = new DriveSystemService(profile, NULL, base::FilePath(), NULL); |
474 else | 429 else |
475 service = factory_for_test_.Run(profile); | 430 service = factory_for_test_.Run(profile); |
476 | 431 |
477 service->Initialize(); | 432 service->Initialize(); |
478 return service; | 433 return service; |
479 } | 434 } |
480 | 435 |
481 } // namespace drive | 436 } // namespace drive |
OLD | NEW |