Skip to content

Commit 5860b0d

Browse files
committed
Fix linux javascript dialog
Fix nwjs#2683 Conflicts: nw.gypi src/nw_shell.cc src/shell_browser_main_parts.cc
1 parent 878f061 commit 5860b0d

7 files changed

+184
-0
lines changed

nw.gypi

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
'dependencies': [
5151
'<(DEPTH)/base/base.gyp:base',
5252
'<(DEPTH)/base/third_party/dynamic_annotations/dynamic_annotations.gyp:dynamic_annotations',
53+
'<(DEPTH)/components/components.gyp:app_modal',
5354
'<(DEPTH)/components/components.gyp:autofill_content_renderer',
5455
'<(DEPTH)/components/components.gyp:keyed_service_content',
5556
'<(DEPTH)/components/components_resources.gyp:components_resources',
@@ -77,6 +78,7 @@
7778
'<(DEPTH)/v8/tools/gyp/v8.gyp:v8',
7879
'<(DEPTH)/third_party/zlib/zlib.gyp:minizip',
7980
'<(DEPTH)/third_party/WebKit/public/blink.gyp:blink',
81+
'<(DEPTH)/components/components_strings.gyp:components_strings',
8082
'nw_resources',
8183
'commit_id',
8284
],
@@ -120,6 +122,11 @@
120122
'<(DEPTH)/chrome/browser/ui/cocoa/custom_frame_view.h',
121123
'<(DEPTH)/chrome/browser/ui/cocoa/custom_frame_view.mm',
122124
'<(DEPTH)/chrome/browser/ui/base_window.h',
125+
'<(DEPTH)/chrome/browser/ui/views/chrome_javascript_native_dialog_factory_views.cc',
126+
'<(DEPTH)/chrome/browser/ui/views/javascript_app_modal_dialog_views_x11.cc',
127+
'<(DEPTH)/chrome/browser/ui/views/javascript_app_modal_dialog_views_x11.h',
128+
'<(DEPTH)/chrome/browser/ui/views/javascript_app_modal_event_blocker_x11.cc',
129+
'<(DEPTH)/chrome/browser/ui/views/javascript_app_modal_event_blocker_x11.h',
123130
'<(DEPTH)/chrome/browser/ui/views/status_icons/status_icon_linux_wrapper.cc',
124131
'<(DEPTH)/chrome/browser/ui/views/status_icons/status_icon_linux_wrapper.h',
125132
'<(DEPTH)/chrome/browser/ui/views/status_icons/status_icon_win.cc',
@@ -268,6 +275,8 @@
268275
'src/browser/shell_download_manager_delegate.h',
269276
'src/browser/shell_download_manager_delegate_win.cc',
270277
'src/browser/shell_download_manager_delegate_mac.mm',
278+
'src/browser/nw_constrained_window_views_client.cc',
279+
'src/browser/nw_constrained_window_views_client.h',
271280
'src/browser/shell_javascript_dialog_creator.cc',
272281
'src/browser/shell_javascript_dialog_creator.h',
273282
# 'src/browser/shell_javascript_dialog_gtk.cc',
@@ -657,6 +666,7 @@
657666
'<(SHARED_INTERMEDIATE_DIR)/ui/resources/ui_resources_100_percent.pak',
658667
'<(SHARED_INTERMEDIATE_DIR)/ui/resources/webui_resources.pak',
659668
'<(SHARED_INTERMEDIATE_DIR)/ui/strings/app_locale_settings_en-US.pak',
669+
'<(SHARED_INTERMEDIATE_DIR)/components/strings/components_strings_en-US.pak',
660670
'<(SHARED_INTERMEDIATE_DIR)/ui/strings/ui_strings_en-US.pak',
661671
'<(SHARED_INTERMEDIATE_DIR)/webkit/devtools_resources.pak',
662672
'<(SHARED_INTERMEDIATE_DIR)/blink/public/resources/blink_resources.pak',

src/browser/native_window_aura.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,14 @@ gfx::Size NativeWindowFrameView::GetMaximumSize() const {
275275

276276
} // namespace
277277

278+
NativeWindowAura* NativeWindowAura::GetBrowserViewForNativeWindow(
279+
gfx::NativeWindow window) {
280+
views::Widget* widget = views::Widget::GetWidgetForNativeWindow(window);
281+
return widget ?
282+
reinterpret_cast<NativeWindowAura*>(widget->GetNativeWindowProperty(
283+
"__BROWSER_VIEW__")) : nullptr;
284+
}
285+
278286
NativeWindowAura::NativeWindowAura(const base::WeakPtr<content::Shell>& shell,
279287
base::DictionaryValue* manifest)
280288
: NativeWindow(shell, manifest),
@@ -343,6 +351,8 @@ NativeWindowAura::NativeWindowAura(const base::WeakPtr<content::Shell>& shell,
343351

344352
OnViewWasResized();
345353
window_->SetInitialFocus(ui::SHOW_STATE_NORMAL);
354+
355+
window_->SetNativeWindowProperty("__BROWSER_VIEW__", this);
346356
}
347357

