| 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 // Implements the Chrome Extensions Media Galleries API. | 5 // Implements the Chrome Extensions Media Galleries API. |
| 6 | 6 |
| 7 #include "chrome/browser/extensions/api/media_galleries/media_galleries_api.h" | 7 #include "chrome/browser/extensions/api/media_galleries/media_galleries_api.h" |
| 8 | 8 |
| 9 #include <stddef.h> | 9 #include <stddef.h> |
| 10 | |
| 11 #include <set> | 10 #include <set> |
| 12 #include <string> | 11 #include <string> |
| 12 #include <utility> |
| 13 #include <vector> | 13 #include <vector> |
| 14 | 14 |
| 15 #include "base/callback.h" | 15 #include "base/callback.h" |
| 16 #include "base/lazy_instance.h" | 16 #include "base/lazy_instance.h" |
| 17 #include "base/macros.h" | 17 #include "base/macros.h" |
| 18 #include "base/numerics/safe_conversions.h" | 18 #include "base/numerics/safe_conversions.h" |
| 19 #include "base/stl_util.h" | 19 #include "base/stl_util.h" |
| 20 #include "base/strings/string_number_conversions.h" | 20 #include "base/strings/string_number_conversions.h" |
| 21 #include "base/strings/utf_string_conversions.h" | 21 #include "base/strings/utf_string_conversions.h" |
| 22 #include "base/values.h" | 22 #include "base/values.h" |
| (...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 342 | 342 |
| 343 bool MediaGalleriesEventRouter::ExtensionHasScanProgressListener( | 343 bool MediaGalleriesEventRouter::ExtensionHasScanProgressListener( |
| 344 const std::string& extension_id) const { | 344 const std::string& extension_id) const { |
| 345 return EventRouter::Get(profile_)->ExtensionHasEventListener( | 345 return EventRouter::Get(profile_)->ExtensionHasEventListener( |
| 346 extension_id, MediaGalleries::OnScanProgress::kEventName); | 346 extension_id, MediaGalleries::OnScanProgress::kEventName); |
| 347 } | 347 } |
| 348 | 348 |
| 349 void MediaGalleriesEventRouter::OnScanStarted(const std::string& extension_id) { | 349 void MediaGalleriesEventRouter::OnScanStarted(const std::string& extension_id) { |
| 350 MediaGalleries::ScanProgressDetails details; | 350 MediaGalleries::ScanProgressDetails details; |
| 351 details.type = MediaGalleries::SCAN_PROGRESS_TYPE_START; | 351 details.type = MediaGalleries::SCAN_PROGRESS_TYPE_START; |
| 352 DispatchEventToExtension( | 352 DispatchEventToExtension(extension_id, |
| 353 extension_id, events::MEDIA_GALLERIES_ON_SCAN_PROGRESS, | 353 events::MEDIA_GALLERIES_ON_SCAN_PROGRESS, |
| 354 MediaGalleries::OnScanProgress::kEventName, | 354 MediaGalleries::OnScanProgress::kEventName, |
| 355 MediaGalleries::OnScanProgress::Create(details).Pass()); | 355 MediaGalleries::OnScanProgress::Create(details)); |
| 356 } | 356 } |
| 357 | 357 |
| 358 void MediaGalleriesEventRouter::OnScanCancelled( | 358 void MediaGalleriesEventRouter::OnScanCancelled( |
| 359 const std::string& extension_id) { | 359 const std::string& extension_id) { |
| 360 MediaGalleries::ScanProgressDetails details; | 360 MediaGalleries::ScanProgressDetails details; |
| 361 details.type = MediaGalleries::SCAN_PROGRESS_TYPE_CANCEL; | 361 details.type = MediaGalleries::SCAN_PROGRESS_TYPE_CANCEL; |
| 362 DispatchEventToExtension( | 362 DispatchEventToExtension(extension_id, |
| 363 extension_id, events::MEDIA_GALLERIES_ON_SCAN_PROGRESS, | 363 events::MEDIA_GALLERIES_ON_SCAN_PROGRESS, |
| 364 MediaGalleries::OnScanProgress::kEventName, | 364 MediaGalleries::OnScanProgress::kEventName, |
| 365 MediaGalleries::OnScanProgress::Create(details).Pass()); | 365 MediaGalleries::OnScanProgress::Create(details)); |
| 366 } | 366 } |
| 367 | 367 |
| 368 void MediaGalleriesEventRouter::OnScanFinished( | 368 void MediaGalleriesEventRouter::OnScanFinished( |
| 369 const std::string& extension_id, int gallery_count, | 369 const std::string& extension_id, int gallery_count, |
| 370 const MediaGalleryScanResult& file_counts) { | 370 const MediaGalleryScanResult& file_counts) { |
| 371 media_galleries::UsageCount(media_galleries::SCAN_FINISHED); | 371 media_galleries::UsageCount(media_galleries::SCAN_FINISHED); |
| 372 MediaGalleries::ScanProgressDetails details; | 372 MediaGalleries::ScanProgressDetails details; |
| 373 details.type = MediaGalleries::SCAN_PROGRESS_TYPE_FINISH; | 373 details.type = MediaGalleries::SCAN_PROGRESS_TYPE_FINISH; |
| 374 details.gallery_count.reset(new int(gallery_count)); | 374 details.gallery_count.reset(new int(gallery_count)); |
| 375 details.audio_count.reset(new int(file_counts.audio_count)); | 375 details.audio_count.reset(new int(file_counts.audio_count)); |
| 376 details.image_count.reset(new int(file_counts.image_count)); | 376 details.image_count.reset(new int(file_counts.image_count)); |
| 377 details.video_count.reset(new int(file_counts.video_count)); | 377 details.video_count.reset(new int(file_counts.video_count)); |
| 378 DispatchEventToExtension( | 378 DispatchEventToExtension(extension_id, |
| 379 extension_id, events::MEDIA_GALLERIES_ON_SCAN_PROGRESS, | 379 events::MEDIA_GALLERIES_ON_SCAN_PROGRESS, |
| 380 MediaGalleries::OnScanProgress::kEventName, | 380 MediaGalleries::OnScanProgress::kEventName, |
| 381 MediaGalleries::OnScanProgress::Create(details).Pass()); | 381 MediaGalleries::OnScanProgress::Create(details)); |
| 382 } | 382 } |
| 383 | 383 |
| 384 void MediaGalleriesEventRouter::OnScanError( | 384 void MediaGalleriesEventRouter::OnScanError( |
| 385 const std::string& extension_id) { | 385 const std::string& extension_id) { |
| 386 MediaGalleries::ScanProgressDetails details; | 386 MediaGalleries::ScanProgressDetails details; |
| 387 details.type = MediaGalleries::SCAN_PROGRESS_TYPE_ERROR; | 387 details.type = MediaGalleries::SCAN_PROGRESS_TYPE_ERROR; |
| 388 DispatchEventToExtension( | 388 DispatchEventToExtension(extension_id, |
| 389 extension_id, events::MEDIA_GALLERIES_ON_SCAN_PROGRESS, | 389 events::MEDIA_GALLERIES_ON_SCAN_PROGRESS, |
| 390 MediaGalleries::OnScanProgress::kEventName, | 390 MediaGalleries::OnScanProgress::kEventName, |
| 391 MediaGalleries::OnScanProgress::Create(details).Pass()); | 391 MediaGalleries::OnScanProgress::Create(details)); |
| 392 } | 392 } |
| 393 | 393 |
| 394 void MediaGalleriesEventRouter::DispatchEventToExtension( | 394 void MediaGalleriesEventRouter::DispatchEventToExtension( |
| 395 const std::string& extension_id, | 395 const std::string& extension_id, |
| 396 events::HistogramValue histogram_value, | 396 events::HistogramValue histogram_value, |
| 397 const std::string& event_name, | 397 const std::string& event_name, |
| 398 scoped_ptr<base::ListValue> event_args) { | 398 scoped_ptr<base::ListValue> event_args) { |
| 399 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 399 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 400 | 400 |
| 401 // TODO(tommycli): Remove these CHECKs after fixing https://crbug.com/467627. | 401 // TODO(tommycli): Remove these CHECKs after fixing https://crbug.com/467627. |
| 402 CHECK(profile_); | 402 CHECK(profile_); |
| 403 EventRouter* router = EventRouter::Get(profile_); | 403 EventRouter* router = EventRouter::Get(profile_); |
| 404 CHECK(router); | 404 CHECK(router); |
| 405 | 405 |
| 406 if (!router->ExtensionHasEventListener(extension_id, event_name)) | 406 if (!router->ExtensionHasEventListener(extension_id, event_name)) |
| 407 return; | 407 return; |
| 408 | 408 |
| 409 scoped_ptr<extensions::Event> event( | 409 scoped_ptr<extensions::Event> event(new extensions::Event( |
| 410 new extensions::Event(histogram_value, event_name, event_args.Pass())); | 410 histogram_value, event_name, std::move(event_args))); |
| 411 router->DispatchEventToExtension(extension_id, event.Pass()); | 411 router->DispatchEventToExtension(extension_id, std::move(event)); |
| 412 } | 412 } |
| 413 | 413 |
| 414 void MediaGalleriesEventRouter::OnGalleryChanged( | 414 void MediaGalleriesEventRouter::OnGalleryChanged( |
| 415 const std::string& extension_id, MediaGalleryPrefId gallery_id) { | 415 const std::string& extension_id, MediaGalleryPrefId gallery_id) { |
| 416 MediaGalleries::GalleryChangeDetails details; | 416 MediaGalleries::GalleryChangeDetails details; |
| 417 details.type = MediaGalleries::GALLERY_CHANGE_TYPE_CONTENTS_CHANGED; | 417 details.type = MediaGalleries::GALLERY_CHANGE_TYPE_CONTENTS_CHANGED; |
| 418 details.gallery_id = base::Uint64ToString(gallery_id); | 418 details.gallery_id = base::Uint64ToString(gallery_id); |
| 419 DispatchEventToExtension( | 419 DispatchEventToExtension(extension_id, |
| 420 extension_id, events::MEDIA_GALLERIES_ON_GALLERY_CHANGED, | 420 events::MEDIA_GALLERIES_ON_GALLERY_CHANGED, |
| 421 MediaGalleries::OnGalleryChanged::kEventName, | 421 MediaGalleries::OnGalleryChanged::kEventName, |
| 422 MediaGalleries::OnGalleryChanged::Create(details).Pass()); | 422 MediaGalleries::OnGalleryChanged::Create(details)); |
| 423 } | 423 } |
| 424 | 424 |
| 425 void MediaGalleriesEventRouter::OnGalleryWatchDropped( | 425 void MediaGalleriesEventRouter::OnGalleryWatchDropped( |
| 426 const std::string& extension_id, MediaGalleryPrefId gallery_id) { | 426 const std::string& extension_id, MediaGalleryPrefId gallery_id) { |
| 427 MediaGalleries::GalleryChangeDetails details; | 427 MediaGalleries::GalleryChangeDetails details; |
| 428 details.type = MediaGalleries::GALLERY_CHANGE_TYPE_WATCH_DROPPED; | 428 details.type = MediaGalleries::GALLERY_CHANGE_TYPE_WATCH_DROPPED; |
| 429 details.gallery_id = gallery_id; | 429 details.gallery_id = gallery_id; |
| 430 DispatchEventToExtension( | 430 DispatchEventToExtension(extension_id, |
| 431 extension_id, events::MEDIA_GALLERIES_ON_GALLERY_CHANGED, | 431 events::MEDIA_GALLERIES_ON_GALLERY_CHANGED, |
| 432 MediaGalleries::OnGalleryChanged::kEventName, | 432 MediaGalleries::OnGalleryChanged::kEventName, |
| 433 MediaGalleries::OnGalleryChanged::Create(details).Pass()); | 433 MediaGalleries::OnGalleryChanged::Create(details)); |
| 434 } | 434 } |
| 435 | 435 |
| 436 void MediaGalleriesEventRouter::OnListenerRemoved( | 436 void MediaGalleriesEventRouter::OnListenerRemoved( |
| 437 const EventListenerInfo& details) { | 437 const EventListenerInfo& details) { |
| 438 if (details.event_name == MediaGalleries::OnGalleryChanged::kEventName && | 438 if (details.event_name == MediaGalleries::OnGalleryChanged::kEventName && |
| 439 !ExtensionHasGalleryChangeListener(details.extension_id)) { | 439 !ExtensionHasGalleryChangeListener(details.extension_id)) { |
| 440 gallery_watch_manager()->RemoveAllWatches(profile_, details.extension_id); | 440 gallery_watch_manager()->RemoveAllWatches(profile_, details.extension_id); |
| 441 } | 441 } |
| 442 } | 442 } |
| 443 | 443 |
| (...skipping 596 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1040 attached_image->Set(kTypeKey, new base::StringValue( | 1040 attached_image->Set(kTypeKey, new base::StringValue( |
| 1041 current_image->type)); | 1041 current_image->type)); |
| 1042 attached_image->Set(kSizeKey, new base::FundamentalValue( | 1042 attached_image->Set(kSizeKey, new base::FundamentalValue( |
| 1043 base::checked_cast<int>(current_image->data.size()))); | 1043 base::checked_cast<int>(current_image->data.size()))); |
| 1044 attached_images_list->Append(attached_image); | 1044 attached_images_list->Append(attached_image); |
| 1045 | 1045 |
| 1046 blob_uuids->push_back(current_blob->GetUUID()); | 1046 blob_uuids->push_back(current_blob->GetUUID()); |
| 1047 extensions::BlobHolder* holder = | 1047 extensions::BlobHolder* holder = |
| 1048 extensions::BlobHolder::FromRenderProcessHost( | 1048 extensions::BlobHolder::FromRenderProcessHost( |
| 1049 render_frame_host()->GetProcess()); | 1049 render_frame_host()->GetProcess()); |
| 1050 holder->HoldBlobReference(current_blob.Pass()); | 1050 holder->HoldBlobReference(std::move(current_blob)); |
| 1051 | 1051 |
| 1052 // Construct the next Blob if necessary. | 1052 // Construct the next Blob if necessary. |
| 1053 if (blob_uuids->size() < attached_images->size()) { | 1053 if (blob_uuids->size() < attached_images->size()) { |
| 1054 metadata::AttachedImage* next_image = | 1054 metadata::AttachedImage* next_image = |
| 1055 &(*attached_images)[blob_uuids->size()]; | 1055 &(*attached_images)[blob_uuids->size()]; |
| 1056 content::BrowserContext::CreateMemoryBackedBlob( | 1056 content::BrowserContext::CreateMemoryBackedBlob( |
| 1057 GetProfile(), | 1057 GetProfile(), |
| 1058 next_image->data.c_str(), | 1058 next_image->data.c_str(), |
| 1059 next_image->data.size(), | 1059 next_image->data.size(), |
| 1060 base::Bind(&MediaGalleriesGetMetadataFunction::ConstructNextBlob, | 1060 base::Bind(&MediaGalleriesGetMetadataFunction::ConstructNextBlob, |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1255 &MediaGalleriesRemoveAllGalleryWatchFunction::OnPreferencesInit, this)); | 1255 &MediaGalleriesRemoveAllGalleryWatchFunction::OnPreferencesInit, this)); |
| 1256 return true; | 1256 return true; |
| 1257 } | 1257 } |
| 1258 | 1258 |
| 1259 void MediaGalleriesRemoveAllGalleryWatchFunction::OnPreferencesInit() { | 1259 void MediaGalleriesRemoveAllGalleryWatchFunction::OnPreferencesInit() { |
| 1260 gallery_watch_manager()->RemoveAllWatches(GetProfile(), extension_id()); | 1260 gallery_watch_manager()->RemoveAllWatches(GetProfile(), extension_id()); |
| 1261 SendResponse(true); | 1261 SendResponse(true); |
| 1262 } | 1262 } |
| 1263 | 1263 |
| 1264 } // namespace extensions | 1264 } // namespace extensions |
| OLD | NEW |