| 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/extensions/api/media_galleries_private/media_galleries_
private_api.h" | 5 #include "chrome/browser/extensions/api/media_galleries_private/media_galleries_
private_api.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
| 11 #include "base/location.h" | 11 #include "base/location.h" |
| 12 #include "base/prefs/pref_service.h" | 12 #include "base/prefs/pref_service.h" |
| 13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 14 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
| 15 #include "chrome/browser/extensions/api/media_galleries_private/gallery_watch_ma
nager.h" | 15 #include "chrome/browser/extensions/api/media_galleries_private/gallery_watch_ma
nager.h" |
| 16 #include "chrome/browser/extensions/api/media_galleries_private/media_galleries_
private_event_router.h" | 16 #include "chrome/browser/extensions/api/media_galleries_private/media_galleries_
private_event_router.h" |
| 17 #include "chrome/browser/extensions/event_names.h" | 17 #include "chrome/browser/extensions/event_names.h" |
| 18 #include "chrome/browser/extensions/event_router.h" | 18 #include "chrome/browser/extensions/event_router.h" |
| 19 #include "chrome/browser/extensions/extension_function.h" | 19 #include "chrome/browser/extensions/extension_function.h" |
| 20 #include "chrome/browser/extensions/extension_service.h" | 20 #include "chrome/browser/extensions/extension_service.h" |
| 21 #include "chrome/browser/extensions/extension_system.h" | 21 #include "chrome/browser/extensions/extension_system.h" |
| 22 #include "chrome/browser/extensions/media_galleries_handler.h" | 22 #include "chrome/browser/extensions/media_galleries_handler.h" |
| 23 #include "chrome/browser/media_galleries/media_file_system_registry.h" | 23 #include "chrome/browser/media_galleries/media_file_system_registry.h" |
| 24 #include "chrome/browser/media_galleries/media_galleries_preferences.h" | 24 #include "chrome/browser/media_galleries/media_galleries_preferences.h" |
| 25 #include "chrome/browser/profiles/profile.h" | 25 #include "chrome/browser/profiles/profile.h" |
| 26 #include "chrome/browser/storage_monitor/storage_monitor.h" |
| 26 #include "content/public/browser/browser_thread.h" | 27 #include "content/public/browser/browser_thread.h" |
| 27 #include "content/public/browser/render_view_host.h" | 28 #include "content/public/browser/render_view_host.h" |
| 28 | 29 |
| 29 using base::DictionaryValue; | 30 using base::DictionaryValue; |
| 30 using base::ListValue; | 31 using base::ListValue; |
| 31 | 32 |
| 32 namespace extensions { | 33 namespace extensions { |
| 33 | 34 |
| 34 namespace AddGalleryWatch = | 35 namespace AddGalleryWatch = |
| 35 extensions::api::media_galleries_private::AddGalleryWatch; | 36 extensions::api::media_galleries_private::AddGalleryWatch; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 53 // Key for Media Gallery Permission Value. | 54 // Key for Media Gallery Permission Value. |
| 54 const char kMediaGalleryHasPermissionKey[] = "has_permission"; | 55 const char kMediaGalleryHasPermissionKey[] = "has_permission"; |
| 55 | 56 |
| 56 // Handles the profile shutdown event on the file thread to clean up | 57 // Handles the profile shutdown event on the file thread to clean up |
| 57 // GalleryWatchManager. | 58 // GalleryWatchManager. |
| 58 void HandleProfileShutdownOnFileThread(void* profile_id) { | 59 void HandleProfileShutdownOnFileThread(void* profile_id) { |
| 59 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); | 60 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); |
| 60 GalleryWatchManager::OnProfileShutdown(profile_id); | 61 GalleryWatchManager::OnProfileShutdown(profile_id); |
| 61 } | 62 } |
| 62 | 63 |
| 63 // Gets the |gallery_file_path| and |gallery_pref_id| of the gallery specified | |
| 64 // by the |gallery_id|. Returns true and set |gallery_file_path| and | |
| 65 // |gallery_pref_id| if the |gallery_id| is valid and returns false otherwise. | |
| 66 bool GetGalleryFilePathAndId(const std::string& gallery_id, | |
| 67 Profile* profile, | |
| 68 const Extension* extension, | |
| 69 base::FilePath* gallery_file_path, | |
| 70 chrome::MediaGalleryPrefId* gallery_pref_id) { | |
| 71 chrome::MediaGalleryPrefId pref_id; | |
| 72 if (!base::StringToUint64(gallery_id, &pref_id)) | |
| 73 return false; | |
| 74 chrome::MediaFileSystemRegistry* registry = | |
| 75 g_browser_process->media_file_system_registry(); | |
| 76 base::FilePath file_path( | |
| 77 registry->GetPreferences(profile)->LookUpGalleryPathForExtension( | |
| 78 pref_id, extension, false)); | |
| 79 if (file_path.empty()) | |
| 80 return false; | |
| 81 *gallery_pref_id = pref_id; | |
| 82 *gallery_file_path = file_path; | |
| 83 return true; | |
| 84 } | |
| 85 | |
| 86 bool GetMediaGalleryPermissionFromDictionary( | 64 bool GetMediaGalleryPermissionFromDictionary( |
| 87 const DictionaryValue* dict, | 65 const DictionaryValue* dict, |
| 88 chrome::MediaGalleryPermission* out_permission) { | 66 chrome::MediaGalleryPermission* out_permission) { |
| 89 std::string string_id; | 67 std::string string_id; |
| 90 if (dict->GetString(kMediaGalleryIdKey, &string_id) && | 68 if (dict->GetString(kMediaGalleryIdKey, &string_id) && |
| 91 base::StringToUint64(string_id, &out_permission->pref_id) && | 69 base::StringToUint64(string_id, &out_permission->pref_id) && |
| 92 dict->GetBoolean(kMediaGalleryHasPermissionKey, | 70 dict->GetBoolean(kMediaGalleryHasPermissionKey, |
| 93 &out_permission->has_permission)) { | 71 &out_permission->has_permission)) { |
| 94 return true; | 72 return true; |
| 95 } | 73 } |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 | 275 |
| 298 bool MediaGalleriesPrivateAddGalleryWatchFunction::RunImpl() { | 276 bool MediaGalleriesPrivateAddGalleryWatchFunction::RunImpl() { |
| 299 DCHECK(profile_); | 277 DCHECK(profile_); |
| 300 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 278 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 301 if (!render_view_host() || !render_view_host()->GetProcess()) | 279 if (!render_view_host() || !render_view_host()->GetProcess()) |
| 302 return false; | 280 return false; |
| 303 | 281 |
| 304 scoped_ptr<AddGalleryWatch::Params> params( | 282 scoped_ptr<AddGalleryWatch::Params> params( |
| 305 AddGalleryWatch::Params::Create(*args_)); | 283 AddGalleryWatch::Params::Create(*args_)); |
| 306 EXTENSION_FUNCTION_VALIDATE(params.get()); | 284 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 307 base::FilePath gallery_file_path; | 285 |
| 308 chrome::MediaGalleryPrefId gallery_pref_id = 0; | 286 chrome::MediaGalleryPrefId pref_id = chrome::kInvalidMediaGalleryPrefId; |
| 309 if (!GetGalleryFilePathAndId(params->gallery_id, profile_, GetExtension(), | 287 if (!base::StringToUint64(params->gallery_id, &pref_id) || |
| 310 &gallery_file_path, &gallery_pref_id)) { | 288 pref_id == chrome::kInvalidMediaGalleryPrefId) { |
| 311 error_ = kInvalidGalleryIDError; | 289 error_ = kInvalidGalleryIDError; |
| 312 return false; | 290 return false; |
| 313 } | 291 } |
| 314 | 292 |
| 293 chrome::StorageMonitor::GetInstance()->Initialize(base::Bind( |
| 294 &MediaGalleriesPrivateAddGalleryWatchFunction::OnStorageMonitorInit, |
| 295 this, pref_id)); |
| 296 return true; |
| 297 } |
| 298 |
| 299 void MediaGalleriesPrivateAddGalleryWatchFunction::OnStorageMonitorInit( |
| 300 chrome::MediaGalleryPrefId pref_id) { |
| 301 chrome::MediaFileSystemRegistry* registry = |
| 302 g_browser_process->media_file_system_registry(); |
| 303 chrome::MediaGalleriesPreferences* preferences = |
| 304 registry->GetPreferences(profile_); |
| 305 base::FilePath file_path(preferences->LookUpGalleryPathForExtension( |
| 306 pref_id, GetExtension(), false)); |
| 307 |
| 308 if (file_path.empty()) { |
| 309 error_ = kInvalidGalleryIDError; |
| 310 HandleResponse(pref_id, false); |
| 311 return; |
| 312 } |
| 313 |
| 315 #if defined(OS_WIN) | 314 #if defined(OS_WIN) |
| 316 MediaGalleriesPrivateEventRouter* router = | 315 MediaGalleriesPrivateEventRouter* router = |
| 317 MediaGalleriesPrivateAPI::Get(profile_)->GetEventRouter(); | 316 MediaGalleriesPrivateAPI::Get(profile_)->GetEventRouter(); |
| 318 DCHECK(router); | 317 DCHECK(router); |
| 319 content::BrowserThread::PostTaskAndReplyWithResult( | 318 content::BrowserThread::PostTaskAndReplyWithResult( |
| 320 content::BrowserThread::FILE, | 319 content::BrowserThread::FILE, |
| 321 FROM_HERE, | 320 FROM_HERE, |
| 322 base::Bind(&GalleryWatchManager::SetupGalleryWatch, | 321 base::Bind(&GalleryWatchManager::SetupGalleryWatch, |
| 323 profile_, | 322 profile_, |
| 324 gallery_pref_id, | 323 pref_id, |
| 325 gallery_file_path, | 324 file_path, |
| 326 extension_id(), | 325 extension_id(), |
| 327 router->AsWeakPtr()), | 326 router->AsWeakPtr()), |
| 328 base::Bind(&MediaGalleriesPrivateAddGalleryWatchFunction::HandleResponse, | 327 base::Bind(&MediaGalleriesPrivateAddGalleryWatchFunction::HandleResponse, |
| 329 this, | 328 this, |
| 330 gallery_pref_id)); | 329 pref_id)); |
| 331 #else | 330 #else |
| 332 // Recursive gallery watch operation is not currently supported on | 331 // Recursive gallery watch operation is not currently supported on |
| 333 // non-windows platforms. Please refer to crbug.com/144491 for more details. | 332 // non-windows platforms. Please refer to crbug.com/144491 for more details. |
| 334 HandleResponse(gallery_pref_id, false); | 333 HandleResponse(pref_id, false); |
| 335 #endif | 334 #endif |
| 336 return true; | |
| 337 } | 335 } |
| 338 | 336 |
| 339 void MediaGalleriesPrivateAddGalleryWatchFunction::HandleResponse( | 337 void MediaGalleriesPrivateAddGalleryWatchFunction::HandleResponse( |
| 340 chrome::MediaGalleryPrefId gallery_id, | 338 chrome::MediaGalleryPrefId gallery_id, |
| 341 bool success) { | 339 bool success) { |
| 342 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 340 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 343 extensions::api::media_galleries_private::AddGalleryWatchResult result; | 341 extensions::api::media_galleries_private::AddGalleryWatchResult result; |
| 344 result.gallery_id = base::Uint64ToString(gallery_id); | 342 result.gallery_id = base::Uint64ToString(gallery_id); |
| 345 result.success = success; | 343 result.success = success; |
| 346 SetResult(result.ToValue().release()); | 344 SetResult(result.ToValue().release()); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 366 #if defined(OS_WIN) | 364 #if defined(OS_WIN) |
| 367 if (!render_view_host() || !render_view_host()->GetProcess()) | 365 if (!render_view_host() || !render_view_host()->GetProcess()) |
| 368 return false; | 366 return false; |
| 369 | 367 |
| 370 // Remove gallery watch operation is currently supported on windows platforms. | 368 // Remove gallery watch operation is currently supported on windows platforms. |
| 371 // Please refer to crbug.com/144491 for more details. | 369 // Please refer to crbug.com/144491 for more details. |
| 372 scoped_ptr<RemoveGalleryWatch::Params> params( | 370 scoped_ptr<RemoveGalleryWatch::Params> params( |
| 373 RemoveGalleryWatch::Params::Create(*args_)); | 371 RemoveGalleryWatch::Params::Create(*args_)); |
| 374 EXTENSION_FUNCTION_VALIDATE(params.get()); | 372 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 375 | 373 |
| 376 base::FilePath gallery_file_path; | 374 chrome::MediaGalleryPrefId pref_id = chrome::kInvalidMediaGalleryPrefId; |
| 377 chrome::MediaGalleryPrefId gallery_pref_id = 0; | 375 if (!base::StringToUint64(params->gallery_id, &pref_id) || |
| 378 if (!GetGalleryFilePathAndId(params->gallery_id, profile_, GetExtension(), | 376 pref_id == chrome::kInvalidMediaGalleryPrefId) { |
| 379 &gallery_file_path, &gallery_pref_id)) { | |
| 380 error_ = kInvalidGalleryIDError; | 377 error_ = kInvalidGalleryIDError; |
| 381 return false; | 378 return false; |
| 382 } | 379 } |
| 383 | 380 |
| 381 chrome::StorageMonitor::GetInstance()->Initialize(base::Bind( |
| 382 &MediaGalleriesPrivateRemoveGalleryWatchFunction::OnStorageMonitorInit, |
| 383 this, pref_id)); |
| 384 return true; |
| 385 } |
| 386 |
| 387 void MediaGalleriesPrivateRemoveGalleryWatchFunction::OnStorageMonitorInit( |
| 388 chrome::MediaGalleryPrefId pref_id) { |
| 389 chrome::MediaFileSystemRegistry* registry = |
| 390 g_browser_process->media_file_system_registry(); |
| 391 chrome::MediaGalleriesPreferences* preferences = |
| 392 registry->GetPreferences(profile_); |
| 393 base::FilePath file_path(preferences->LookUpGalleryPathForExtension( |
| 394 pref_id, GetExtension(), false)); |
| 395 |
| 396 if (file_path.empty) { |
| 397 error_ = kInvalidGalleryIDError; |
| 398 HandleResponse(pref_id, false); |
| 399 return; |
| 400 } |
| 401 |
| 402 GalleryWatchStateTracker* state_tracker = |
| 403 MediaGalleriesPrivateAPI::Get(profile_)->GetGalleryWatchStateTracker(); |
| 404 |
| 384 content::BrowserThread::PostTask( | 405 content::BrowserThread::PostTask( |
| 385 content::BrowserThread::FILE, FROM_HERE, | 406 content::BrowserThread::FILE, FROM_HERE, |
| 386 base::Bind(&GalleryWatchManager::RemoveGalleryWatch, | 407 base::Bind(&GalleryWatchManager::RemoveGalleryWatch, |
| 387 profile_, | 408 profile_, |
| 388 gallery_file_path, | 409 preferences, |
| 410 file_path, |
| 389 extension_id())); | 411 extension_id())); |
| 390 | 412 |
| 391 GalleryWatchStateTracker* state_tracker = | 413 GalleryWatchStateTracker* state_tracker = |
| 392 MediaGalleriesPrivateAPI::Get(profile_)->GetGalleryWatchStateTracker(); | 414 MediaGalleriesPrivateAPI::Get(profile_)->GetGalleryWatchStateTracker(); |
| 393 state_tracker->OnGalleryWatchRemoved(extension_id(), gallery_pref_id); | 415 state_tracker->OnGalleryWatchRemoved(extension_id(), pref_id); |
| 394 #endif | 416 #endif |
| 395 return true; | 417 return true; |
| 396 } | 418 } |
| 397 | 419 |
| 398 /////////////////////////////////////////////////////////////////////////////// | 420 /////////////////////////////////////////////////////////////////////////////// |
| 399 // MediaGalleriesPrivateGetAllGalleryWatchFunction // | 421 // MediaGalleriesPrivateGetAllGalleryWatchFunction // |
| 400 /////////////////////////////////////////////////////////////////////////////// | 422 /////////////////////////////////////////////////////////////////////////////// |
| 401 | 423 |
| 402 MediaGalleriesPrivateGetAllGalleryWatchFunction:: | 424 MediaGalleriesPrivateGetAllGalleryWatchFunction:: |
| 403 ~MediaGalleriesPrivateGetAllGalleryWatchFunction() { | 425 ~MediaGalleriesPrivateGetAllGalleryWatchFunction() { |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 544 } | 566 } |
| 545 } | 567 } |
| 546 | 568 |
| 547 SetResult(result_list); | 569 SetResult(result_list); |
| 548 SendResponse(true); | 570 SendResponse(true); |
| 549 | 571 |
| 550 return true; | 572 return true; |
| 551 } | 573 } |
| 552 | 574 |
| 553 } // namespace extensions | 575 } // namespace extensions |
| OLD | NEW |