| 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" |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 // Key for Media Gallery Permission Value. | 53 // Key for Media Gallery Permission Value. |
| 54 const char kMediaGalleryHasPermissionKey[] = "has_permission"; | 54 const char kMediaGalleryHasPermissionKey[] = "has_permission"; |
| 55 | 55 |
| 56 // Handles the profile shutdown event on the file thread to clean up | 56 // Handles the profile shutdown event on the file thread to clean up |
| 57 // GalleryWatchManager. | 57 // GalleryWatchManager. |
| 58 void HandleProfileShutdownOnFileThread(void* profile_id) { | 58 void HandleProfileShutdownOnFileThread(void* profile_id) { |
| 59 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); | 59 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); |
| 60 GalleryWatchManager::OnProfileShutdown(profile_id); | 60 GalleryWatchManager::OnProfileShutdown(profile_id); |
| 61 } | 61 } |
| 62 | 62 |
| 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( | 63 bool GetMediaGalleryPermissionFromDictionary( |
| 87 const DictionaryValue* dict, | 64 const DictionaryValue* dict, |
| 88 chrome::MediaGalleryPermission* out_permission) { | 65 chrome::MediaGalleryPermission* out_permission) { |
| 89 std::string string_id; | 66 std::string string_id; |
| 90 if (dict->GetString(kMediaGalleryIdKey, &string_id) && | 67 if (dict->GetString(kMediaGalleryIdKey, &string_id) && |
| 91 base::StringToUint64(string_id, &out_permission->pref_id) && | 68 base::StringToUint64(string_id, &out_permission->pref_id) && |
| 92 dict->GetBoolean(kMediaGalleryHasPermissionKey, | 69 dict->GetBoolean(kMediaGalleryHasPermissionKey, |
| 93 &out_permission->has_permission)) { | 70 &out_permission->has_permission)) { |
| 94 return true; | 71 return true; |
| 95 } | 72 } |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 | 274 |
| 298 bool MediaGalleriesPrivateAddGalleryWatchFunction::RunImpl() { | 275 bool MediaGalleriesPrivateAddGalleryWatchFunction::RunImpl() { |
| 299 DCHECK(profile_); | 276 DCHECK(profile_); |
| 300 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 277 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 301 if (!render_view_host() || !render_view_host()->GetProcess()) | 278 if (!render_view_host() || !render_view_host()->GetProcess()) |
| 302 return false; | 279 return false; |
| 303 | 280 |
| 304 scoped_ptr<AddGalleryWatch::Params> params( | 281 scoped_ptr<AddGalleryWatch::Params> params( |
| 305 AddGalleryWatch::Params::Create(*args_)); | 282 AddGalleryWatch::Params::Create(*args_)); |
| 306 EXTENSION_FUNCTION_VALIDATE(params.get()); | 283 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 307 base::FilePath gallery_file_path; | 284 |
| 308 chrome::MediaGalleryPrefId gallery_pref_id = 0; | 285 chrome::MediaGalleryPrefId pref_id = chrome::kInvalidMediaGalleryPrefId; |
| 309 if (!GetGalleryFilePathAndId(params->gallery_id, profile_, GetExtension(), | 286 if (!base::StringToUint64(params->gallery_id, &pref_id) || |
| 310 &gallery_file_path, &gallery_pref_id)) { | 287 pref_id == chrome::kInvalidMediaGalleryPrefId) { |
| 311 error_ = kInvalidGalleryIDError; | 288 error_ = kInvalidGalleryIDError; |
| 312 return false; | 289 return false; |
| 313 } | 290 } |
| 314 | 291 |
| 292 chrome::MediaFileSystemRegistry* registry = |
| 293 g_browser_process->media_file_system_registry(); |
| 294 registry->GetPreferencesAsync(profile_, |
| 295 base::Bind(&MediaGalleriesPrivateAddGalleryWatchFunction::OnPreferences, |
| 296 this, pref_id)); |
| 297 return true; |
| 298 } |
| 299 |
| 300 void MediaGalleriesPrivateAddGalleryWatchFunction::OnPreferences( |
| 301 chrome::MediaGalleryPrefId pref_id, |
| 302 chrome::MediaGalleriesPreferences* preferences) { |
| 303 base::FilePath file_path(preferences->LookUpGalleryPathForExtension( |
| 304 pref_id, GetExtension(), false)); |
| 305 |
| 306 if (file_path.empty()) { |
| 307 HandleResponse(pref_id, false); |
| 308 return; |
| 309 } |
| 310 |
| 311 GalleryWatchStateTracker* state_tracker = |
| 312 MediaGalleriesPrivateAPI::Get(profile_)->GetGalleryWatchStateTracker(); |
| 313 state_tracker->SetPreferences(preferences); |
| 314 |
| 315 #if defined(OS_WIN) | 315 #if defined(OS_WIN) |
| 316 MediaGalleriesPrivateEventRouter* router = | 316 MediaGalleriesPrivateEventRouter* router = |
| 317 MediaGalleriesPrivateAPI::Get(profile_)->GetEventRouter(); | 317 MediaGalleriesPrivateAPI::Get(profile_)->GetEventRouter(); |
| 318 DCHECK(router); | 318 DCHECK(router); |
| 319 content::BrowserThread::PostTaskAndReplyWithResult( | 319 content::BrowserThread::PostTaskAndReplyWithResult( |
| 320 content::BrowserThread::FILE, | 320 content::BrowserThread::FILE, |
| 321 FROM_HERE, | 321 FROM_HERE, |
| 322 base::Bind(&GalleryWatchManager::SetupGalleryWatch, | 322 base::Bind(&GalleryWatchManager::SetupGalleryWatch, |
| 323 profile_, | 323 profile_, |
| 324 gallery_pref_id, | 324 pref_id, |
| 325 gallery_file_path, | 325 file_path, |
| 326 extension_id(), | 326 extension_id(), |
| 327 router->AsWeakPtr()), | 327 router->AsWeakPtr()), |
| 328 base::Bind(&MediaGalleriesPrivateAddGalleryWatchFunction::HandleResponse, | 328 base::Bind(&MediaGalleriesPrivateAddGalleryWatchFunction::HandleResponse, |
| 329 this, | 329 this, |
| 330 gallery_pref_id)); | 330 pref_id)); |
| 331 #else | 331 #else |
| 332 // Recursive gallery watch operation is not currently supported on | 332 // Recursive gallery watch operation is not currently supported on |
| 333 // non-windows platforms. Please refer to crbug.com/144491 for more details. | 333 // non-windows platforms. Please refer to crbug.com/144491 for more details. |
| 334 HandleResponse(gallery_pref_id, false); | 334 HandleResponse(pref_id, false); |
| 335 #endif | 335 #endif |
| 336 return true; | |
| 337 } | 336 } |
| 338 | 337 |
| 339 void MediaGalleriesPrivateAddGalleryWatchFunction::HandleResponse( | 338 void MediaGalleriesPrivateAddGalleryWatchFunction::HandleResponse( |
| 340 chrome::MediaGalleryPrefId gallery_id, | 339 chrome::MediaGalleryPrefId gallery_id, |
| 341 bool success) { | 340 bool success) { |
| 342 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 341 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 343 extensions::api::media_galleries_private::AddGalleryWatchResult result; | 342 extensions::api::media_galleries_private::AddGalleryWatchResult result; |
| 344 result.gallery_id = base::Uint64ToString(gallery_id); | 343 result.gallery_id = base::Uint64ToString(gallery_id); |
| 345 result.success = success; | 344 result.success = success; |
| 346 SetResult(result.ToValue().release()); | 345 SetResult(result.ToValue().release()); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 366 #if defined(OS_WIN) | 365 #if defined(OS_WIN) |
| 367 if (!render_view_host() || !render_view_host()->GetProcess()) | 366 if (!render_view_host() || !render_view_host()->GetProcess()) |
| 368 return false; | 367 return false; |
| 369 | 368 |
| 370 // Remove gallery watch operation is currently supported on windows platforms. | 369 // Remove gallery watch operation is currently supported on windows platforms. |
| 371 // Please refer to crbug.com/144491 for more details. | 370 // Please refer to crbug.com/144491 for more details. |
| 372 scoped_ptr<RemoveGalleryWatch::Params> params( | 371 scoped_ptr<RemoveGalleryWatch::Params> params( |
| 373 RemoveGalleryWatch::Params::Create(*args_)); | 372 RemoveGalleryWatch::Params::Create(*args_)); |
| 374 EXTENSION_FUNCTION_VALIDATE(params.get()); | 373 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 375 | 374 |
| 376 base::FilePath gallery_file_path; | 375 chrome::MediaGalleryPrefId pref_id = chrome::kInvalidMediaGalleryPrefId; |
| 377 chrome::MediaGalleryPrefId gallery_pref_id = 0; | 376 if (!base::StringToUint64(params->gallery_id, &pref_id) || |
| 378 if (!GetGalleryFilePathAndId(params->gallery_id, profile_, GetExtension(), | 377 pref_id == chrome::kInvalidMediaGalleryPrefId) { |
| 379 &gallery_file_path, &gallery_pref_id)) { | |
| 380 error_ = kInvalidGalleryIDError; | 378 error_ = kInvalidGalleryIDError; |
| 381 return false; | 379 return false; |
| 382 } | 380 } |
| 383 | 381 |
| 382 chrome::MediaFileSystemRegistry* registry = |
| 383 g_browser_process->media_file_system_registry(); |
| 384 registry->GetPreferencesAsync(profile_, base::Bind( |
| 385 &MediaGalleriesPrivateRemoveGalleryWatchFunction::OnPreferences, |
| 386 base::Unretained(this), pref_id)); |
| 387 return true; |
| 388 } |
| 389 |
| 390 void MediaGalleriesPrivateRemoveGalleryWatchFunction::OnPreferences( |
| 391 chrome::MediaGalleriesPreferences* preferences, |
| 392 chrome::MediaGalleryPrefId pref_id) { |
| 393 base::FilePath file_path(preferences->LookUpGalleryPathForExtension( |
| 394 pref_id, GetExtension(), false)); |
| 395 |
| 396 if (file_path.empty) { |
| 397 HandleResponse(pref_id, false); |
| 398 return; |
| 399 } |
| 400 |
| 401 GalleryWatchStateTracker* state_tracker = |
| 402 MediaGalleriesPrivateAPI::Get(profile_)->GetGalleryWatchStateTracker(); |
| 403 state_tracker->SetPreferences(preferences); |
| 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 |