refactor: mojofy draggable regions (#18536)

This commit is contained in:
Jeremy Apthorp 2019-06-03 10:43:04 -07:00 committed by GitHub
parent 52c76d737a
commit 0b25176893
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 59 additions and 91 deletions

View file

@ -170,17 +170,6 @@ void BrowserWindow::OnRendererUnresponsive(content::RenderProcessHost*) {
ScheduleUnresponsiveEvent(50); ScheduleUnresponsiveEvent(50);
} }
bool BrowserWindow::OnMessageReceived(const IPC::Message& message,
content::RenderFrameHost* rfh) {
bool handled = true;
IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(BrowserWindow, message, rfh)
IPC_MESSAGE_HANDLER(AtomFrameHostMsg_UpdateDraggableRegions,
UpdateDraggableRegions)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
return handled;
}
void BrowserWindow::OnCloseContents() { void BrowserWindow::OnCloseContents() {
// On some machines it may happen that the window gets destroyed for twice, // On some machines it may happen that the window gets destroyed for twice,
// checking web_contents() can effectively guard against that. // checking web_contents() can effectively guard against that.
@ -216,6 +205,11 @@ void BrowserWindow::OnRendererResponsive() {
Emit("responsive"); Emit("responsive");
} }
void BrowserWindow::OnDraggableRegionsUpdated(
const std::vector<mojom::DraggableRegionPtr>& regions) {
UpdateDraggableRegions(regions);
}
void BrowserWindow::RequestPreferredWidth(int* width) { void BrowserWindow::RequestPreferredWidth(int* width) {
*width = web_contents()->GetPreferredSize().width(); *width = web_contents()->GetPreferredSize().width();
} }
@ -276,7 +270,7 @@ void BrowserWindow::OnWindowFocus() {
void BrowserWindow::OnWindowResize() { void BrowserWindow::OnWindowResize() {
#if defined(OS_MACOSX) #if defined(OS_MACOSX)
if (!draggable_regions_.empty()) if (!draggable_regions_.empty())
UpdateDraggableRegions(nullptr, draggable_regions_); UpdateDraggableRegions(draggable_regions_);
#endif #endif
TopLevelWindow::OnWindowResize(); TopLevelWindow::OnWindowResize();
} }
@ -314,28 +308,28 @@ void BrowserWindow::SetBrowserView(v8::Local<v8::Value> value) {
TopLevelWindow::ResetBrowserViews(); TopLevelWindow::ResetBrowserViews();
TopLevelWindow::AddBrowserView(value); TopLevelWindow::AddBrowserView(value);
#if defined(OS_MACOSX) #if defined(OS_MACOSX)
UpdateDraggableRegions(nullptr, draggable_regions_); UpdateDraggableRegions(draggable_regions_);
#endif #endif
} }
void BrowserWindow::AddBrowserView(v8::Local<v8::Value> value) { void BrowserWindow::AddBrowserView(v8::Local<v8::Value> value) {
TopLevelWindow::AddBrowserView(value); TopLevelWindow::AddBrowserView(value);
#if defined(OS_MACOSX) #if defined(OS_MACOSX)
UpdateDraggableRegions(nullptr, draggable_regions_); UpdateDraggableRegions(draggable_regions_);
#endif #endif
} }
void BrowserWindow::RemoveBrowserView(v8::Local<v8::Value> value) { void BrowserWindow::RemoveBrowserView(v8::Local<v8::Value> value) {
TopLevelWindow::RemoveBrowserView(value); TopLevelWindow::RemoveBrowserView(value);
#if defined(OS_MACOSX) #if defined(OS_MACOSX)
UpdateDraggableRegions(nullptr, draggable_regions_); UpdateDraggableRegions(draggable_regions_);
#endif #endif
} }
void BrowserWindow::ResetBrowserViews() { void BrowserWindow::ResetBrowserViews() {
TopLevelWindow::ResetBrowserViews(); TopLevelWindow::ResetBrowserViews();
#if defined(OS_MACOSX) #if defined(OS_MACOSX)
UpdateDraggableRegions(nullptr, draggable_regions_); UpdateDraggableRegions(draggable_regions_);
#endif #endif
} }
@ -377,13 +371,13 @@ v8::Local<v8::Value> BrowserWindow::GetWebContents(v8::Isolate* isolate) {
// Convert draggable regions in raw format to SkRegion format. // Convert draggable regions in raw format to SkRegion format.
std::unique_ptr<SkRegion> BrowserWindow::DraggableRegionsToSkRegion( std::unique_ptr<SkRegion> BrowserWindow::DraggableRegionsToSkRegion(
const std::vector<DraggableRegion>& regions) { const std::vector<mojom::DraggableRegionPtr>& regions) {
auto sk_region = std::make_unique<SkRegion>(); auto sk_region = std::make_unique<SkRegion>();
for (const DraggableRegion& region : regions) { for (const auto& region : regions) {
sk_region->op( sk_region->op(
region.bounds.x(), region.bounds.y(), region.bounds.right(), region->bounds.x(), region->bounds.y(), region->bounds.right(),
region.bounds.bottom(), region->bounds.bottom(),
region.draggable ? SkRegion::kUnion_Op : SkRegion::kDifference_Op); region->draggable ? SkRegion::kUnion_Op : SkRegion::kDifference_Op);
} }
return sk_region; return sk_region;
} }

View file

@ -51,12 +51,12 @@ class BrowserWindow : public TopLevelWindow,
void DidFirstVisuallyNonEmptyPaint() override; void DidFirstVisuallyNonEmptyPaint() override;
void BeforeUnloadDialogCancelled() override; void BeforeUnloadDialogCancelled() override;
void OnRendererUnresponsive(content::RenderProcessHost*) override; void OnRendererUnresponsive(content::RenderProcessHost*) override;
bool OnMessageReceived(const IPC::Message& message,
content::RenderFrameHost* rfh) override;
// ExtendedWebContentsObserver: // ExtendedWebContentsObserver:
void OnCloseContents() override; void OnCloseContents() override;
void OnRendererResponsive() override; void OnRendererResponsive() override;
void OnDraggableRegionsUpdated(
const std::vector<mojom::DraggableRegionPtr>& regions) override;
// NativeWindowObserver: // NativeWindowObserver:
void RequestPreferredWidth(int* width) override; void RequestPreferredWidth(int* width) override;
@ -91,12 +91,12 @@ class BrowserWindow : public TopLevelWindow,
// Helpers. // Helpers.
// Called when the window needs to update its draggable region. // Called when the window needs to update its draggable region.
void UpdateDraggableRegions(content::RenderFrameHost* rfh, void UpdateDraggableRegions(
const std::vector<DraggableRegion>& regions); const std::vector<mojom::DraggableRegionPtr>& regions);
// Convert draggable regions in raw format to SkRegion format. // Convert draggable regions in raw format to SkRegion format.
std::unique_ptr<SkRegion> DraggableRegionsToSkRegion( std::unique_ptr<SkRegion> DraggableRegionsToSkRegion(
const std::vector<DraggableRegion>& regions); const std::vector<mojom::DraggableRegionPtr>& regions);
// Schedule a notification unresponsive event. // Schedule a notification unresponsive event.
void ScheduleUnresponsiveEvent(int ms); void ScheduleUnresponsiveEvent(int ms);
@ -112,7 +112,7 @@ class BrowserWindow : public TopLevelWindow,
base::CancelableClosure window_unresponsive_closure_; base::CancelableClosure window_unresponsive_closure_;
#if defined(OS_MACOSX) #if defined(OS_MACOSX)
std::vector<DraggableRegion> draggable_regions_; std::vector<mojom::DraggableRegionPtr> draggable_regions_;
#endif #endif
v8::Global<v8::Value> web_contents_; v8::Global<v8::Value> web_contents_;

View file

@ -12,7 +12,6 @@
#include "atom/browser/native_browser_view.h" #include "atom/browser/native_browser_view.h"
#include "atom/browser/native_window_mac.h" #include "atom/browser/native_window_mac.h"
#include "atom/browser/ui/inspectable_web_contents_view.h" #include "atom/browser/ui/inspectable_web_contents_view.h"
#include "atom/common/draggable_region.h"
#include "base/mac/scoped_nsobject.h" #include "base/mac/scoped_nsobject.h"
@interface NSView (WebContentsView) @interface NSView (WebContentsView)
@ -76,8 +75,7 @@ void BrowserWindow::OverrideNSWindowContentView(InspectableWebContents* iwc) {
} }
void BrowserWindow::UpdateDraggableRegions( void BrowserWindow::UpdateDraggableRegions(
content::RenderFrameHost* rfh, const std::vector<mojom::DraggableRegionPtr>& regions) {
const std::vector<DraggableRegion>& regions) {
if (window_->has_frame()) if (window_->has_frame())
return; return;
@ -103,7 +101,9 @@ void BrowserWindow::UpdateDraggableRegions(
// Draggable regions is implemented by having the whole web view draggable // Draggable regions is implemented by having the whole web view draggable
// (mouseDownCanMoveWindow) and overlaying regions that are not draggable. // (mouseDownCanMoveWindow) and overlaying regions that are not draggable.
draggable_regions_ = regions; draggable_regions_.clear();
for (const auto& r : regions)
draggable_regions_.push_back(r.Clone());
std::vector<gfx::Rect> drag_exclude_rects; std::vector<gfx::Rect> drag_exclude_rects;
if (regions.empty()) { if (regions.empty()) {
drag_exclude_rects.push_back(gfx::Rect(0, 0, webViewWidth, webViewHeight)); drag_exclude_rects.push_back(gfx::Rect(0, 0, webViewWidth, webViewHeight));
@ -114,7 +114,7 @@ void BrowserWindow::UpdateDraggableRegions(
auto browser_views = window_->browser_views(); auto browser_views = window_->browser_views();
for (NativeBrowserView* view : browser_views) { for (NativeBrowserView* view : browser_views) {
(view)->UpdateDraggableRegions(drag_exclude_rects); view->UpdateDraggableRegions(drag_exclude_rects);
} }
// Create and add a ControlRegionView for each region that needs to be // Create and add a ControlRegionView for each region that needs to be

View file

@ -11,8 +11,7 @@ namespace atom {
namespace api { namespace api {
void BrowserWindow::UpdateDraggableRegions( void BrowserWindow::UpdateDraggableRegions(
content::RenderFrameHost* rfh, const std::vector<mojom::DraggableRegionPtr>& regions) {
const std::vector<DraggableRegion>& regions) {
if (window_->has_frame()) if (window_->has_frame())
return; return;
static_cast<NativeWindowViews*>(window_.get()) static_cast<NativeWindowViews*>(window_.get())

View file

@ -964,6 +964,12 @@ void WebContents::MessageHost(const std::string& channel,
base::nullopt, channel, std::move(arguments)); base::nullopt, channel, std::move(arguments));
} }
void WebContents::UpdateDraggableRegions(
std::vector<mojom::DraggableRegionPtr> regions) {
for (ExtendedWebContentsObserver& observer : observers_)
observer.OnDraggableRegionsUpdated(regions);
}
void WebContents::RenderFrameDeleted( void WebContents::RenderFrameDeleted(
content::RenderFrameHost* render_frame_host) { content::RenderFrameHost* render_frame_host) {
// A RenderFrameHost can be destroyed before the related Mojo binding is // A RenderFrameHost can be destroyed before the related Mojo binding is

View file

@ -69,6 +69,8 @@ class ExtendedWebContentsObserver : public base::CheckedObserver {
public: public:
virtual void OnCloseContents() {} virtual void OnCloseContents() {}
virtual void OnRendererResponsive() {} virtual void OnRendererResponsive() {}
virtual void OnDraggableRegionsUpdated(
const std::vector<mojom::DraggableRegionPtr>& regions) {}
protected: protected:
~ExtendedWebContentsObserver() override {} ~ExtendedWebContentsObserver() override {}
@ -505,6 +507,8 @@ class WebContents : public mate::TrackableObject<WebContents>,
const std::string& channel, const std::string& channel,
base::Value arguments) override; base::Value arguments) override;
void MessageHost(const std::string& channel, base::Value arguments) override; void MessageHost(const std::string& channel, base::Value arguments) override;
void UpdateDraggableRegions(
std::vector<mojom::DraggableRegionPtr> regions) override;
// Called when we receive a CursorChange message from chromium. // Called when we receive a CursorChange message from chromium.
void OnCursorChange(const content::WebCursor& cursor); void OnCursorChange(const content::WebCursor& cursor);

View file

@ -7,7 +7,6 @@
#include <vector> #include <vector>
#include "atom/common/draggable_region.h"
#include "base/macros.h" #include "base/macros.h"
#include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents.h"
#include "third_party/skia/include/core/SkColor.h" #include "third_party/skia/include/core/SkColor.h"

View file

@ -9,7 +9,6 @@
#include <vector> #include <vector>
#include "atom/browser/native_browser_view.h" #include "atom/browser/native_browser_view.h"
#include "atom/common/draggable_region.h"
#include "base/mac/scoped_nsobject.h" #include "base/mac/scoped_nsobject.h"
namespace atom { namespace atom {

View file

@ -44,8 +44,6 @@ namespace atom {
class AtomMenuModel; class AtomMenuModel;
class NativeBrowserView; class NativeBrowserView;
struct DraggableRegion;
#if defined(OS_MACOSX) #if defined(OS_MACOSX)
typedef NSView* NativeWindowHandle; typedef NSView* NativeWindowHandle;
#else #else

View file

@ -22,7 +22,6 @@
#include "atom/browser/web_view_manager.h" #include "atom/browser/web_view_manager.h"
#include "atom/browser/window_list.h" #include "atom/browser/window_list.h"
#include "atom/common/atom_constants.h" #include "atom/common/atom_constants.h"
#include "atom/common/draggable_region.h"
#include "atom/common/native_mate_converters/image_converter.h" #include "atom/common/native_mate_converters/image_converter.h"
#include "atom/common/options_switches.h" #include "atom/common/options_switches.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"

View file

@ -7,5 +7,6 @@ mojom("mojo") {
public_deps = [ public_deps = [
"//mojo/public/mojom/base", "//mojo/public/mojom/base",
"//ui/gfx/geometry/mojo",
] ]
} }

View file

@ -1,6 +1,7 @@
module atom.mojom; module atom.mojom;
import "mojo/public/mojom/base/values.mojom"; import "mojo/public/mojom/base/values.mojom";
import "ui/gfx/geometry/mojo/geometry.mojom";
interface ElectronRenderer { interface ElectronRenderer {
Message( Message(
@ -13,6 +14,11 @@ interface ElectronRenderer {
TakeHeapSnapshot(handle file) => (bool success); TakeHeapSnapshot(handle file) => (bool success);
}; };
struct DraggableRegion {
bool draggable;
gfx.mojom.Rect bounds;
};
interface ElectronBrowser { interface ElectronBrowser {
// Emits an event on |channel| from the ipcMain JavaScript object in the main // Emits an event on |channel| from the ipcMain JavaScript object in the main
// process. // process.
@ -50,4 +56,7 @@ interface ElectronBrowser {
MessageHost( MessageHost(
string channel, string channel,
mojo_base.mojom.ListValue arguments); mojo_base.mojom.ListValue arguments);
UpdateDraggableRegions(
array<DraggableRegion> regions);
}; };

View file

@ -4,7 +4,6 @@
// Multiply-included file, no traditional include guard. // Multiply-included file, no traditional include guard.
#include "atom/common/draggable_region.h"
#include "base/strings/string16.h" #include "base/strings/string16.h"
#include "base/values.h" #include "base/values.h"
#include "content/public/common/common_param_traits.h" #include "content/public/common/common_param_traits.h"
@ -20,11 +19,6 @@
#define IPC_MESSAGE_START ElectronMsgStart #define IPC_MESSAGE_START ElectronMsgStart
IPC_STRUCT_TRAITS_BEGIN(atom::DraggableRegion)
IPC_STRUCT_TRAITS_MEMBER(draggable)
IPC_STRUCT_TRAITS_MEMBER(bounds)
IPC_STRUCT_TRAITS_END()
IPC_MESSAGE_ROUTED3(AtomAutofillFrameHostMsg_ShowPopup, IPC_MESSAGE_ROUTED3(AtomAutofillFrameHostMsg_ShowPopup,
gfx::RectF /* bounds */, gfx::RectF /* bounds */,
std::vector<base::string16> /* values */, std::vector<base::string16> /* values */,
@ -35,10 +29,6 @@ IPC_MESSAGE_ROUTED0(AtomAutofillFrameHostMsg_HidePopup)
IPC_MESSAGE_ROUTED1(AtomAutofillFrameMsg_AcceptSuggestion, IPC_MESSAGE_ROUTED1(AtomAutofillFrameMsg_AcceptSuggestion,
base::string16 /* suggestion */) base::string16 /* suggestion */)
// Sent by the renderer when the draggable regions are updated.
IPC_MESSAGE_ROUTED1(AtomFrameHostMsg_UpdateDraggableRegions,
std::vector<atom::DraggableRegion> /* regions */)
// Update renderer process preferences. // Update renderer process preferences.
IPC_MESSAGE_CONTROL1(AtomMsg_UpdatePreferences, base::ListValue) IPC_MESSAGE_CONTROL1(AtomMsg_UpdatePreferences, base::ListValue)

View file

@ -1,11 +0,0 @@
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "atom/common/draggable_region.h"
namespace atom {
DraggableRegion::DraggableRegion() : draggable(false) {}
} // namespace atom

View file

@ -1,21 +0,0 @@
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef ATOM_COMMON_DRAGGABLE_REGION_H_
#define ATOM_COMMON_DRAGGABLE_REGION_H_
#include "ui/gfx/geometry/rect.h"
namespace atom {
struct DraggableRegion {
bool draggable;
gfx::Rect bounds;
DraggableRegion();
};
} // namespace atom
#endif // ATOM_COMMON_DRAGGABLE_REGION_H_

View file

@ -5,6 +5,7 @@
#include "atom/renderer/atom_render_frame_observer.h" #include "atom/renderer/atom_render_frame_observer.h"
#include <string> #include <string>
#include <utility>
#include <vector> #include <vector>
#include "atom/common/api/api_messages.h" #include "atom/common/api/api_messages.h"
@ -19,11 +20,10 @@
#include "content/public/renderer/render_view.h" #include "content/public/renderer/render_view.h"
#include "electron/atom/common/api/api.mojom.h" #include "electron/atom/common/api/api.mojom.h"
#include "ipc/ipc_message_macros.h" #include "ipc/ipc_message_macros.h"
#include "mojo/public/cpp/bindings/associated_binding.h"
#include "native_mate/dictionary.h" #include "native_mate/dictionary.h"
#include "net/base/net_module.h" #include "net/base/net_module.h"
#include "net/grit/net_resources.h" #include "net/grit/net_resources.h"
#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h" #include "services/service_manager/public/cpp/interface_provider.h"
#include "third_party/blink/public/platform/web_isolated_world_info.h" #include "third_party/blink/public/platform/web_isolated_world_info.h"
#include "third_party/blink/public/web/blink.h" #include "third_party/blink/public/web/blink.h"
#include "third_party/blink/public/web/web_document.h" #include "third_party/blink/public/web/web_document.h"
@ -95,16 +95,20 @@ void AtomRenderFrameObserver::DidCreateScriptContext(
void AtomRenderFrameObserver::DraggableRegionsChanged() { void AtomRenderFrameObserver::DraggableRegionsChanged() {
blink::WebVector<blink::WebDraggableRegion> webregions = blink::WebVector<blink::WebDraggableRegion> webregions =
render_frame_->GetWebFrame()->GetDocument().DraggableRegions(); render_frame_->GetWebFrame()->GetDocument().DraggableRegions();
std::vector<DraggableRegion> regions; std::vector<mojom::DraggableRegionPtr> regions;
for (auto& webregion : webregions) { for (auto& webregion : webregions) {
DraggableRegion region; auto region = mojom::DraggableRegion::New();
render_frame_->GetRenderView()->ConvertViewportToWindowViaWidget( render_frame_->GetRenderView()->ConvertViewportToWindowViaWidget(
&webregion.bounds); &webregion.bounds);
region.bounds = webregion.bounds; region->bounds = webregion.bounds;
region.draggable = webregion.draggable; region->draggable = webregion.draggable;
regions.push_back(region); regions.push_back(std::move(region));
} }
Send(new AtomFrameHostMsg_UpdateDraggableRegions(routing_id(), regions));
mojom::ElectronBrowserPtr browser_ptr;
render_frame_->GetRemoteInterfaces()->GetInterface(
mojo::MakeRequest(&browser_ptr));
browser_ptr->UpdateDraggableRegions(std::move(regions));
} }
void AtomRenderFrameObserver::WillReleaseScriptContext( void AtomRenderFrameObserver::WillReleaseScriptContext(

View file

@ -512,8 +512,6 @@ filenames = {
"atom/common/crash_reporter/win/crash_service.h", "atom/common/crash_reporter/win/crash_service.h",
"atom/common/crash_reporter/win/crash_service_main.cc", "atom/common/crash_reporter/win/crash_service_main.cc",
"atom/common/crash_reporter/win/crash_service_main.h", "atom/common/crash_reporter/win/crash_service_main.h",
"atom/common/draggable_region.cc",
"atom/common/draggable_region.h",
"atom/common/gin_util.h", "atom/common/gin_util.h",
"atom/common/heap_snapshot.cc", "atom/common/heap_snapshot.cc",
"atom/common/heap_snapshot.h", "atom/common/heap_snapshot.h",