Browse Source

refactor: use gin::Arguments in WebContents (#24604)

Jeremy Rose 4 years ago
parent
commit
e5cb22b7f9

+ 34 - 22
shell/browser/api/electron_api_web_contents.cc

@@ -47,6 +47,7 @@
 #include "content/public/common/referrer_type_converters.h"
 #include "electron/buildflags/buildflags.h"
 #include "electron/shell/common/api/api.mojom.h"
+#include "gin/arguments.h"
 #include "gin/data_object_builder.h"
 #include "gin/handle.h"
 #include "gin/object_template_builder.h"
@@ -1651,8 +1652,7 @@ bool WebContents::IsCrashed() const {
   return web_contents()->IsCrashed();
 }
 
-void WebContents::SetUserAgent(const std::string& user_agent,
-                               gin_helper::Arguments* args) {
+void WebContents::SetUserAgent(const std::string& user_agent) {
   web_contents()->SetUserAgentOverride(
       blink::UserAgentOverride::UserAgentOnly(user_agent), false);
 }
@@ -1674,7 +1674,7 @@ v8::Local<v8::Promise> WebContents::SavePage(
   return handle;
 }
 
-void WebContents::OpenDevTools(gin_helper::Arguments* args) {
+void WebContents::OpenDevTools(gin::Arguments* args) {
   if (type_ == Type::REMOTE)
     return;
 
@@ -1897,20 +1897,21 @@ void WebContents::OnGetDefaultPrinter(
                                std::move(print_callback));
 }
 
-void WebContents::Print(gin_helper::Arguments* args) {
+void WebContents::Print(gin::Arguments* args) {
   gin_helper::Dictionary options =
       gin::Dictionary::CreateEmpty(args->isolate());
   base::Value settings(base::Value::Type::DICTIONARY);
 
   if (args->Length() >= 1 && !args->GetNext(&options)) {
-    args->ThrowError("webContents.print(): Invalid print settings specified.");
+    gin_helper::ErrorThrower(args->isolate())
+        .ThrowError("webContents.print(): Invalid print settings specified.");
     return;
   }
 
   printing::CompletionCallback callback;
   if (args->Length() == 2 && !args->GetNext(&callback)) {
-    args->ThrowError(
-        "webContents.print(): Invalid optional callback provided.");
+    gin_helper::ErrorThrower(args->isolate())
+        .ThrowError("webContents.print(): Invalid optional callback provided.");
     return;
   }
 
@@ -1970,7 +1971,8 @@ void WebContents::Print(gin_helper::Arguments* args) {
   base::string16 device_name;
   options.Get("deviceName", &device_name);
   if (!device_name.empty() && !IsDeviceNameValid(device_name)) {
-    args->ThrowError("webContents.print(): Invalid deviceName provided.");
+    gin_helper::ErrorThrower(args->isolate())
+        .ThrowError("webContents.print(): Invalid deviceName provided.");
     return;
   }
 
@@ -2092,19 +2094,21 @@ v8::Local<v8::Promise> WebContents::PrintToPDF(base::DictionaryValue settings) {
 }
 #endif
 
-void WebContents::AddWorkSpace(gin_helper::Arguments* args,
+void WebContents::AddWorkSpace(gin::Arguments* args,
                                const base::FilePath& path) {
   if (path.empty()) {
-    args->ThrowError("path cannot be empty");
+    gin_helper::ErrorThrower(args->isolate())
+        .ThrowError("path cannot be empty");
     return;
   }
   DevToolsAddFileSystem(std::string(), path);
 }
 
-void WebContents::RemoveWorkSpace(gin_helper::Arguments* args,
+void WebContents::RemoveWorkSpace(gin::Arguments* args,
                                   const base::FilePath& path) {
   if (path.empty()) {
-    args->ThrowError("path cannot be empty");
+    gin_helper::ErrorThrower(args->isolate())
+        .ThrowError("path cannot be empty");
     return;
   }
   DevToolsRemoveFileSystem(path);
@@ -2154,10 +2158,11 @@ void WebContents::ReplaceMisspelling(const base::string16& word) {
   web_contents()->ReplaceMisspelling(word);
 }
 
-uint32_t WebContents::FindInPage(gin_helper::Arguments* args) {
+uint32_t WebContents::FindInPage(gin::Arguments* args) {
   base::string16 search_text;
   if (!args->GetNext(&search_text) || search_text.empty()) {
-    args->ThrowError("Must provide a non-empty search content");
+    gin_helper::ErrorThrower(args->isolate())
+        .ThrowError("Must provide a non-empty search content");
     return 0;
   }
 
@@ -2353,11 +2358,16 @@ void WebContents::SendInputEvent(v8::Isolate* isolate,
       v8::Exception::Error(gin::StringToV8(isolate, "Invalid event object")));
 }
 
-void WebContents::BeginFrameSubscription(gin_helper::Arguments* args) {
+void WebContents::BeginFrameSubscription(gin::Arguments* args) {
   bool only_dirty = false;
   FrameSubscriber::FrameCaptureCallback callback;
 
-  args->GetNext(&only_dirty);
+  if (args->Length() > 1) {
+    if (!args->GetNext(&only_dirty)) {
+      args->ThrowError();
+      return;
+    }
+  }
   if (!args->GetNext(&callback)) {
     args->ThrowError();
     return;
@@ -2372,7 +2382,7 @@ void WebContents::EndFrameSubscription() {
 }
 
 void WebContents::StartDrag(const gin_helper::Dictionary& item,
-                            gin_helper::Arguments* args) {
+                            gin::Arguments* args) {
   base::FilePath file;
   std::vector<base::FilePath> files;
   if (!item.Get("files", &files) && item.Get("file", &file)) {
@@ -2381,7 +2391,8 @@ void WebContents::StartDrag(const gin_helper::Dictionary& item,
 
   gin::Handle<NativeImage> icon;
   if (!item.Get("icon", &icon) || icon->image().IsEmpty()) {
-    args->ThrowError("Must specify non-empty 'icon' option");
+    gin_helper::ErrorThrower(args->isolate())
+        .ThrowError("Must specify non-empty 'icon' option");
     return;
   }
 
@@ -2390,11 +2401,12 @@ void WebContents::StartDrag(const gin_helper::Dictionary& item,
     base::MessageLoopCurrent::ScopedNestableTaskAllower allow;
     DragFileItems(files, icon->image(), web_contents()->GetNativeView());
   } else {
-    args->ThrowError("Must specify either 'file' or 'files' option");
+    gin_helper::ErrorThrower(args->isolate())
+        .ThrowError("Must specify either 'file' or 'files' option");
   }
 }
 
-v8::Local<v8::Promise> WebContents::CapturePage(gin_helper::Arguments* args) {
+v8::Local<v8::Promise> WebContents::CapturePage(gin::Arguments* args) {
   gfx::Rect rect;
   gin_helper::Promise<gfx::Image> promise(args->isolate());
   v8::Local<v8::Promise> handle = promise.GetHandle();
@@ -2428,7 +2440,7 @@ v8::Local<v8::Promise> WebContents::CapturePage(gin_helper::Arguments* args) {
   return handle;
 }
 
-void WebContents::IncrementCapturerCount(gin_helper::Arguments* args) {
+void WebContents::IncrementCapturerCount(gin::Arguments* args) {
   gfx::Size size;
   bool stay_hidden = false;
 
@@ -2440,7 +2452,7 @@ void WebContents::IncrementCapturerCount(gin_helper::Arguments* args) {
   web_contents()->IncrementCapturerCount(size, stay_hidden);
 }
 
-void WebContents::DecrementCapturerCount(gin_helper::Arguments* args) {
+void WebContents::DecrementCapturerCount(gin::Arguments* args) {
   bool stay_hidden = false;
 
   // get stayHidden arguments if they exist

+ 14 - 12
shell/browser/api/electron_api_web_contents.h

@@ -28,6 +28,7 @@
 #include "shell/browser/api/frame_subscriber.h"
 #include "shell/browser/api/save_page_handler.h"
 #include "shell/browser/common_web_contents_delegate.h"
+#include "shell/common/gin_helper/error_thrower.h"
 #include "shell/common/gin_helper/trackable_object.h"
 #include "ui/gfx/image/image.h"
 
@@ -62,6 +63,8 @@ class ResourceRequestBody;
 
 namespace gin {
 
+class Arguments;
+
 template <>
 struct Converter<base::TerminationStatus> {
   static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
@@ -210,12 +213,12 @@ class WebContents : public gin_helper::TrackableObject<WebContents>,
   const std::string GetWebRTCIPHandlingPolicy() const;
   void SetWebRTCIPHandlingPolicy(const std::string& webrtc_ip_handling_policy);
   bool IsCrashed() const;
-  void SetUserAgent(const std::string& user_agent, gin_helper::Arguments* args);
+  void SetUserAgent(const std::string& user_agent);
   std::string GetUserAgent();
   void InsertCSS(const std::string& css);
   v8::Local<v8::Promise> SavePage(const base::FilePath& full_file_path,
                                   const content::SavePageType& save_type);
-  void OpenDevTools(gin_helper::Arguments* args);
+  void OpenDevTools(gin::Arguments* args);
   void CloseDevTools();
   bool IsDevToolsOpened();
   bool IsDevToolsFocused();
@@ -234,8 +237,8 @@ class WebContents : public gin_helper::TrackableObject<WebContents>,
   void SetEmbedder(const WebContents* embedder);
   void SetDevToolsWebContents(const WebContents* devtools);
   v8::Local<v8::Value> GetNativeView(v8::Isolate* isolate) const;
-  void IncrementCapturerCount(gin_helper::Arguments* args);
-  void DecrementCapturerCount(gin_helper::Arguments* args);
+  void IncrementCapturerCount(gin::Arguments* args);
+  void DecrementCapturerCount(gin::Arguments* args);
   bool IsBeingCaptured();
 
 #if BUILDFLAG(ENABLE_PRINTING)
@@ -244,15 +247,15 @@ class WebContents : public gin_helper::TrackableObject<WebContents>,
                            base::string16 device_name,
                            bool silent,
                            base::string16 default_printer);
-  void Print(gin_helper::Arguments* args);
+  void Print(gin::Arguments* args);
   std::vector<printing::PrinterBasicInfo> GetPrinterList();
   // Print current page as PDF.
   v8::Local<v8::Promise> PrintToPDF(base::DictionaryValue settings);
 #endif
 
   // DevTools workspace api.
-  void AddWorkSpace(gin_helper::Arguments* args, const base::FilePath& path);
-  void RemoveWorkSpace(gin_helper::Arguments* args, const base::FilePath& path);
+  void AddWorkSpace(gin::Arguments* args, const base::FilePath& path);
+  void RemoveWorkSpace(gin::Arguments* args, const base::FilePath& path);
 
   // Editing commands.
   void Undo();
@@ -266,7 +269,7 @@ class WebContents : public gin_helper::TrackableObject<WebContents>,
   void Unselect();
   void Replace(const base::string16& word);
   void ReplaceMisspelling(const base::string16& word);
-  uint32_t FindInPage(gin_helper::Arguments* args);
+  uint32_t FindInPage(gin::Arguments* args);
   void StopFindInPage(content::StopFindAction action);
   void ShowDefinitionForSelection();
   void CopyImageAt(int x, int y);
@@ -302,16 +305,15 @@ class WebContents : public gin_helper::TrackableObject<WebContents>,
   void SendInputEvent(v8::Isolate* isolate, v8::Local<v8::Value> input_event);
 
   // Subscribe to the frame updates.
-  void BeginFrameSubscription(gin_helper::Arguments* args);
+  void BeginFrameSubscription(gin::Arguments* args);
   void EndFrameSubscription();
 
   // Dragging native items.
-  void StartDrag(const gin_helper::Dictionary& item,
-                 gin_helper::Arguments* args);
+  void StartDrag(const gin_helper::Dictionary& item, gin::Arguments* args);
 
   // Captures the page with |rect|, |callback| would be called when capturing is
   // done.
-  v8::Local<v8::Promise> CapturePage(gin_helper::Arguments* args);
+  v8::Local<v8::Promise> CapturePage(gin::Arguments* args);
 
   // Methods for creating <webview>.
   bool IsGuest() const;