refactor: ginify WebContents (#24651)

This commit is contained in:
Jeremy Rose 2020-07-30 09:17:57 -07:00 committed by GitHub
parent e7fc19c98e
commit b5cd9ce0b3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 156 additions and 67 deletions

View file

@ -8,6 +8,7 @@
#include <map>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "base/observer_list.h"
@ -22,14 +23,17 @@
#include "electron/buildflags/buildflags.h"
#include "electron/shell/common/api/api.mojom.h"
#include "gin/handle.h"
#include "gin/wrappable.h"
#include "mojo/public/cpp/bindings/receiver_set.h"
#include "printing/buildflags/buildflags.h"
#include "services/service_manager/public/cpp/binder_registry.h"
#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/browser/event_emitter_mixin.h"
#include "shell/common/gin_helper/cleaned_up_at_exit.h"
#include "shell/common/gin_helper/constructible.h"
#include "shell/common/gin_helper/error_thrower.h"
#include "shell/common/gin_helper/trackable_object.h"
#include "ui/gfx/image/image.h"
#if BUILDFLAG(ENABLE_PRINTING)
@ -131,7 +135,10 @@ class ExtendedWebContentsObserver : public base::CheckedObserver {
};
// Wrapper around the content::WebContents.
class WebContents : public gin_helper::TrackableObject<WebContents>,
class WebContents : public gin::Wrappable<WebContents>,
public gin_helper::EventEmitterMixin<WebContents>,
public gin_helper::Constructible<WebContents>,
public gin_helper::CleanedUpAtExit,
public CommonWebContentsDelegate,
public content::WebContentsObserver,
public mojom::ElectronBrowser {
@ -148,6 +155,8 @@ class WebContents : public gin_helper::TrackableObject<WebContents>,
// Create a new WebContents and return the V8 wrapper of it.
static gin::Handle<WebContents> Create(v8::Isolate* isolate,
const gin_helper::Dictionary& options);
static gin::Handle<WebContents> New(v8::Isolate* isolate,
const gin_helper::Dictionary& options);
// Create a new V8 wrapper for an existing |web_content|.
//
@ -170,8 +179,12 @@ class WebContents : public gin_helper::TrackableObject<WebContents>,
v8::Isolate* isolate,
content::WebContents* web_contents);
static void BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype);
// gin::Wrappable
static gin::WrapperInfo kWrapperInfo;
static v8::Local<v8::ObjectTemplate> FillObjectTemplate(
v8::Isolate*,
v8::Local<v8::ObjectTemplate>);
const char* GetTypeName() override;
base::WeakPtr<WebContents> GetWeakPtr() { return weak_factory_.GetWeakPtr(); }
@ -375,7 +388,7 @@ class WebContents : public gin_helper::TrackableObject<WebContents>,
const base::FilePath& file_path);
// Properties.
int32_t ID() const;
int32_t ID() const { return id_; }
v8::Local<v8::Value> Session(v8::Isolate* isolate);
content::WebContents* HostWebContents() const;
v8::Local<v8::Value> DevToolsWebContents(v8::Isolate* isolate);
@ -395,6 +408,25 @@ class WebContents : public gin_helper::TrackableObject<WebContents>,
bool EmitNavigationEvent(const std::string& event,
content::NavigationHandle* navigation_handle);
// this.emit(name, new Event(sender, message), args...);
template <typename... Args>
bool EmitWithSender(base::StringPiece name,
content::RenderFrameHost* sender,
electron::mojom::ElectronBrowser::InvokeCallback callback,
Args&&... args) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
v8::HandleScope handle_scope(isolate);
v8::Local<v8::Object> wrapper;
if (!GetWrapper(isolate).ToLocal(&wrapper))
return false;
v8::Local<v8::Object> event = gin_helper::internal::CreateNativeEvent(
isolate, wrapper, sender, std::move(callback));
return EmitCustomEvent(name, event, std::forward<Args>(args)...);
}
void MarkDestroyed();
WebContents* embedder() { return embedder_; }
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
@ -637,6 +669,8 @@ class WebContents : public gin_helper::TrackableObject<WebContents>,
// The type of current WebContents.
Type type_ = Type::BROWSER_WINDOW;
int32_t id_;
// Request id used for findInPage request.
uint32_t request_id_ = 0;