Skip to content

Commit

Permalink
fix: convert CatchException to WebContents static helper method
Browse files Browse the repository at this point in the history
  • Loading branch information
VerteDinde committed May 6, 2021
1 parent 030399e commit a40f874
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 25 deletions.
26 changes: 23 additions & 3 deletions shell/browser/api/electron_api_web_contents.cc
Expand Up @@ -3708,7 +3708,7 @@ gin::Handle<WebContents> WebContents::New(
const gin_helper::Dictionary& options) {
gin::Handle<WebContents> handle =
gin::CreateHandle(isolate, new WebContents(isolate, options));
gin_helper::CallMethodCatchException(isolate, handle.get(), "_init");
CatchException(isolate, handle);
return handle;
}

Expand All @@ -3719,7 +3719,7 @@ gin::Handle<WebContents> WebContents::CreateAndTake(
Type type) {
gin::Handle<WebContents> handle = gin::CreateHandle(
isolate, new WebContents(isolate, std::move(web_contents), type));
gin_helper::CallMethodCatchException(isolate, handle.get(), "_init");
CatchException(isolate, handle);
return handle;
}

Expand All @@ -3739,7 +3739,7 @@ gin::Handle<WebContents> WebContents::FromOrCreate(
WebContents* api_web_contents = From(web_contents);
if (!api_web_contents) {
api_web_contents = new WebContents(isolate, web_contents);
gin_helper::CallMethodCatchException(isolate, api_web_contents, "_init");
CatchException(isolate, api_web_contents);
}
return gin::CreateHandle(isolate, api_web_contents);
}
Expand Down Expand Up @@ -3771,6 +3771,26 @@ gin::Handle<WebContents> WebContents::CreateFromWebPreferences(
return web_contents;
}

// static
void WebContents::CatchException(v8::Isolate* isolate,
gin::Handle<WebContents> handle) {
v8::TryCatch try_catch(isolate);
gin_helper::CallMethod(isolate, handle.get(), "_init");
if (try_catch.HasCaught()) {
node::errors::TriggerUncaughtException(isolate, try_catch);
}
}

// static
void WebContents::CatchException(v8::Isolate* isolate,
WebContents* api_web_contents) {
v8::TryCatch try_catch(isolate);
gin_helper::CallMethod(isolate, api_web_contents, "_init");
if (try_catch.HasCaught()) {
node::errors::TriggerUncaughtException(isolate, try_catch);
}
}

// static
WebContents* WebContents::FromID(int32_t id) {
return GetAllWebContents().Lookup(id);
Expand Down
7 changes: 7 additions & 0 deletions shell/browser/api/electron_api_web_contents.h
Expand Up @@ -137,6 +137,13 @@ class WebContents : public gin::Wrappable<WebContents>,
v8::Isolate* isolate,
const gin_helper::Dictionary& web_preferences);

// exception helpers to handle catching errors within JavaScript
// and sending them to the JS scope, so they don't crash C++
static void CatchException(v8::Isolate* isolate,
gin::Handle<WebContents> handle);
static void CatchException(v8::Isolate* isolate,
WebContents* api_web_contents);

// gin::Wrappable
static gin::WrapperInfo kWrapperInfo;
static v8::Local<v8::ObjectTemplate> FillObjectTemplate(
Expand Down
22 changes: 0 additions & 22 deletions shell/common/gin_helper/event_emitter_caller.h
Expand Up @@ -65,28 +65,6 @@ v8::Local<v8::Value> CustomEmit(v8::Isolate* isolate,
&converted_args);
}

template <typename T, typename... Args>
v8::Local<v8::Value> CallMethodCatchException(v8::Isolate* isolate,
gin::Wrappable<T>* object,
const char* method_name,
Args&&... args) {
v8::EscapableHandleScope scope(isolate);
v8::Local<v8::Object> v8_object;
if (object->GetWrapper(isolate).ToLocal(&v8_object)) {
// Send unhandled errors within init to a different scope,
// so they're handled within JavaScript and don't crash within C++
v8::TryCatch try_catch(isolate);
v8::Local<v8::Value> value = scope.Escape(CustomEmit(
isolate, v8_object, method_name, std::forward<Args>(args)...));
if (try_catch.HasCaught()) {
node::errors::TriggerUncaughtException(isolate, try_catch);
}
return value;
} else {
return v8::Local<v8::Value>();
}
}

template <typename T, typename... Args>
v8::Local<v8::Value> CallMethod(v8::Isolate* isolate,
gin::Wrappable<T>* object,
Expand Down

0 comments on commit a40f874

Please sign in to comment.