Skip to content

Commit 47d7a35

Browse files
committed
Make file dialogs work in <webview>, fixes electron#930
1 parent a1ae399 commit 47d7a35

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

atom/browser/api/atom_api_web_contents.cc

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
#include "atom/browser/api/atom_api_web_contents.h"
66

77
#include "atom/browser/atom_browser_context.h"
8+
#include "atom/browser/native_window.h"
9+
#include "atom/browser/web_dialog_helper.h"
10+
#include "atom/browser/web_view/web_view_renderer_state.h"
811
#include "atom/common/api/api_messages.h"
912
#include "atom/common/native_mate_converters/gfx_converter.h"
1013
#include "atom/common/native_mate_converters/gurl_converter.h"
@@ -34,6 +37,17 @@ namespace {
3437

3538
v8::Persistent<v8::ObjectTemplate> template_;
3639

40+
// Get the window that has the |guest| embedded.
41+
NativeWindow* GetWindowFromGuest(const content::WebContents* guest) {
42+
int guest_process_id = guest->GetRenderProcessHost()->GetID();
43+
WebViewRendererState::WebViewInfo info;
44+
if (!WebViewRendererState::GetInstance()->GetInfo(guest_process_id, &info))
45+
return nullptr;
46+
return NativeWindow::FromRenderView(
47+
info.embedder->GetRenderProcessHost()->GetID(),
48+
info.embedder->GetRoutingID());
49+
}
50+
3751
} // namespace
3852

3953
WebContents::WebContents(content::WebContents* web_contents)
@@ -135,6 +149,21 @@ content::WebContents* WebContents::OpenURLFromTab(
135149
return web_contents();
136150
}
137151

152+
void WebContents::RunFileChooser(content::WebContents* guest,
153+
const content::FileChooserParams& params) {
154+
if (!web_dialog_helper_)
155+
web_dialog_helper_.reset(new WebDialogHelper(GetWindowFromGuest(guest)));
156+
web_dialog_helper_->RunFileChooser(guest, params);
157+
}
158+
159+
void WebContents::EnumerateDirectory(content::WebContents* guest,
160+
int request_id,
161+
const base::FilePath& path) {
162+
if (!web_dialog_helper_)
163+
web_dialog_helper_.reset(new WebDialogHelper(GetWindowFromGuest(guest)));
164+
web_dialog_helper_->EnumerateDirectory(guest, request_id, path);
165+
}
166+
138167
void WebContents::RequestMediaAccessPermission(
139168
content::WebContents*,
140169
const content::MediaStreamRequest& request,

atom/browser/api/atom_api_web_contents.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ class Dictionary;
2424

2525
namespace atom {
2626

27+
class WebDialogHelper;
28+
2729
namespace api {
2830

2931
class WebContents : public mate::EventEmitter,
@@ -113,6 +115,11 @@ class WebContents : public mate::EventEmitter,
113115
content::WebContents* OpenURLFromTab(
114116
content::WebContents* source,
115117
const content::OpenURLParams& params) override;
118+
void RunFileChooser(content::WebContents* web_contents,
119+
const content::FileChooserParams& params) override;
120+
void EnumerateDirectory(content::WebContents* web_contents,
121+
int request_id,
122+
const base::FilePath& path) override;
116123
void RequestMediaAccessPermission(
117124
content::WebContents*,
118125
const content::MediaStreamRequest&,
@@ -165,6 +172,8 @@ class WebContents : public mate::EventEmitter,
165172
void GuestSizeChangedDueToAutoSize(const gfx::Size& old_size,
166173
const gfx::Size& new_size);
167174

175+
scoped_ptr<WebDialogHelper> web_dialog_helper_;
176+
168177
// Unique ID for a guest WebContents.
169178
int guest_instance_id_;
170179

atom/browser/web_view/web_view_manager.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ void WebViewManager::AddGuest(int guest_instance_id,
6565

6666
WebViewRendererState::WebViewInfo web_view_info = {
6767
guest_instance_id,
68+
embedder,
6869
options.node_integration,
6970
options.plugins,
7071
options.disable_web_security,

atom/browser/web_view/web_view_renderer_state.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
#include "base/files/file_path.h"
1313
#include "base/memory/singleton.h"
1414

15+
namespace content {
16+
class WebContents;
17+
}
18+
1519
namespace atom {
1620

1721
class WebViewManager;
@@ -22,6 +26,7 @@ class WebViewRendererState {
2226
public:
2327
struct WebViewInfo {
2428
int guest_instance_id;
29+
content::WebContents* embedder;
2530
bool node_integration;
2631
bool plugins;
2732
bool disable_web_security;

0 commit comments

Comments
 (0)