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