348358
NativeWindowAura::~NativeWindowAura() {

src/browser/native_window_aura.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class NativeWindowAura : public NativeWindow,
5959
explicit NativeWindowAura(const base::WeakPtr<content::Shell>& shell,
6060
base::DictionaryValue* manifest);
6161
virtual ~NativeWindowAura();
62+
static NativeWindowAura* GetBrowserViewForNativeWindow(gfx::NativeWindow window);
6263

6364
SkRegion* draggable_region() { return draggable_region_.get(); }
6465
NativeWindowToolbarAura* toolbar() { return toolbar_; }
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
// Copyright 2014 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "content/nw/src/browser/nw_constrained_window_views_client.h"
6+
7+
#include "base/memory/scoped_ptr.h"
8+
#include "base/observer_list.h"
9+
#include "components/constrained_window/constrained_window_views.h"
10+
#include "components/constrained_window/constrained_window_views_client.h"
11+
#include "components/web_modal/web_contents_modal_dialog_host.h"
12+
#include "extensions/browser/guest_view/guest_view_base.h"
13+
#include "ui/aura/window.h"
14+
#include "ui/aura/window_observer.h"
15+
#include "ui/aura/window_property.h"
16+
17+
namespace nw {
18+
namespace {
19+
20+
// Provides the host environment for web modal dialogs. See
21+
// web_modal::WebContentsModalDialogHost, and ModalDialogHost for more
22+
// details.
23+
class ModalDialogHostImpl : public web_modal::WebContentsModalDialogHost,
24+
public aura::WindowObserver {
25+
public:
26+
// Returns a modal dialog host for |window|. If it doesn't exist it creates
27+
// one and stores it as owned property.
28+
static ModalDialogHost* Get(aura::Window* window);
29+
30+
private:
31+
explicit ModalDialogHostImpl(aura::Window* host_window)
32+
: host_window_(host_window) {
33+
host_window_->AddObserver(this);
34+
}
35+
~ModalDialogHostImpl() override {}
36+
37+
// web_modal::ModalDialogHost:
38+
gfx::NativeView GetHostView() const override {
39+
return host_window_;
40+
}
41+
gfx::Point GetDialogPosition(const gfx::Size& size) override {
42+
gfx::Size app_window_size = host_window_->GetBoundsInScreen().size();
43+
return gfx::Point(app_window_size.width() / 2 - size.width() / 2,
44+
app_window_size.height() / 2 - size.height() / 2);
45+
}
46+
void AddObserver(web_modal::ModalDialogHostObserver* observer) override {
47+
observer_list_.AddObserver(observer);
48+
}
49+
void RemoveObserver(web_modal::ModalDialogHostObserver* observer) override {
50+
observer_list_.RemoveObserver(observer);
51+
}
52+
53+
// web_modal::WebContensModalDialogHost:
54+
gfx::Size GetMaximumDialogSize() override {
55+
return host_window_->bounds().size();
56+
}
57+
58+
// aura::WindowObserver:
59+
void OnWindowDestroying(aura::Window* window) override {
60+
if (window != host_window_)
61+
return;
62+
host_window_->RemoveObserver(this);
63+
FOR_EACH_OBSERVER(web_modal::ModalDialogHostObserver,
64+
observer_list_,
65+
OnHostDestroying());
66+
}
67+
void OnWindowBoundsChanged(aura::Window* window,
68+
const gfx::Rect& old_bounds,
69+
const gfx::Rect& new_bounds) override {
70+
if (window != host_window_)
71+
return;
72+
FOR_EACH_OBSERVER(web_modal::ModalDialogHostObserver,
73+
observer_list_,
74+
OnPositionRequiresUpdate());
75+
}
76+
77+
aura::Window* host_window_;
78+
ObserverList<web_modal::ModalDialogHostObserver> observer_list_;
79+
80+
DISALLOW_COPY_AND_ASSIGN(ModalDialogHostImpl);
81+
};
82+
83+
// A window property key to store the modal dialog host for
84+
// dialogs created with the window as its parent.
85+
DEFINE_OWNED_WINDOW_PROPERTY_KEY(web_modal::ModalDialogHost,
86+
kModalDialogHostKey,
87+
nullptr);
88+
89+
// static
90+
web_modal::ModalDialogHost* ModalDialogHostImpl::Get(
91+
aura::Window* window) {
92+
web_modal::ModalDialogHost* host = window->GetProperty(kModalDialogHostKey);
93+
if (!host) {
94+
host = new ModalDialogHostImpl(window);
95+
window->SetProperty(kModalDialogHostKey, host);
96+
}
97+
return host;
98+
}
99+
100+
class NWConstrainedWindowViewsClient
101+
: public constrained_window::ConstrainedWindowViewsClient {
102+
public:
103+
NWConstrainedWindowViewsClient() {}
104+
~NWConstrainedWindowViewsClient() override {}
105+
106+
private:
107+
// ConstrainedWindowViewsClient:
108+
content::WebContents* GetEmbedderWebContents(
109+
content::WebContents* initiator_web_contents) override {
110+
extensions::GuestViewBase* guest_view =
111+
extensions::GuestViewBase::FromWebContents(initiator_web_contents);
112+
return guest_view && guest_view->embedder_web_contents() ?
113+
guest_view->embedder_web_contents() : initiator_web_contents;
114+
}
115+
web_modal::ModalDialogHost* GetModalDialogHost(
116+
gfx::NativeWindow parent) override {
117+
return ModalDialogHostImpl::Get(parent);
118+
}
119+
gfx::NativeView GetDialogHostView(gfx::NativeWindow parent) override {
120+
return parent;
121+
}
122+
123+
DISALLOW_COPY_AND_ASSIGN(NWConstrainedWindowViewsClient);
124+
};
125+
126+
} // namespace
127+
128+
void InstallConstrainedWindowViewsClient() {
129+
constrained_window::SetConstrainedWindowViewsClient(
130+
make_scoped_ptr(new NWConstrainedWindowViewsClient));
131+
}
132+
133+
void UninstallConstrainedWindowViewsClient() {
134+
constrained_window::SetConstrainedWindowViewsClient(nullptr);
135+
}
136+
137+
} // namespace athena
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2014 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef NW_CONSTRAINED_WINDOW_VIEWS_CLIENT_H_
6+
#define NW_CONSTRAINED_WINDOW_VIEWS_CLIENT_H_
7+
8+
namespace nw {
9+
10+
void InstallConstrainedWindowViewsClient();
11+
void UninstallConstrainedWindowViewsClient();
12+
13+
}
14+
15+
#endif

src/nw_shell.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
#include "components/autofill/core/browser/autofill_manager.h"
7171
#include "components/web_modal/web_contents_modal_dialog_manager.h"
7272

73+
#include "components/app_modal/javascript_dialog_manager.h"
7374

7475
#if defined(OS_WIN) || defined(OS_LINUX)
7576
#include "content/nw/src/browser/native_window_aura.h"
@@ -652,9 +653,13 @@ void Shell::DidNavigateMainFramePostCommit(WebContents* web_contents) {
652653
}
653654

654655
JavaScriptDialogManager* Shell::GetJavaScriptDialogManager() {
656+
#if defined(OS_LINUX)
657+
return app_modal::JavaScriptDialogManager::GetInstance();
658+
#else
655659
if (!dialog_creator_.get())
656660
dialog_creator_.reset(new ShellJavaScriptDialogCreator());
657661
return dialog_creator_.get();
662+
#endif
658663
}
659664

660665
bool Shell::AddMessageToConsole(WebContents* source,

src/shell_browser_main_parts.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "content/nw/src/nw_package.h"
3838
#include "content/nw/src/nw_shell.h"
3939
#include "content/nw/src/shell_browser_context.h"
40+
#include "content/nw/src/browser/nw_constrained_window_views_client.h"
4041
#include "content/public/common/content_switches.h"
4142
#include "content/public/common/main_function_params.h"
4243
#include "grit/net_resources.h"
@@ -68,6 +69,8 @@
6869
#include "ui/views/linux_ui/linux_ui.h"
6970
#endif
7071

72+
#include "chrome/browser/ui/app_modal/chrome_javascript_native_dialog_factory.h"
73+
7174
using base::MessageLoop;
7275

7376
namespace {
@@ -241,6 +244,9 @@ void ShellBrowserMainParts::Init() {
241244
NULL,
242245
MSG_ROUTING_NONE,
243246
NULL);
247+
248+
InstallChromeJavaScriptNativeDialogFactory();
249+
nw::InstallConstrainedWindowViewsClient();
244250
}
245251

246252
bool ShellBrowserMainParts::ProcessSingletonNotificationCallback(

0 commit comments

Comments
 (0)