Skip to content
This repository has been archived by the owner on Jan 4, 2019. It is now read-only.

Commit

Permalink
fix handling of some methods for remote/sender webContents
Browse files Browse the repository at this point in the history
auditors @darkdh
  • Loading branch information
bridiver committed Dec 5, 2017
1 parent 237a6ee commit cdeecd9
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 21 deletions.
68 changes: 54 additions & 14 deletions atom/browser/api/atom_api_web_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,13 @@ WebContents::~WebContents() {
}
}

brightray::InspectableWebContents* WebContents::managed_web_contents() const {
if (type_ == REMOTE && !GetMainFrame().IsEmpty())
return GetMainFrame()->managed_web_contents();

return CommonWebContentsDelegate::managed_web_contents();
}

void WebContents::CreateWebContents(v8::Isolate* isolate,
const mate::Dictionary& options,
const content::WebContents::CreateParams& create_params) {
Expand Down Expand Up @@ -512,7 +519,7 @@ void WebContents::CompleteInit(v8::Isolate* isolate,
zoom::ZoomController::CreateForWebContents(web_contents);
brave::RendererPreferencesHelper::CreateForWebContents(web_contents);

if (GetType() == WEB_VIEW) {
if (IsGuest()) {
// Initialize autofill client
autofill::AtomAutofillClient::CreateForWebContents(web_contents);
std::string locale = static_cast<brave::BraveContentBrowserClient*>(
Expand Down Expand Up @@ -957,7 +964,7 @@ bool WebContents::IsPopupOrPanel(const content::WebContents* source) const {
void WebContents::HandleKeyboardEvent(
content::WebContents* source,
const content::NativeWebKeyboardEvent& event) {
if (type_ == WEB_VIEW && HostWebContents()) {
if (IsGuest() && HostWebContents()) {
// Send the unhandled keyboard events back to the embedder.
auto embedder =
atom::api::WebContents::CreateFrom(isolate(), HostWebContents());
Expand Down Expand Up @@ -1478,6 +1485,11 @@ bool WebContents::OnMessageReceived(const IPC::Message& message,
}

void WebContents::DestroyWebContents() {
if (type_ == REMOTE && !GetMainFrame().IsEmpty()) {
GetMainFrame()->DestroyWebContents();
return;
}

if (guest_delegate_) {
guest_delegate_->Destroy(true);
} else {
Expand Down Expand Up @@ -1585,6 +1597,14 @@ int WebContents::GetGuestInstanceId() const {
}
}

mate::Handle<WebContents> WebContents::GetMainFrame() const {
auto existing = TrackableObject::FromWrappedClass(isolate(), web_contents());
if (existing)
return mate::CreateHandle(isolate(), static_cast<WebContents*>(existing));
else
return mate::Handle<WebContents>();
}

bool WebContents::Equal(const WebContents* web_contents) const {
return ID() == web_contents->ID();
}
Expand Down Expand Up @@ -1844,14 +1864,17 @@ bool WebContents::SavePage(const base::FilePath& full_file_path,
}

void WebContents::OpenDevTools(mate::Arguments* args) {
if (type_ == REMOTE)
if (type_ == REMOTE) {
if (!GetMainFrame().IsEmpty())
GetMainFrame()->OpenDevTools(args);
return;
}

if (!enable_devtools_)
if (!enable_devtools_ || !managed_web_contents())
return;

std::string state;
if (type_ == WEB_VIEW || type_ == BACKGROUND_PAGE || !owner_window()) {
if (IsGuest() || type_ == BACKGROUND_PAGE || !owner_window()) {
state = "detach";
} else if (args && args->Length() == 1) {
bool detach = false;
Expand All @@ -1870,22 +1893,22 @@ void WebContents::OpenDevTools(mate::Arguments* args) {
}

void WebContents::CloseDevTools() {
if (type_ == REMOTE)
if (!managed_web_contents())
return;

managed_web_contents()->CloseDevTools();
}

bool WebContents::IsDevToolsOpened() {
if (type_ == REMOTE)
return false;
if (!managed_web_contents())
return;

return managed_web_contents()->IsDevToolsViewShowing();
}

bool WebContents::IsDevToolsFocused() {
if (type_ == REMOTE)
return false;
if (!managed_web_contents())
return;

return managed_web_contents()->GetView()->IsDevToolsViewFocused();
}
Expand All @@ -1898,10 +1921,13 @@ void WebContents::ToggleDevTools() {
}

void WebContents::InspectElement(int x, int y) {
if (type_ == REMOTE)
if (type_ == REMOTE) {
if (!GetMainFrame().IsEmpty())
GetMainFrame()->InspectElement(x, y);
return;
}

if (!enable_devtools_)
if (!enable_devtools_ || !managed_web_contents())
return;

if (!managed_web_contents()->GetDevToolsWebContents())
Expand All @@ -1913,10 +1939,13 @@ void WebContents::InspectElement(int x, int y) {
}

void WebContents::InspectServiceWorker() {
if (type_ == REMOTE)
if (type_ == REMOTE) {
if (!GetMainFrame().IsEmpty())
GetMainFrame()->InspectServiceWorker();
return;
}

if (!enable_devtools_)
if (!enable_devtools_ || !managed_web_contents())
return;

for (const auto& agent_host : content::DevToolsAgentHost::GetOrCreateAll()) {
Expand Down Expand Up @@ -2405,6 +2434,9 @@ void WebContents::SetSize(const SetSizeParams& params) {
}

bool WebContents::IsGuest() const {
if (type_ == REMOTE && !GetMainFrame().IsEmpty())
return GetMainFrame()->IsGuest();

return type_ == WEB_VIEW;
}

Expand All @@ -2415,6 +2447,9 @@ v8::Local<v8::Value> WebContents::GetWebPreferences(v8::Isolate* isolate) {
}

v8::Local<v8::Value> WebContents::GetOwnerBrowserWindow() {
if (type_ == REMOTE && !GetMainFrame().IsEmpty())
return GetMainFrame()->GetOwnerBrowserWindow();

if (owner_window())
return Window::From(isolate(), owner_window());
else
Expand Down Expand Up @@ -2480,6 +2515,9 @@ v8::Local<v8::Value> WebContents::TabValue() {
}

int32_t WebContents::ID() const {
if (type_ == REMOTE && !GetMainFrame().IsEmpty())
return GetMainFrame()->weak_map_id();

return weak_map_id();
}

Expand All @@ -2496,6 +2534,7 @@ v8::Local<v8::Value> WebContents::Session(v8::Isolate* isolate) {
content::WebContents* WebContents::HostWebContents() {
if (guest_delegate_)
return guest_delegate_->embedder_web_contents();

return nullptr;
}

Expand Down Expand Up @@ -2643,6 +2682,7 @@ void WebContents::BuildPrototype(v8::Isolate* isolate,
.SetMethod("neverSavePassword", &WebContents::NeverSavePassword)
.SetMethod("updatePassword", &WebContents::UpdatePassword)
.SetMethod("noUpdatePassword", &WebContents::NoUpdatePassword)
.SetProperty("mainFrame", &WebContents::GetMainFrame)
.SetProperty("session", &WebContents::Session)
.SetProperty("guestInstanceId", &WebContents::GetGuestInstanceId)
.SetProperty("hostWebContents", &WebContents::HostWebContents)
Expand Down
3 changes: 3 additions & 0 deletions atom/browser/api/atom_api_web_contents.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ class WebContents : public mate::TrackableObject<WebContents>,
static void BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype);

brightray::InspectableWebContents* managed_web_contents() const override;

void Clone(mate::Arguments* args);

void DestroyWebContents();
Expand All @@ -150,6 +152,7 @@ class WebContents : public mate::TrackableObject<WebContents>,
int GetID() const;
Type GetType() const;
int GetGuestInstanceId() const;
mate::Handle<WebContents> GetMainFrame() const;
bool Equal(const WebContents* web_contents) const;
void LoadURL(const GURL& url, const mate::Dictionary& options);
void Reload(bool ignore_cache);
Expand Down
6 changes: 0 additions & 6 deletions atom/browser/api/event_emitter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,6 @@ v8::Local<v8::Object> CreateJSEvent(
base::Bind(&atom::api::WebContents::SendIPCSharedMemory,
render_process_id, render_frame_id));

if (render_frame_host != web_contents->GetMainFrame()) {
auto mainFrame =
WebContents::CreateFrom(isolate, web_contents)->GetWrapper();
sender.Set("mainFrame", mainFrame);
}

object = handle_scope.Escape(handle.ToV8());
}
}
Expand Down
2 changes: 1 addition & 1 deletion atom/browser/common_web_contents_delegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class CommonWebContentsDelegate
// Returns the WebContents of devtools.
content::WebContents* GetDevToolsWebContents() const;

brightray::InspectableWebContents* managed_web_contents() const {
virtual brightray::InspectableWebContents* managed_web_contents() const {
return web_contents_.get();
}

Expand Down

1 comment on commit cdeecd9

@darkdh
Copy link
Member

@darkdh darkdh commented on cdeecd9 Dec 6, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

++

Please sign in to comment.