From 5a28759fea358c117c55121c1cd370abd143a207 Mon Sep 17 00:00:00 2001 From: Samuel Attard Date: Tue, 19 Jun 2018 11:37:53 +1000 Subject: [PATCH] post all desktop capturer apis to worker threads --- atom/browser/api/atom_api_desktop_capturer.cc | 91 +++++++++++++------ atom/browser/api/atom_api_desktop_capturer.h | 10 +- 2 files changed, 67 insertions(+), 34 deletions(-) diff --git a/atom/browser/api/atom_api_desktop_capturer.cc b/atom/browser/api/atom_api_desktop_capturer.cc index 1d33bbbd4a7..4c7ed314550 100644 --- a/atom/browser/api/atom_api_desktop_capturer.cc +++ b/atom/browser/api/atom_api_desktop_capturer.cc @@ -12,7 +12,10 @@ using base::PlatformThreadRef; #include "atom/common/native_mate_converters/gfx_converter.h" #include "base/strings/string_number_conversions.h" #include "base/strings/utf_string_conversions.h" +#include "base/task_scheduler/post_task.h" +#include "base/threading/thread_task_runner_handle.h" #include "chrome/browser/media/desktop_media_list.h" +#include "content/public/browser/browser_thread.h" #include "content/public/browser/desktop_capture.h" #include "native_mate/dictionary.h" #include "third_party/webrtc/modules/desktop_capture/desktop_capture_options.h" @@ -45,21 +48,20 @@ struct Converter { } // namespace mate -namespace atom { +namespace { -namespace api { - -DesktopCapturer::DesktopCapturer(v8::Isolate* isolate) { - Init(isolate); +void EmitFinished( + const std::vector& sources, + atom::api::DesktopCapturer* cap) { + cap->Emit("finished", sources); } -DesktopCapturer::~DesktopCapturer() {} - -void DesktopCapturer::StartHandling(bool capture_window, - bool capture_screen, - const gfx::Size& thumbnail_size) { +void StartHandlingTask(bool capture_window, + bool capture_screen, + const gfx::Size& thumbnail_size, + atom::api::DesktopCapturer* cap) { #if defined(OS_WIN) - using_directx_capturer_ = + cap->using_directx_capturer_ = content::desktop_capture::CreateDesktopCaptureOptions() .allow_directx_capturer(); #endif // defined(OS_WIN) @@ -69,29 +71,19 @@ void DesktopCapturer::StartHandling(bool capture_window, std::unique_ptr window_capturer( capture_window ? content::desktop_capture::CreateScreenCapturer() : nullptr); - media_list_.reset(new NativeDesktopMediaList(std::move(screen_capturer), - std::move(window_capturer))); + cap->media_list_.reset(new NativeDesktopMediaList( + std::move(screen_capturer), std::move(window_capturer))); - media_list_->SetThumbnailSize(thumbnail_size); - media_list_->StartUpdating(this); + cap->media_list_->SetThumbnailSize(thumbnail_size); + cap->media_list_->StartUpdating(cap); } -void DesktopCapturer::OnSourceAdded(int index) {} - -void DesktopCapturer::OnSourceRemoved(int index) {} - -void DesktopCapturer::OnSourceMoved(int old_index, int new_index) {} - -void DesktopCapturer::OnSourceNameChanged(int index) {} - -void DesktopCapturer::OnSourceThumbnailChanged(int index) {} - -bool DesktopCapturer::OnRefreshFinished() { - const auto media_list_sources = media_list_->GetSources(); - std::vector sources; +void OnRefreshFinishedTask(atom::api::DesktopCapturer* cap) { + const auto media_list_sources = cap->media_list_->GetSources(); + std::vector sources; for (const auto& media_list_source : media_list_sources) { sources.emplace_back( - DesktopCapturer::Source{media_list_source, std::string()}); + atom::api::DesktopCapturer::Source{media_list_source, std::string()}); } #if defined(OS_WIN) @@ -132,7 +124,46 @@ bool DesktopCapturer::OnRefreshFinished() { // supports capturing the entire desktop on Linux. Revisit this if individual // screen support is added. - Emit("finished", sources); + content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, + base::Bind(EmitFinished, sources, cap)); +} + +} // namespace + +namespace atom { + +namespace api { + +DesktopCapturer::DesktopCapturer(v8::Isolate* isolate) { + Init(isolate); + capture_thread_ = base::CreateSequencedTaskRunnerWithTraits( + {base::WithBaseSyncPrimitives(), base::MayBlock(), + base::TaskPriority::USER_VISIBLE}); +} + +DesktopCapturer::~DesktopCapturer() {} + +void DesktopCapturer::StartHandling(bool capture_window, + bool capture_screen, + const gfx::Size& thumbnail_size) { + capture_thread_->PostTask( + FROM_HERE, base::BindOnce(StartHandlingTask, capture_window, + capture_screen, thumbnail_size, this)); +} + +void DesktopCapturer::OnSourceAdded(int index) {} + +void DesktopCapturer::OnSourceRemoved(int index) {} + +void DesktopCapturer::OnSourceMoved(int old_index, int new_index) {} + +void DesktopCapturer::OnSourceNameChanged(int index) {} + +void DesktopCapturer::OnSourceThumbnailChanged(int index) {} + +bool DesktopCapturer::OnRefreshFinished() { + capture_thread_->PostTask(FROM_HERE, + base::BindOnce(OnRefreshFinishedTask, this)); return false; } diff --git a/atom/browser/api/atom_api_desktop_capturer.h b/atom/browser/api/atom_api_desktop_capturer.h index d933baac2ff..85244b47a01 100644 --- a/atom/browser/api/atom_api_desktop_capturer.h +++ b/atom/browser/api/atom_api_desktop_capturer.h @@ -34,6 +34,11 @@ class DesktopCapturer : public mate::EventEmitter, bool capture_screen, const gfx::Size& thumbnail_size); + std::unique_ptr media_list_; +#if defined(OS_WIN) + bool using_directx_capturer_; +#endif // defined(OS_WIN) + protected: explicit DesktopCapturer(v8::Isolate* isolate); ~DesktopCapturer() override; @@ -47,10 +52,7 @@ class DesktopCapturer : public mate::EventEmitter, bool OnRefreshFinished() override; private: - std::unique_ptr media_list_; -#if defined(OS_WIN) - bool using_directx_capturer_; -#endif // defined(OS_WIN) + scoped_refptr capture_thread_; DISALLOW_COPY_AND_ASSIGN(DesktopCapturer); };