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 |
315 #if defined(OS_WIN) | 311 #if defined(OS_WIN) |
316 MediaGalleriesPrivateEventRouter* router = | 312 MediaGalleriesPrivateEventRouter* router = |
317 MediaGalleriesPrivateAPI::Get(profile_)->GetEventRouter(); | 313 MediaGalleriesPrivateAPI::Get(profile_)->GetEventRouter(); |
318 DCHECK(router); | 314 DCHECK(router); |
319 content::BrowserThread::PostTaskAndReplyWithResult( | 315 content::BrowserThread::PostTaskAndReplyWithResult( |
320 content::BrowserThread::FILE, | 316 content::BrowserThread::FILE, |
321 FROM_HERE, | 317 FROM_HERE, |
322 base::Bind(&GalleryWatchManager::SetupGalleryWatch, | 318 base::Bind(&GalleryWatchManager::SetupGalleryWatch, |
323 profile_, | 319 profile_, |
324 gallery_pref_id, | 320 pref_id, |
325 gallery_file_path, | 321 file_path, |
326 extension_id(), | 322 extension_id(), |
327 router->AsWeakPtr()), | 323 router->AsWeakPtr()), |
328 base::Bind(&MediaGalleriesPrivateAddGalleryWatchFunction::HandleResponse, | 324 base::Bind(&MediaGalleriesPrivateAddGalleryWatchFunction::HandleResponse, |
329 this, | 325 this, |
330 gallery_pref_id)); | 326 pref_id)); |
331 #else | 327 #else |
332 // Recursive gallery watch operation is not currently supported on | 328 // Recursive gallery watch operation is not currently supported on |
333 // non-windows platforms. Please refer to crbug.com/144491 for more details. | 329 // non-windows platforms. Please refer to crbug.com/144491 for more details. |
334 HandleResponse(gallery_pref_id, false); | 330 HandleResponse(pref_id, false); |
335 #endif | 331 #endif |
336 return true; | |
337 } | 332 } |
338 | 333 |
339 void MediaGalleriesPrivateAddGalleryWatchFunction::HandleResponse( | 334 void MediaGalleriesPrivateAddGalleryWatchFunction::HandleResponse( |
340 chrome::MediaGalleryPrefId gallery_id, | 335 chrome::MediaGalleryPrefId gallery_id, |
341 bool success) { | 336 bool success) { |
342 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 337 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
343 extensions::api::media_galleries_private::AddGalleryWatchResult result; | 338 extensions::api::media_galleries_private::AddGalleryWatchResult result; |
344 result.gallery_id = base::Uint64ToString(gallery_id); | 339 result.gallery_id = base::Uint64ToString(gallery_id); |
345 result.success = success; | 340 result.success = success; |
346 SetResult(result.ToValue().release()); | 341 SetResult(result.ToValue().release()); |
(...skipping 19 matching lines...) Expand all Loading... |
366 #if defined(OS_WIN) | 361 #if defined(OS_WIN) |
367 if (!render_view_host() || !render_view_host()->GetProcess()) | 362 if (!render_view_host() || !render_view_host()->GetProcess()) |
368 return false; | 363 return false; |
369 | 364 |
370 // Remove gallery watch operation is currently supported on windows platforms. | 365 // Remove gallery watch operation is currently supported on windows platforms. |
371 // Please refer to crbug.com/144491 for more details. | 366 // Please refer to crbug.com/144491 for more details. |
372 scoped_ptr<RemoveGalleryWatch::Params> params( | 367 scoped_ptr<RemoveGalleryWatch::Params> params( |
373 RemoveGalleryWatch::Params::Create(*args_)); | 368 RemoveGalleryWatch::Params::Create(*args_)); |
374 EXTENSION_FUNCTION_VALIDATE(params.get()); | 369 EXTENSION_FUNCTION_VALIDATE(params.get()); |
375 | 370 |
376 base::FilePath gallery_file_path; | 371 chrome::MediaGalleryPrefId pref_id = chrome::kInvalidMediaGalleryPrefId; |
377 chrome::MediaGalleryPrefId gallery_pref_id = 0; | 372 if (!base::StringToUint64(params->gallery_id, &pref_id) || |
378 if (!GetGalleryFilePathAndId(params->gallery_id, profile_, GetExtension(), | 373 pref_id == chrome::kInvalidMediaGalleryPrefId) { |
379 &gallery_file_path, &gallery_pref_id)) { | |
380 error_ = kInvalidGalleryIDError; | 374 error_ = kInvalidGalleryIDError; |
381 return false; | 375 return false; |
382 } | 376 } |
383 | 377 |
| 378 chrome::MediaFileSystemRegistry* registry = |
| 379 g_browser_process->media_file_system_registry(); |
| 380 registry->GetPreferencesAsync(profile_, base::Bind( |
| 381 &MediaGalleriesPrivateRemoveGalleryWatchFunction::OnPreferences, |
| 382 base::Unretained(this), pref_id)); |
| 383 return true; |
| 384 } |
| 385 |
| 386 void MediaGalleriesPrivateRemoveGalleryWatchFunction::OnPreferences( |
| 387 chrome::MediaGalleriesPreferences* preferences, |
| 388 chrome::MediaGalleryPrefId pref_id) { |
| 389 base::FilePath file_path(preferences->LookUpGalleryPathForExtension( |
| 390 pref_id, GetExtension(), false)); |
| 391 |
| 392 if (file_path.empty) { |
| 393 HandleResponse(pref_id, false); |
| 394 return; |
| 395 } |
| 396 |
| 397 GalleryWatchStateTracker* state_tracker = |
| 398 MediaGalleriesPrivateAPI::Get(profile_)->GetGalleryWatchStateTracker(); |
| 399 state_tracker->SetPreferences(preferences); |
| 400 |
384 content::BrowserThread::PostTask( | 401 content::BrowserThread::PostTask( |
385 content::BrowserThread::FILE, FROM_HERE, | 402 content::BrowserThread::FILE, FROM_HERE, |
386 base::Bind(&GalleryWatchManager::RemoveGalleryWatch, | 403 base::Bind(&GalleryWatchManager::RemoveGalleryWatch, |
387 profile_, | 404 profile_, |
388 gallery_file_path, | 405 preferences, |
| 406 file_path, |
389 extension_id())); | 407 extension_id())); |
390 | 408 |
391 GalleryWatchStateTracker* state_tracker = | 409 GalleryWatchStateTracker* state_tracker = |
392 MediaGalleriesPrivateAPI::Get(profile_)->GetGalleryWatchStateTracker(); | 410 MediaGalleriesPrivateAPI::Get(profile_)->GetGalleryWatchStateTracker(); |
393 state_tracker->OnGalleryWatchRemoved(extension_id(), gallery_pref_id); | 411 state_tracker->OnGalleryWatchRemoved(extension_id(), pref_id); |
394 #endif | 412 #endif |
395 return true; | 413 return true; |
396 } | 414 } |
397 | 415 |
398 /////////////////////////////////////////////////////////////////////////////// | 416 /////////////////////////////////////////////////////////////////////////////// |
399 // MediaGalleriesPrivateGetAllGalleryWatchFunction // | 417 // MediaGalleriesPrivateGetAllGalleryWatchFunction // |
400 /////////////////////////////////////////////////////////////////////////////// | 418 /////////////////////////////////////////////////////////////////////////////// |
401 | 419 |
402 MediaGalleriesPrivateGetAllGalleryWatchFunction:: | 420 MediaGalleriesPrivateGetAllGalleryWatchFunction:: |
403 ~MediaGalleriesPrivateGetAllGalleryWatchFunction() { | 421 ~MediaGalleriesPrivateGetAllGalleryWatchFunction() { |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
544 } | 562 } |
545 } | 563 } |
546 | 564 |
547 SetResult(result_list); | 565 SetResult(result_list); |
548 SendResponse(true); | 566 SendResponse(true); |
549 | 567 |
550 return true; | 568 return true; |
551 } | 569 } |
552 | 570 |
553 } // namespace extensions | 571 } // namespace extensions |
OLD | NEW |