]> khamidou — GitHub mirrors - electron.git/commitdiff
Add options check.
authorHaojian Wu <hokein.wu@gmail.com>
Mon, 19 Oct 2015 02:54:12 +0000 (10:54 +0800)
committerHaojian Wu <hokein.wu@gmail.com>
Mon, 19 Oct 2015 03:06:56 +0000 (11:06 +0800)
This patch avoids main process never response back to renderer if the options is
invalid.

atom/browser/api/atom_api_desktop_capturer.cc
atom/browser/lib/desktop-capturer.coffee
atom/renderer/api/lib/desktop-capturer.coffee
docs/api/desktop-capturer.md

index 116b0f4debf00f9d7655f3d14e5fb5fbb53577cd..21e98fe47c21ad0bf5d2fa08eeba31d22ae50357 100644 (file)
@@ -41,6 +41,24 @@ namespace api {
 namespace {
 const int kThumbnailWidth = 150;
 const int kThumbnailHeight = 150;
+
+bool GetCapturerTypes(const mate::Dictionary& args,
+                      bool* show_windows,
+                      bool* show_screens) {
+  *show_windows = false;
+  *show_screens = false;
+  std::vector<std::string> sources;
+  if (!args.Get("types", &sources))
+    return false;
+  for (const auto& source_type : sources) {
+    if (source_type == "screen")
+      *show_screens = true;
+    else if (source_type == "window")
+      *show_windows = true;
+  }
+  return !show_windows && !show_screens ? false : true;
+}
+
 }  // namespace
 
 DesktopCapturer::DesktopCapturer() {
@@ -50,21 +68,14 @@ DesktopCapturer::~DesktopCapturer() {
 }
 
 void DesktopCapturer::StartHandling(const mate::Dictionary& args) {
-  std::vector<std::string> sources;
-  if (!args.Get("types", &sources))
-    return;
-
   bool show_screens = false;
   bool show_windows = false;
-  for (const auto& source_type : sources) {
-    if (source_type == "screen")
-      show_screens = true;
-    else if (source_type == "window")
-      show_windows = true;
-  }
-
-  if (!show_windows && !show_screens)
+  if (!GetCapturerTypes(args, &show_windows, &show_screens)) {
+    Emit("handling-finished",
+         "Invalid options.",
+         std::vector<DesktopMediaList::Source>());
     return;
+  }
 
   webrtc::DesktopCaptureOptions options =
       webrtc::DesktopCaptureOptions::CreateDefault();
@@ -113,7 +124,7 @@ bool DesktopCapturer::OnRefreshFinished() {
   for (int i = 0; i < media_list_->GetSourceCount(); ++i)
     sources.push_back(media_list_->GetSource(i));
   media_list_.reset();
-  Emit("refresh-finished", sources);
+  Emit("handling-finished", "", sources);
   return false;
 }
 
index fc3cdd5feaae7a275e68f5c2d3c79b1de3983fdd..916eda7b68aa19b16d5a4bf036be2fa88e28f928 100644 (file)
@@ -11,25 +11,26 @@ requestsQueue = []
 
 ipc.on 'ATOM_BROWSER_DESKTOP_CAPTURER_GET_SOURCES', (event, options) ->
   request = { options: options, webContents: event.sender }
-  desktopCapturer.startHandling options if requestsQueue.length is 0
   requestsQueue.push request
+  desktopCapturer.startHandling options if requestsQueue.length is 1
   # If the WebContents is destroyed before receiving result, just remove the
   # reference from requestsQueue to make the module not send the result to it.
   event.sender.once 'destroyed', () ->
     request.webContents = null
 
-desktopCapturer.emit = (event_name, event, sources) ->
+desktopCapturer.emit = (event_name, event, error_message, sources) ->
   # Receiving sources result from main process, now send them back to renderer.
   handledRequest = requestsQueue.shift 0
+  error = if error_message then Error error_message else null
   result = ({ id: source.id, name: source.name, thumbnail: source.thumbnail.toDataUrl() } for source in sources)
-  handledRequest.webContents?.send 'ATOM_RENDERER_DESKTOP_CAPTURER_RESULT', result
+  handledRequest.webContents?.send 'ATOM_RENDERER_DESKTOP_CAPTURER_RESULT', error_message, result
 
   # Check the queue to see whether there is other same request. If has, handle
   # it for reducing redunplicated `desktopCaptuer.startHandling` calls.
   unhandledRequestsQueue = []
   for request in requestsQueue
     if isOptionsEqual handledRequest.options, request.options
-      request.webContents?.send 'ATOM_RENDERER_DESKTOP_CAPTURER_RESULT', result
+      request.webContents?.send 'ATOM_RENDERER_DESKTOP_CAPTURER_RESULT', error_message, result
     else
       unhandledRequestsQueue.push request
   requestsQueue = unhandledRequestsQueue
index a82eb69e657c1bf949a9f186ca52d23583468709..5c8caf076a3a2d3375e9e245a78cc53779b46b90 100644 (file)
@@ -3,8 +3,9 @@ NativeImage = require 'native-image'
 
 getSources = (options, callback) ->
   ipc.send 'ATOM_BROWSER_DESKTOP_CAPTURER_GET_SOURCES', options
-  ipc.once 'ATOM_RENDERER_DESKTOP_CAPTURER_RESULT', (sources) ->
-    callback ({id: source.id, name: source.name, thumbnail: NativeImage.createFromDataUrl source.thumbnail} for source in sources)
+  ipc.once 'ATOM_RENDERER_DESKTOP_CAPTURER_RESULT', (error_message, sources) ->
+    error = if error_message then Error error_message else null
+    callback error, ({id: source.id, name: source.name, thumbnail: NativeImage.createFromDataUrl source.thumbnail} for source in sources)
 
 module.exports =
   getSources: getSources
index 8002451c463b8f1eaae94b688400e3607931448f..2d805771cef015660e61f7f40a23ec96d6da155f 100644 (file)
@@ -7,7 +7,8 @@ screen and individual app windows.
 // In the renderer process.
 var desktopCapturer = require('desktop-capturer');
 
-desktopCapturer.getSources({types: ['window', 'screen']}, function(sources) {
+desktopCapturer.getSources({types: ['window', 'screen']}, function(error, sources) {
+  if (error) throw error;
   for (var i = 0; i < sources.length; ++i) {
     if (sources[i].name == "Electron") {
       navigator.webkitGetUserMedia({
@@ -52,9 +53,10 @@ The `desktopCapturer` module has the following methods:
   * `width` Integer - The width of thumbnail. By default, it is 150px.
   * `height` Integer - The height of thumbnail. By default, it is 150px.
 
-`callback` Function - `function(sources) {}`
+`callback` Function - `function(error, sources) {}`
 
-* `Sources` Array - An array of Source
+* `error` Error
+* `sources` Array - An array of Source
 
 Gets all desktop sources.