Skip to content

Commit e31985d

Browse files
committed
Fix linux javascript dialog
Fix nwjs#2683
1 parent 961e750 commit e31985d

7 files changed

+184
-0
lines changed

nw.gypi

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
'<(DEPTH)/base/base.gyp:base_prefs_test_support',
5353
'<(DEPTH)/base/third_party/dynamic_annotations/dynamic_annotations.gyp:dynamic_annotations',
5454
'<(DEPTH)/chrome/common/extensions/api/api.gyp:chrome_api',
55+
'<(DEPTH)/components/components.gyp:app_modal',
5556
'<(DEPTH)/components/components.gyp:autofill_content_renderer',
5657
'<(DEPTH)/components/components.gyp:keyed_service_content',
5758
'<(DEPTH)/components/components_resources.gyp:components_resources',
@@ -90,6 +91,7 @@
9091
'<(DEPTH)/extensions/extensions.gyp:extensions_shell_and_test_pak',
9192
'<(DEPTH)/extensions/extensions.gyp:extensions_utility',
9293
'<(DEPTH)/extensions/extensions_resources.gyp:extensions_resources',
94+
'<(DEPTH)/components/components_strings.gyp:components_strings',
9395
'nw_resources',
9496
'commit_id',
9597
],
@@ -133,6 +135,11 @@
133135
'<(DEPTH)/chrome/browser/ui/cocoa/custom_frame_view.h',
134136
'<(DEPTH)/chrome/browser/ui/cocoa/custom_frame_view.mm',
135137
'<(DEPTH)/chrome/browser/ui/base_window.h',
138+
'<(DEPTH)/chrome/browser/ui/views/chrome_javascript_native_dialog_factory_views.cc',
139+
'<(DEPTH)/chrome/browser/ui/views/javascript_app_modal_dialog_views_x11.cc',
140+
'<(DEPTH)/chrome/browser/ui/views/javascript_app_modal_dialog_views_x11.h',
141+
'<(DEPTH)/chrome/browser/ui/views/javascript_app_modal_event_blocker_x11.cc',
142+
'<(DEPTH)/chrome/browser/ui/views/javascript_app_modal_event_blocker_x11.h',
136143
'<(DEPTH)/chrome/browser/ui/views/status_icons/status_icon_linux_wrapper.cc',
137144
'<(DEPTH)/chrome/browser/ui/views/status_icons/status_icon_linux_wrapper.h',
138145
'<(DEPTH)/chrome/browser/ui/views/status_icons/status_icon_win.cc',
@@ -307,6 +314,8 @@
307314
'src/browser/shell_runtime_api_delegate.h',
308315
'src/browser/media_capture_util.cc',
309316
'src/browser/media_capture_util.h',
317+
'src/browser/nw_constrained_window_views_client.cc',
318+
'src/browser/nw_constrained_window_views_client.h',
310319
'src/browser/shell_javascript_dialog_creator.cc',
311320
'src/browser/shell_javascript_dialog_creator.h',
312321
# 'src/browser/shell_javascript_dialog_gtk.cc',
@@ -700,6 +709,7 @@
700709
'<(SHARED_INTERMEDIATE_DIR)/ui/resources/ui_resources_100_percent.pak',
701710
'<(SHARED_INTERMEDIATE_DIR)/ui/resources/webui_resources.pak',
702711
'<(SHARED_INTERMEDIATE_DIR)/ui/strings/app_locale_settings_en-US.pak',
712+
'<(SHARED_INTERMEDIATE_DIR)/components/strings/components_strings_en-US.pak',
703713
'<(SHARED_INTERMEDIATE_DIR)/ui/strings/ui_strings_en-US.pak',
704714
'<(SHARED_INTERMEDIATE_DIR)/webkit/devtools_resources.pak',
705715
'<(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
@@ -69,6 +69,7 @@
6969
#include "components/autofill/core/browser/autofill_manager.h"
7070
#include "components/web_modal/web_contents_modal_dialog_manager.h"
7171

72+
#include "components/app_modal/javascript_dialog_manager.h"
7273

7374
#if defined(OS_WIN) || defined(OS_LINUX)
7475
#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(WebContents* source) {
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/nw/src/browser/shell_devtools_manager_delegate.h"
4142
#include "content/public/browser/devtools_http_handler.h"
4243
#include "content/public/common/content_switches.h"
@@ -78,6 +79,8 @@
7879
#include "extensions/browser/browser_context_keyed_service_factories.h"
7980
#include "extensions/browser/process_manager.h"
8081

82+
#include "chrome/browser/ui/app_modal/chrome_javascript_native_dialog_factory.h"
83+
8184
using base::MessageLoop;
8285

8386
namespace {
@@ -276,6 +279,9 @@ void ShellBrowserMainParts::Init() {
276279
NULL,
277280
MSG_ROUTING_NONE,
278281
NULL);
282+
283+
InstallChromeJavaScriptNativeDialogFactory();
284+
nw::InstallConstrainedWindowViewsClient();
279285
}
280286

281287
bool ShellBrowserMainParts::ProcessSingletonNotificationCallback(

0 commit comments

Comments
 (0)