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

This commit is contained in:
Jeremy Rose 2020-07-20 11:07:02 -07:00 committed by GitHub
parent 682f78b9a8
commit e5cb22b7f9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 34 deletions

View file

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

View file

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