Move BrowserPluginGuestDelegate to a separate class
This commit is contained in:
parent
9974a238c2
commit
b2a8678c47
6 changed files with 322 additions and 223 deletions
|
@ -11,6 +11,7 @@
|
||||||
#include "atom/browser/atom_browser_context.h"
|
#include "atom/browser/atom_browser_context.h"
|
||||||
#include "atom/browser/atom_browser_main_parts.h"
|
#include "atom/browser/atom_browser_main_parts.h"
|
||||||
#include "atom/browser/native_window.h"
|
#include "atom/browser/native_window.h"
|
||||||
|
#include "atom/browser/web_view_guest_delegate.h"
|
||||||
#include "atom/common/api/api_messages.h"
|
#include "atom/common/api/api_messages.h"
|
||||||
#include "atom/common/event_emitter_caller.h"
|
#include "atom/common/event_emitter_caller.h"
|
||||||
#include "atom/common/native_mate_converters/gfx_converter.h"
|
#include "atom/common/native_mate_converters/gfx_converter.h"
|
||||||
|
@ -24,14 +25,12 @@
|
||||||
#include "chrome/browser/printing/print_view_manager_basic.h"
|
#include "chrome/browser/printing/print_view_manager_basic.h"
|
||||||
#include "chrome/browser/printing/print_preview_message_handler.h"
|
#include "chrome/browser/printing/print_preview_message_handler.h"
|
||||||
#include "content/public/browser/favicon_status.h"
|
#include "content/public/browser/favicon_status.h"
|
||||||
#include "content/public/browser/guest_host.h"
|
|
||||||
#include "content/public/browser/navigation_details.h"
|
#include "content/public/browser/navigation_details.h"
|
||||||
#include "content/public/browser/navigation_entry.h"
|
#include "content/public/browser/navigation_entry.h"
|
||||||
#include "content/public/browser/plugin_service.h"
|
#include "content/public/browser/plugin_service.h"
|
||||||
#include "content/public/browser/render_frame_host.h"
|
#include "content/public/browser/render_frame_host.h"
|
||||||
#include "content/public/browser/render_process_host.h"
|
#include "content/public/browser/render_process_host.h"
|
||||||
#include "content/public/browser/render_view_host.h"
|
#include "content/public/browser/render_view_host.h"
|
||||||
#include "content/public/browser/render_widget_host_view.h"
|
|
||||||
#include "content/public/browser/resource_request_details.h"
|
#include "content/public/browser/resource_request_details.h"
|
||||||
#include "content/public/browser/service_worker_context.h"
|
#include "content/public/browser/service_worker_context.h"
|
||||||
#include "content/public/browser/storage_partition.h"
|
#include "content/public/browser/storage_partition.h"
|
||||||
|
@ -57,10 +56,10 @@ struct PrintSettings {
|
||||||
namespace mate {
|
namespace mate {
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
struct Converter<atom::api::SetSizeParams> {
|
struct Converter<atom::SetSizeParams> {
|
||||||
static bool FromV8(v8::Isolate* isolate,
|
static bool FromV8(v8::Isolate* isolate,
|
||||||
v8::Local<v8::Value> val,
|
v8::Local<v8::Value> val,
|
||||||
atom::api::SetSizeParams* out) {
|
atom::SetSizeParams* out) {
|
||||||
mate::Dictionary params;
|
mate::Dictionary params;
|
||||||
if (!ConvertFromV8(isolate, val, ¶ms))
|
if (!ConvertFromV8(isolate, val, ¶ms))
|
||||||
return false;
|
return false;
|
||||||
|
@ -116,9 +115,6 @@ namespace api {
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
const int kDefaultWidth = 300;
|
|
||||||
const int kDefaultHeight = 300;
|
|
||||||
|
|
||||||
v8::Persistent<v8::ObjectTemplate> template_;
|
v8::Persistent<v8::ObjectTemplate> template_;
|
||||||
|
|
||||||
// The wrapWebContents funtion which is implemented in JavaScript
|
// The wrapWebContents funtion which is implemented in JavaScript
|
||||||
|
@ -144,20 +140,12 @@ content::ServiceWorkerContext* GetServiceWorkerContext(
|
||||||
|
|
||||||
WebContents::WebContents(content::WebContents* web_contents)
|
WebContents::WebContents(content::WebContents* web_contents)
|
||||||
: content::WebContentsObserver(web_contents),
|
: content::WebContentsObserver(web_contents),
|
||||||
guest_opaque_(true),
|
|
||||||
guest_host_(nullptr),
|
|
||||||
auto_size_enabled_(false),
|
|
||||||
is_full_page_plugin_(false),
|
|
||||||
type_(REMOTE),
|
type_(REMOTE),
|
||||||
inspectable_web_contents_(nullptr) {
|
inspectable_web_contents_(nullptr) {
|
||||||
AttachAsUserData(web_contents);
|
AttachAsUserData(web_contents);
|
||||||
}
|
}
|
||||||
|
|
||||||
WebContents::WebContents(const mate::Dictionary& options)
|
WebContents::WebContents(const mate::Dictionary& options) {
|
||||||
: guest_opaque_(true),
|
|
||||||
guest_host_(nullptr),
|
|
||||||
auto_size_enabled_(false),
|
|
||||||
is_full_page_plugin_(false) {
|
|
||||||
bool is_guest = false;
|
bool is_guest = false;
|
||||||
options.Get("isGuest", &is_guest);
|
options.Get("isGuest", &is_guest);
|
||||||
|
|
||||||
|
@ -169,7 +157,8 @@ WebContents::WebContents(const mate::Dictionary& options)
|
||||||
content::SiteInstance* site_instance = content::SiteInstance::CreateForURL(
|
content::SiteInstance* site_instance = content::SiteInstance::CreateForURL(
|
||||||
browser_context, GURL("chrome-guest://fake-host"));
|
browser_context, GURL("chrome-guest://fake-host"));
|
||||||
content::WebContents::CreateParams params(browser_context, site_instance);
|
content::WebContents::CreateParams params(browser_context, site_instance);
|
||||||
params.guest_delegate = this;
|
guest_delegate_.reset(new WebViewGuestDelegate);
|
||||||
|
params.guest_delegate = guest_delegate_.get();
|
||||||
web_contents = content::WebContents::Create(params);
|
web_contents = content::WebContents::Create(params);
|
||||||
} else {
|
} else {
|
||||||
content::WebContents::CreateParams params(browser_context);
|
content::WebContents::CreateParams params(browser_context);
|
||||||
|
@ -182,6 +171,8 @@ WebContents::WebContents(const mate::Dictionary& options)
|
||||||
inspectable_web_contents_ = managed_web_contents();
|
inspectable_web_contents_ = managed_web_contents();
|
||||||
|
|
||||||
if (is_guest) {
|
if (is_guest) {
|
||||||
|
guest_delegate_->Initialize(this);
|
||||||
|
|
||||||
NativeWindow* owner_window = nullptr;
|
NativeWindow* owner_window = nullptr;
|
||||||
WebContents* embedder = nullptr;
|
WebContents* embedder = nullptr;
|
||||||
if (options.Get("embedder", &embedder) && embedder) {
|
if (options.Get("embedder", &embedder) && embedder) {
|
||||||
|
@ -279,9 +270,9 @@ void WebContents::HandleKeyboardEvent(
|
||||||
const content::NativeWebKeyboardEvent& event) {
|
const content::NativeWebKeyboardEvent& event) {
|
||||||
if (type_ == BROWSER_WINDOW) {
|
if (type_ == BROWSER_WINDOW) {
|
||||||
owner_window()->HandleKeyboardEvent(source, event);
|
owner_window()->HandleKeyboardEvent(source, event);
|
||||||
} else if (type_ == WEB_VIEW && !attached()) {
|
} else if (type_ == WEB_VIEW && guest_delegate_) {
|
||||||
// Send the unhandled keyboard events back to the embedder.
|
// Send the unhandled keyboard events back to the embedder.
|
||||||
embedder_web_contents_->GetDelegate()->HandleKeyboardEvent(source, event);
|
guest_delegate_->HandleKeyboardEvent(source, event);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -467,21 +458,6 @@ bool WebContents::OnMessageReceived(const IPC::Message& message) {
|
||||||
return handled;
|
return handled;
|
||||||
}
|
}
|
||||||
|
|
||||||
void WebContents::RenderViewReady() {
|
|
||||||
if (type_ != WEB_VIEW)
|
|
||||||
return;
|
|
||||||
|
|
||||||
// We don't want to accidentally set the opacity of an interstitial page.
|
|
||||||
// WebContents::GetRenderWidgetHostView will return the RWHV of an
|
|
||||||
// interstitial page if one is showing at this time. We only want opacity
|
|
||||||
// to apply to web pages.
|
|
||||||
auto render_view_host_view = web_contents()->GetRenderViewHost()->GetView();
|
|
||||||
if (guest_opaque_)
|
|
||||||
render_view_host_view->SetBackgroundColorToDefault();
|
|
||||||
else
|
|
||||||
render_view_host_view->SetBackgroundColor(SK_ColorTRANSPARENT);
|
|
||||||
}
|
|
||||||
|
|
||||||
void WebContents::WebContentsDestroyed() {
|
void WebContents::WebContentsDestroyed() {
|
||||||
// The RenderViewDeleted was not called when the WebContents is destroyed.
|
// The RenderViewDeleted was not called when the WebContents is destroyed.
|
||||||
RenderViewDeleted(web_contents()->GetRenderViewHost());
|
RenderViewDeleted(web_contents()->GetRenderViewHost());
|
||||||
|
@ -495,40 +471,12 @@ void WebContents::NavigationEntryCommitted(
|
||||||
details.is_in_page, details.did_replace_entry);
|
details.is_in_page, details.did_replace_entry);
|
||||||
}
|
}
|
||||||
|
|
||||||
void WebContents::DidAttach(int guest_proxy_routing_id) {
|
|
||||||
Emit("did-attach");
|
|
||||||
}
|
|
||||||
|
|
||||||
content::WebContents* WebContents::GetOwnerWebContents() const {
|
|
||||||
return embedder_web_contents_;
|
|
||||||
}
|
|
||||||
|
|
||||||
void WebContents::GuestSizeChanged(const gfx::Size& new_size) {
|
|
||||||
if (!auto_size_enabled_)
|
|
||||||
return;
|
|
||||||
GuestSizeChangedDueToAutoSize(guest_size_, new_size);
|
|
||||||
guest_size_ = new_size;
|
|
||||||
}
|
|
||||||
|
|
||||||
void WebContents::SetGuestHost(content::GuestHost* guest_host) {
|
|
||||||
guest_host_ = guest_host;
|
|
||||||
}
|
|
||||||
|
|
||||||
void WebContents::WillAttach(content::WebContents* embedder_web_contents,
|
|
||||||
int element_instance_id,
|
|
||||||
bool is_full_page_plugin) {
|
|
||||||
embedder_web_contents_ = embedder_web_contents;
|
|
||||||
is_full_page_plugin_ = is_full_page_plugin;
|
|
||||||
}
|
|
||||||
|
|
||||||
void WebContents::Destroy() {
|
void WebContents::Destroy() {
|
||||||
if (type_ == WEB_VIEW && managed_web_contents()) {
|
if (type_ == WEB_VIEW && managed_web_contents()) {
|
||||||
// When force destroying the "destroyed" event is not emitted.
|
// When force destroying the "destroyed" event is not emitted.
|
||||||
WebContentsDestroyed();
|
WebContentsDestroyed();
|
||||||
|
|
||||||
// Give the content module an opportunity to perform some cleanup.
|
guest_delegate_->Destroy();
|
||||||
guest_host_->WillDestroy();
|
|
||||||
guest_host_ = nullptr;
|
|
||||||
|
|
||||||
Observe(nullptr);
|
Observe(nullptr);
|
||||||
DestroyWebContents();
|
DestroyWebContents();
|
||||||
|
@ -781,72 +729,13 @@ bool WebContents::SendIPCMessage(const base::string16& channel,
|
||||||
}
|
}
|
||||||
|
|
||||||
void WebContents::SetSize(const SetSizeParams& params) {
|
void WebContents::SetSize(const SetSizeParams& params) {
|
||||||
bool enable_auto_size =
|
if (guest_delegate_)
|
||||||
params.enable_auto_size ? *params.enable_auto_size : auto_size_enabled_;
|
guest_delegate_->SetSize(params);
|
||||||
gfx::Size min_size = params.min_size ? *params.min_size : min_auto_size_;
|
|
||||||
gfx::Size max_size = params.max_size ? *params.max_size : max_auto_size_;
|
|
||||||
|
|
||||||
if (params.normal_size)
|
|
||||||
normal_size_ = *params.normal_size;
|
|
||||||
|
|
||||||
min_auto_size_ = min_size;
|
|
||||||
min_auto_size_.SetToMin(max_size);
|
|
||||||
max_auto_size_ = max_size;
|
|
||||||
max_auto_size_.SetToMax(min_size);
|
|
||||||
|
|
||||||
enable_auto_size &= !min_auto_size_.IsEmpty() && !max_auto_size_.IsEmpty();
|
|
||||||
|
|
||||||
content::RenderViewHost* rvh = web_contents()->GetRenderViewHost();
|
|
||||||
if (enable_auto_size) {
|
|
||||||
// Autosize is being enabled.
|
|
||||||
rvh->EnableAutoResize(min_auto_size_, max_auto_size_);
|
|
||||||
normal_size_.SetSize(0, 0);
|
|
||||||
} else {
|
|
||||||
// Autosize is being disabled.
|
|
||||||
// Use default width/height if missing from partially defined normal size.
|
|
||||||
if (normal_size_.width() && !normal_size_.height())
|
|
||||||
normal_size_.set_height(GetDefaultSize().height());
|
|
||||||
if (!normal_size_.width() && normal_size_.height())
|
|
||||||
normal_size_.set_width(GetDefaultSize().width());
|
|
||||||
|
|
||||||
gfx::Size new_size;
|
|
||||||
if (!normal_size_.IsEmpty()) {
|
|
||||||
new_size = normal_size_;
|
|
||||||
} else if (!guest_size_.IsEmpty()) {
|
|
||||||
new_size = guest_size_;
|
|
||||||
} else {
|
|
||||||
new_size = GetDefaultSize();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (auto_size_enabled_) {
|
|
||||||
// Autosize was previously enabled.
|
|
||||||
rvh->DisableAutoResize(new_size);
|
|
||||||
GuestSizeChangedDueToAutoSize(guest_size_, new_size);
|
|
||||||
} else {
|
|
||||||
// Autosize was already disabled.
|
|
||||||
guest_host_->SizeContents(new_size);
|
|
||||||
}
|
|
||||||
|
|
||||||
guest_size_ = new_size;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto_size_enabled_ = enable_auto_size;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void WebContents::SetAllowTransparency(bool allow) {
|
void WebContents::SetAllowTransparency(bool allow) {
|
||||||
if (guest_opaque_ != allow)
|
if (guest_delegate_)
|
||||||
return;
|
guest_delegate_->SetAllowTransparency(allow);
|
||||||
|
|
||||||
auto render_view_host = web_contents()->GetRenderViewHost();
|
|
||||||
guest_opaque_ = !allow;
|
|
||||||
if (!render_view_host->GetView())
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (guest_opaque_) {
|
|
||||||
render_view_host->GetView()->SetBackgroundColorToDefault();
|
|
||||||
} else {
|
|
||||||
render_view_host->GetView()->SetBackgroundColor(SK_ColorTRANSPARENT);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool WebContents::IsGuest() const {
|
bool WebContents::IsGuest() const {
|
||||||
|
@ -922,23 +811,6 @@ void WebContents::OnRendererMessageSync(const base::string16& channel,
|
||||||
EmitWithSender(base::UTF16ToUTF8(channel), web_contents(), message, args);
|
EmitWithSender(base::UTF16ToUTF8(channel), web_contents(), message, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
void WebContents::GuestSizeChangedDueToAutoSize(const gfx::Size& old_size,
|
|
||||||
const gfx::Size& new_size) {
|
|
||||||
Emit("size-changed",
|
|
||||||
old_size.width(), old_size.height(),
|
|
||||||
new_size.width(), new_size.height());
|
|
||||||
}
|
|
||||||
|
|
||||||
gfx::Size WebContents::GetDefaultSize() const {
|
|
||||||
if (is_full_page_plugin_) {
|
|
||||||
// Full page plugins default to the size of the owner's viewport.
|
|
||||||
return embedder_web_contents_->GetRenderWidgetHostView()
|
|
||||||
->GetVisibleViewportSize();
|
|
||||||
} else {
|
|
||||||
return gfx::Size(kDefaultWidth, kDefaultHeight);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// static
|
// static
|
||||||
mate::Handle<WebContents> WebContents::CreateFrom(
|
mate::Handle<WebContents> WebContents::CreateFrom(
|
||||||
v8::Isolate* isolate, content::WebContents* web_contents) {
|
v8::Isolate* isolate, content::WebContents* web_contents) {
|
||||||
|
|
|
@ -10,7 +10,6 @@
|
||||||
|
|
||||||
#include "atom/browser/api/trackable_object.h"
|
#include "atom/browser/api/trackable_object.h"
|
||||||
#include "atom/browser/common_web_contents_delegate.h"
|
#include "atom/browser/common_web_contents_delegate.h"
|
||||||
#include "content/public/browser/browser_plugin_guest_delegate.h"
|
|
||||||
#include "content/public/common/favicon_url.h"
|
#include "content/public/common/favicon_url.h"
|
||||||
#include "content/public/browser/web_contents_observer.h"
|
#include "content/public/browser/web_contents_observer.h"
|
||||||
#include "content/public/browser/gpu_data_manager_observer.h"
|
#include "content/public/browser/gpu_data_manager_observer.h"
|
||||||
|
@ -28,26 +27,12 @@ class Dictionary;
|
||||||
|
|
||||||
namespace atom {
|
namespace atom {
|
||||||
|
|
||||||
|
struct SetSizeParams;
|
||||||
|
class WebViewGuestDelegate;
|
||||||
|
|
||||||
namespace api {
|
namespace api {
|
||||||
|
|
||||||
// A struct of parameters for SetSize(). The parameters are all declared as
|
|
||||||
// scoped pointers since they are all optional. Null pointers indicate that the
|
|
||||||
// parameter has not been provided, and the last used value should be used. Note
|
|
||||||
// that when |enable_auto_size| is true, providing |normal_size| is not
|
|
||||||
// meaningful. This is because the normal size of the guestview is overridden
|
|
||||||
// whenever autosizing occurs.
|
|
||||||
struct SetSizeParams {
|
|
||||||
SetSizeParams() {}
|
|
||||||
~SetSizeParams() {}
|
|
||||||
|
|
||||||
scoped_ptr<bool> enable_auto_size;
|
|
||||||
scoped_ptr<gfx::Size> min_size;
|
|
||||||
scoped_ptr<gfx::Size> max_size;
|
|
||||||
scoped_ptr<gfx::Size> normal_size;
|
|
||||||
};
|
|
||||||
|
|
||||||
class WebContents : public mate::TrackableObject<WebContents>,
|
class WebContents : public mate::TrackableObject<WebContents>,
|
||||||
public content::BrowserPluginGuestDelegate,
|
|
||||||
public CommonWebContentsDelegate,
|
public CommonWebContentsDelegate,
|
||||||
public content::WebContentsObserver,
|
public content::WebContentsObserver,
|
||||||
public content::GpuDataManagerObserver {
|
public content::GpuDataManagerObserver {
|
||||||
|
@ -115,18 +100,11 @@ class WebContents : public mate::TrackableObject<WebContents>,
|
||||||
bool SendIPCMessage(const base::string16& channel,
|
bool SendIPCMessage(const base::string16& channel,
|
||||||
const base::ListValue& args);
|
const base::ListValue& args);
|
||||||
|
|
||||||
// Used to toggle autosize mode for this GuestView, and set both the automatic
|
// Methods for creating <webview>.
|
||||||
// and normal sizes.
|
|
||||||
void SetSize(const SetSizeParams& params);
|
void SetSize(const SetSizeParams& params);
|
||||||
|
|
||||||
// Sets the transparency of the guest.
|
|
||||||
void SetAllowTransparency(bool allow);
|
void SetAllowTransparency(bool allow);
|
||||||
|
|
||||||
bool IsGuest() const;
|
bool IsGuest() const;
|
||||||
|
|
||||||
// Returns whether this guest has an associated embedder.
|
|
||||||
bool attached() const { return !!embedder_web_contents_; }
|
|
||||||
|
|
||||||
// Returns the current InspectableWebContents object, nullptr will be returned
|
// Returns the current InspectableWebContents object, nullptr will be returned
|
||||||
// if current WebContents can not beinspected, e.g. it is the devtools.
|
// if current WebContents can not beinspected, e.g. it is the devtools.
|
||||||
brightray::InspectableWebContents* inspectable_web_contents() const {
|
brightray::InspectableWebContents* inspectable_web_contents() const {
|
||||||
|
@ -204,7 +182,6 @@ class WebContents : public mate::TrackableObject<WebContents>,
|
||||||
const content::LoadCommittedDetails& details,
|
const content::LoadCommittedDetails& details,
|
||||||
const content::FrameNavigateParams& params) override;
|
const content::FrameNavigateParams& params) override;
|
||||||
bool OnMessageReceived(const IPC::Message& message) override;
|
bool OnMessageReceived(const IPC::Message& message) override;
|
||||||
void RenderViewReady() override;
|
|
||||||
void WebContentsDestroyed() override;
|
void WebContentsDestroyed() override;
|
||||||
void NavigationEntryCommitted(
|
void NavigationEntryCommitted(
|
||||||
const content::LoadCommittedDetails& load_details) override;
|
const content::LoadCommittedDetails& load_details) override;
|
||||||
|
@ -214,15 +191,6 @@ class WebContents : public mate::TrackableObject<WebContents>,
|
||||||
void PluginCrashed(const base::FilePath& plugin_path,
|
void PluginCrashed(const base::FilePath& plugin_path,
|
||||||
base::ProcessId plugin_pid) override;
|
base::ProcessId plugin_pid) override;
|
||||||
|
|
||||||
// content::BrowserPluginGuestDelegate:
|
|
||||||
void DidAttach(int guest_proxy_routing_id) final;
|
|
||||||
content::WebContents* GetOwnerWebContents() const final;
|
|
||||||
void GuestSizeChanged(const gfx::Size& new_size) final;
|
|
||||||
void SetGuestHost(content::GuestHost* guest_host) final;
|
|
||||||
void WillAttach(content::WebContents* embedder_web_contents,
|
|
||||||
int element_instance_id,
|
|
||||||
bool is_full_page_plugin) final;
|
|
||||||
|
|
||||||
// content::GpuDataManagerObserver:
|
// content::GpuDataManagerObserver:
|
||||||
void OnGpuProcessCrashed(base::TerminationStatus exit_code) override;
|
void OnGpuProcessCrashed(base::TerminationStatus exit_code) override;
|
||||||
|
|
||||||
|
@ -242,49 +210,9 @@ class WebContents : public mate::TrackableObject<WebContents>,
|
||||||
const base::ListValue& args,
|
const base::ListValue& args,
|
||||||
IPC::Message* message);
|
IPC::Message* message);
|
||||||
|
|
||||||
// This method is invoked when the contents auto-resized to give the container
|
|
||||||
// an opportunity to match it if it wishes.
|
|
||||||
//
|
|
||||||
// This gives the derived class an opportunity to inform its container element
|
|
||||||
// or perform other actions.
|
|
||||||
void GuestSizeChangedDueToAutoSize(const gfx::Size& old_size,
|
|
||||||
const gfx::Size& new_size);
|
|
||||||
|
|
||||||
// Returns the default size of the guestview.
|
|
||||||
gfx::Size GetDefaultSize() const;
|
|
||||||
|
|
||||||
v8::Global<v8::Value> session_;
|
v8::Global<v8::Value> session_;
|
||||||
|
|
||||||
// Stores whether the contents of the guest can be transparent.
|
scoped_ptr<WebViewGuestDelegate> guest_delegate_;
|
||||||
bool guest_opaque_;
|
|
||||||
|
|
||||||
// The WebContents that attaches this guest view.
|
|
||||||
content::WebContents* embedder_web_contents_;
|
|
||||||
|
|
||||||
// The size of the container element.
|
|
||||||
gfx::Size element_size_;
|
|
||||||
|
|
||||||
// The size of the guest content. Note: In autosize mode, the container
|
|
||||||
// element may not match the size of the guest.
|
|
||||||
gfx::Size guest_size_;
|
|
||||||
|
|
||||||
// A pointer to the guest_host.
|
|
||||||
content::GuestHost* guest_host_;
|
|
||||||
|
|
||||||
// Indicates whether autosize mode is enabled or not.
|
|
||||||
bool auto_size_enabled_;
|
|
||||||
|
|
||||||
// The maximum size constraints of the container element in autosize mode.
|
|
||||||
gfx::Size max_auto_size_;
|
|
||||||
|
|
||||||
// The minimum size constraints of the container element in autosize mode.
|
|
||||||
gfx::Size min_auto_size_;
|
|
||||||
|
|
||||||
// The size that will be used when autosize mode is disabled.
|
|
||||||
gfx::Size normal_size_;
|
|
||||||
|
|
||||||
// Whether the guest view is inside a plugin document.
|
|
||||||
bool is_full_page_plugin_;
|
|
||||||
|
|
||||||
// The type of current WebContents.
|
// The type of current WebContents.
|
||||||
Type type_;
|
Type type_;
|
||||||
|
|
|
@ -25,9 +25,6 @@ class EventEmitter : public Wrappable {
|
||||||
public:
|
public:
|
||||||
typedef std::vector<v8::Local<v8::Value>> ValueArray;
|
typedef std::vector<v8::Local<v8::Value>> ValueArray;
|
||||||
|
|
||||||
protected:
|
|
||||||
EventEmitter();
|
|
||||||
|
|
||||||
// this.emit(name, new Event(), args...);
|
// this.emit(name, new Event(), args...);
|
||||||
template<typename... Args>
|
template<typename... Args>
|
||||||
bool Emit(const base::StringPiece& name, const Args&... args) {
|
bool Emit(const base::StringPiece& name, const Args&... args) {
|
||||||
|
@ -48,6 +45,9 @@ class EventEmitter : public Wrappable {
|
||||||
StringToV8(isolate(), "defaultPrevented"))->BooleanValue();
|
StringToV8(isolate(), "defaultPrevented"))->BooleanValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
EventEmitter();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
v8::Local<v8::Object> CreateEvent(v8::Isolate* isolate,
|
v8::Local<v8::Object> CreateEvent(v8::Isolate* isolate,
|
||||||
content::WebContents* sender,
|
content::WebContents* sender,
|
||||||
|
|
175
atom/browser/web_view_guest_delegate.cc
Normal file
175
atom/browser/web_view_guest_delegate.cc
Normal file
|
@ -0,0 +1,175 @@
|
||||||
|
// Copyright (c) 2015 GitHub, Inc.
|
||||||
|
// Use of this source code is governed by the MIT license that can be
|
||||||
|
// found in the LICENSE file.
|
||||||
|
|
||||||
|
#include "atom/browser/web_view_guest_delegate.h"
|
||||||
|
|
||||||
|
#include "atom/browser/api/atom_api_web_contents.h"
|
||||||
|
#include "content/public/browser/guest_host.h"
|
||||||
|
#include "content/public/browser/render_view_host.h"
|
||||||
|
#include "content/public/browser/render_widget_host_view.h"
|
||||||
|
|
||||||
|
namespace atom {
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
const int kDefaultWidth = 300;
|
||||||
|
const int kDefaultHeight = 300;
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
WebViewGuestDelegate::WebViewGuestDelegate()
|
||||||
|
: guest_opaque_(true),
|
||||||
|
guest_host_(nullptr),
|
||||||
|
auto_size_enabled_(false),
|
||||||
|
is_full_page_plugin_(false),
|
||||||
|
api_web_contents_(nullptr) {
|
||||||
|
}
|
||||||
|
|
||||||
|
WebViewGuestDelegate::~WebViewGuestDelegate() {
|
||||||
|
}
|
||||||
|
|
||||||
|
void WebViewGuestDelegate::Initialize(api::WebContents* api_web_contents) {
|
||||||
|
api_web_contents_ = api_web_contents;
|
||||||
|
Observe(api_web_contents->GetWebContents());
|
||||||
|
}
|
||||||
|
|
||||||
|
void WebViewGuestDelegate::Destroy() {
|
||||||
|
// Give the content module an opportunity to perform some cleanup.
|
||||||
|
guest_host_->WillDestroy();
|
||||||
|
guest_host_ = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void WebViewGuestDelegate::SetSize(const SetSizeParams& params) {
|
||||||
|
bool enable_auto_size =
|
||||||
|
params.enable_auto_size ? *params.enable_auto_size : auto_size_enabled_;
|
||||||
|
gfx::Size min_size = params.min_size ? *params.min_size : min_auto_size_;
|
||||||
|
gfx::Size max_size = params.max_size ? *params.max_size : max_auto_size_;
|
||||||
|
|
||||||
|
if (params.normal_size)
|
||||||
|
normal_size_ = *params.normal_size;
|
||||||
|
|
||||||
|
min_auto_size_ = min_size;
|
||||||
|
min_auto_size_.SetToMin(max_size);
|
||||||
|
max_auto_size_ = max_size;
|
||||||
|
max_auto_size_.SetToMax(min_size);
|
||||||
|
|
||||||
|
enable_auto_size &= !min_auto_size_.IsEmpty() && !max_auto_size_.IsEmpty();
|
||||||
|
|
||||||
|
auto rvh = web_contents()->GetRenderViewHost();
|
||||||
|
if (enable_auto_size) {
|
||||||
|
// Autosize is being enabled.
|
||||||
|
rvh->EnableAutoResize(min_auto_size_, max_auto_size_);
|
||||||
|
normal_size_.SetSize(0, 0);
|
||||||
|
} else {
|
||||||
|
// Autosize is being disabled.
|
||||||
|
// Use default width/height if missing from partially defined normal size.
|
||||||
|
if (normal_size_.width() && !normal_size_.height())
|
||||||
|
normal_size_.set_height(GetDefaultSize().height());
|
||||||
|
if (!normal_size_.width() && normal_size_.height())
|
||||||
|
normal_size_.set_width(GetDefaultSize().width());
|
||||||
|
|
||||||
|
gfx::Size new_size;
|
||||||
|
if (!normal_size_.IsEmpty()) {
|
||||||
|
new_size = normal_size_;
|
||||||
|
} else if (!guest_size_.IsEmpty()) {
|
||||||
|
new_size = guest_size_;
|
||||||
|
} else {
|
||||||
|
new_size = GetDefaultSize();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (auto_size_enabled_) {
|
||||||
|
// Autosize was previously enabled.
|
||||||
|
rvh->DisableAutoResize(new_size);
|
||||||
|
GuestSizeChangedDueToAutoSize(guest_size_, new_size);
|
||||||
|
} else {
|
||||||
|
// Autosize was already disabled.
|
||||||
|
guest_host_->SizeContents(new_size);
|
||||||
|
}
|
||||||
|
|
||||||
|
guest_size_ = new_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto_size_enabled_ = enable_auto_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
void WebViewGuestDelegate::SetAllowTransparency(bool allow) {
|
||||||
|
if (guest_opaque_ != allow)
|
||||||
|
return;
|
||||||
|
|
||||||
|
auto render_view_host = web_contents()->GetRenderViewHost();
|
||||||
|
guest_opaque_ = !allow;
|
||||||
|
if (!render_view_host->GetView())
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (guest_opaque_) {
|
||||||
|
render_view_host->GetView()->SetBackgroundColorToDefault();
|
||||||
|
} else {
|
||||||
|
render_view_host->GetView()->SetBackgroundColor(SK_ColorTRANSPARENT);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void WebViewGuestDelegate::HandleKeyboardEvent(
|
||||||
|
content::WebContents* source,
|
||||||
|
const content::NativeWebKeyboardEvent& event) {
|
||||||
|
if (embedder_web_contents_)
|
||||||
|
embedder_web_contents_->GetDelegate()->HandleKeyboardEvent(source, event);
|
||||||
|
}
|
||||||
|
|
||||||
|
void WebViewGuestDelegate::RenderViewReady() {
|
||||||
|
// We don't want to accidentally set the opacity of an interstitial page.
|
||||||
|
// WebContents::GetRenderWidgetHostView will return the RWHV of an
|
||||||
|
// interstitial page if one is showing at this time. We only want opacity
|
||||||
|
// to apply to web pages.
|
||||||
|
auto render_view_host_view = web_contents()->GetRenderViewHost()->GetView();
|
||||||
|
if (guest_opaque_)
|
||||||
|
render_view_host_view->SetBackgroundColorToDefault();
|
||||||
|
else
|
||||||
|
render_view_host_view->SetBackgroundColor(SK_ColorTRANSPARENT);
|
||||||
|
}
|
||||||
|
|
||||||
|
void WebViewGuestDelegate::DidAttach(int guest_proxy_routing_id) {
|
||||||
|
api_web_contents_->Emit("did-attach");
|
||||||
|
}
|
||||||
|
|
||||||
|
content::WebContents* WebViewGuestDelegate::GetOwnerWebContents() const {
|
||||||
|
return embedder_web_contents_;
|
||||||
|
}
|
||||||
|
|
||||||
|
void WebViewGuestDelegate::GuestSizeChanged(const gfx::Size& new_size) {
|
||||||
|
if (!auto_size_enabled_)
|
||||||
|
return;
|
||||||
|
GuestSizeChangedDueToAutoSize(guest_size_, new_size);
|
||||||
|
guest_size_ = new_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
void WebViewGuestDelegate::SetGuestHost(content::GuestHost* guest_host) {
|
||||||
|
guest_host_ = guest_host;
|
||||||
|
}
|
||||||
|
|
||||||
|
void WebViewGuestDelegate::WillAttach(
|
||||||
|
content::WebContents* embedder_web_contents,
|
||||||
|
int element_instance_id,
|
||||||
|
bool is_full_page_plugin) {
|
||||||
|
embedder_web_contents_ = embedder_web_contents;
|
||||||
|
is_full_page_plugin_ = is_full_page_plugin;
|
||||||
|
}
|
||||||
|
|
||||||
|
void WebViewGuestDelegate::GuestSizeChangedDueToAutoSize(
|
||||||
|
const gfx::Size& old_size, const gfx::Size& new_size) {
|
||||||
|
api_web_contents_->Emit("size-changed",
|
||||||
|
old_size.width(), old_size.height(),
|
||||||
|
new_size.width(), new_size.height());
|
||||||
|
}
|
||||||
|
|
||||||
|
gfx::Size WebViewGuestDelegate::GetDefaultSize() const {
|
||||||
|
if (is_full_page_plugin_) {
|
||||||
|
// Full page plugins default to the size of the owner's viewport.
|
||||||
|
return embedder_web_contents_->GetRenderWidgetHostView()
|
||||||
|
->GetVisibleViewportSize();
|
||||||
|
} else {
|
||||||
|
return gfx::Size(kDefaultWidth, kDefaultHeight);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace atom
|
122
atom/browser/web_view_guest_delegate.h
Normal file
122
atom/browser/web_view_guest_delegate.h
Normal file
|
@ -0,0 +1,122 @@
|
||||||
|
// Copyright (c) 2015 GitHub, Inc.
|
||||||
|
// Use of this source code is governed by the MIT license that can be
|
||||||
|
// found in the LICENSE file.
|
||||||
|
|
||||||
|
#ifndef ATOM_BROWSER_WEB_VIEW_GUEST_DELEGATE_H_
|
||||||
|
#define ATOM_BROWSER_WEB_VIEW_GUEST_DELEGATE_H_
|
||||||
|
|
||||||
|
#include "content/public/browser/browser_plugin_guest_delegate.h"
|
||||||
|
#include "content/public/browser/web_contents_observer.h"
|
||||||
|
|
||||||
|
namespace content {
|
||||||
|
struct NativeWebKeyboardEvent;
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace atom {
|
||||||
|
|
||||||
|
namespace api {
|
||||||
|
class WebContents;
|
||||||
|
}
|
||||||
|
|
||||||
|
// A struct of parameters for SetSize(). The parameters are all declared as
|
||||||
|
// scoped pointers since they are all optional. Null pointers indicate that the
|
||||||
|
// parameter has not been provided, and the last used value should be used. Note
|
||||||
|
// that when |enable_auto_size| is true, providing |normal_size| is not
|
||||||
|
// meaningful. This is because the normal size of the guestview is overridden
|
||||||
|
// whenever autosizing occurs.
|
||||||
|
struct SetSizeParams {
|
||||||
|
SetSizeParams() {}
|
||||||
|
~SetSizeParams() {}
|
||||||
|
|
||||||
|
scoped_ptr<bool> enable_auto_size;
|
||||||
|
scoped_ptr<gfx::Size> min_size;
|
||||||
|
scoped_ptr<gfx::Size> max_size;
|
||||||
|
scoped_ptr<gfx::Size> normal_size;
|
||||||
|
};
|
||||||
|
|
||||||
|
class WebViewGuestDelegate : public content::BrowserPluginGuestDelegate,
|
||||||
|
public content::WebContentsObserver {
|
||||||
|
public:
|
||||||
|
WebViewGuestDelegate();
|
||||||
|
~WebViewGuestDelegate() override;
|
||||||
|
|
||||||
|
void Initialize(api::WebContents* api_web_contents);
|
||||||
|
|
||||||
|
// Called when the WebContents is going to be destroyed.
|
||||||
|
void Destroy();
|
||||||
|
|
||||||
|
// Used to toggle autosize mode for this GuestView, and set both the automatic
|
||||||
|
// and normal sizes.
|
||||||
|
void SetSize(const SetSizeParams& params);
|
||||||
|
|
||||||
|
// Sets the transparency of the guest.
|
||||||
|
void SetAllowTransparency(bool allow);
|
||||||
|
|
||||||
|
// Transfer the keyboard event to embedder.
|
||||||
|
void HandleKeyboardEvent(content::WebContents* source,
|
||||||
|
const content::NativeWebKeyboardEvent& event);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
// content::WebContentsObserver:
|
||||||
|
void RenderViewReady() override;
|
||||||
|
|
||||||
|
// content::BrowserPluginGuestDelegate:
|
||||||
|
void DidAttach(int guest_proxy_routing_id) final;
|
||||||
|
content::WebContents* GetOwnerWebContents() const final;
|
||||||
|
void GuestSizeChanged(const gfx::Size& new_size) final;
|
||||||
|
void SetGuestHost(content::GuestHost* guest_host) final;
|
||||||
|
void WillAttach(content::WebContents* embedder_web_contents,
|
||||||
|
int element_instance_id,
|
||||||
|
bool is_full_page_plugin) final;
|
||||||
|
|
||||||
|
private:
|
||||||
|
// This method is invoked when the contents auto-resized to give the container
|
||||||
|
// an opportunity to match it if it wishes.
|
||||||
|
//
|
||||||
|
// This gives the derived class an opportunity to inform its container element
|
||||||
|
// or perform other actions.
|
||||||
|
void GuestSizeChangedDueToAutoSize(const gfx::Size& old_size,
|
||||||
|
const gfx::Size& new_size);
|
||||||
|
|
||||||
|
// Returns the default size of the guestview.
|
||||||
|
gfx::Size GetDefaultSize() const;
|
||||||
|
|
||||||
|
// Stores whether the contents of the guest can be transparent.
|
||||||
|
bool guest_opaque_;
|
||||||
|
|
||||||
|
// The WebContents that attaches this guest view.
|
||||||
|
content::WebContents* embedder_web_contents_;
|
||||||
|
|
||||||
|
// The size of the container element.
|
||||||
|
gfx::Size element_size_;
|
||||||
|
|
||||||
|
// The size of the guest content. Note: In autosize mode, the container
|
||||||
|
// element may not match the size of the guest.
|
||||||
|
gfx::Size guest_size_;
|
||||||
|
|
||||||
|
// A pointer to the guest_host.
|
||||||
|
content::GuestHost* guest_host_;
|
||||||
|
|
||||||
|
// Indicates whether autosize mode is enabled or not.
|
||||||
|
bool auto_size_enabled_;
|
||||||
|
|
||||||
|
// The maximum size constraints of the container element in autosize mode.
|
||||||
|
gfx::Size max_auto_size_;
|
||||||
|
|
||||||
|
// The minimum size constraints of the container element in autosize mode.
|
||||||
|
gfx::Size min_auto_size_;
|
||||||
|
|
||||||
|
// The size that will be used when autosize mode is disabled.
|
||||||
|
gfx::Size normal_size_;
|
||||||
|
|
||||||
|
// Whether the guest view is inside a plugin document.
|
||||||
|
bool is_full_page_plugin_;
|
||||||
|
|
||||||
|
api::WebContents* api_web_contents_;
|
||||||
|
|
||||||
|
DISALLOW_COPY_AND_ASSIGN(WebViewGuestDelegate);
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace atom
|
||||||
|
|
||||||
|
#endif // ATOM_BROWSER_WEB_VIEW_GUEST_DELEGATE_H_
|
|
@ -207,6 +207,8 @@
|
||||||
'atom/browser/ui/x/window_state_watcher.h',
|
'atom/browser/ui/x/window_state_watcher.h',
|
||||||
'atom/browser/ui/x/x_window_utils.cc',
|
'atom/browser/ui/x/x_window_utils.cc',
|
||||||
'atom/browser/ui/x/x_window_utils.h',
|
'atom/browser/ui/x/x_window_utils.h',
|
||||||
|
'atom/browser/web_view_guest_delegate.cc',
|
||||||
|
'atom/browser/web_view_guest_delegate.h',
|
||||||
'atom/browser/web_view_manager.cc',
|
'atom/browser/web_view_manager.cc',
|
||||||
'atom/browser/web_view_manager.h',
|
'atom/browser/web_view_manager.h',
|
||||||
'atom/browser/web_dialog_helper.cc',
|
'atom/browser/web_dialog_helper.cc',
|
||||||
|
|
Loading…
Reference in a new issue