webview: add permission-request event
This commit is contained in:
parent
30b35644f6
commit
85e13333c3
12 changed files with 260 additions and 3 deletions
|
@ -14,6 +14,7 @@
|
|||
#include "atom/browser/atom_browser_context.h"
|
||||
#include "atom/browser/atom_browser_main_parts.h"
|
||||
#include "atom/browser/native_window.h"
|
||||
#include "atom/browser/web_contents_permission_helper.h"
|
||||
#include "atom/browser/web_contents_preferences.h"
|
||||
#include "atom/browser/web_view_guest_delegate.h"
|
||||
#include "atom/common/api/api_messages.h"
|
||||
|
@ -262,6 +263,9 @@ WebContents::WebContents(v8::Isolate* isolate,
|
|||
// Save the preferences in C++.
|
||||
new WebContentsPreferences(web_contents, options);
|
||||
|
||||
// Initialze permission helper.
|
||||
new WebContentsPermissionHelper(web_contents, this);
|
||||
|
||||
web_contents->SetUserAgentOverride(GetBrowserContext()->GetUserAgent());
|
||||
|
||||
if (is_guest) {
|
||||
|
@ -444,6 +448,15 @@ void WebContents::FindReply(content::WebContents* web_contents,
|
|||
}
|
||||
}
|
||||
|
||||
void WebContents::RequestMediaAccessPermission(
|
||||
content::WebContents* web_contents,
|
||||
const content::MediaStreamRequest& request,
|
||||
const content::MediaResponseCallback& callback) {
|
||||
auto permission_helper =
|
||||
WebContentsPermissionHelper::FromWebContents(web_contents);
|
||||
permission_helper->RequestMediaAccessPermission(request, callback);
|
||||
}
|
||||
|
||||
void WebContents::BeforeUnloadFired(const base::TimeTicks& proceed_time) {
|
||||
// Do nothing, we override this method just to avoid compilation error since
|
||||
// there are two virtual functions named BeforeUnloadFired.
|
||||
|
|
|
@ -195,6 +195,10 @@ class WebContents : public mate::TrackableObject<WebContents>,
|
|||
const gfx::Rect& selection_rect,
|
||||
int active_match_ordinal,
|
||||
bool final_update) override;
|
||||
void RequestMediaAccessPermission(
|
||||
content::WebContents* web_contents,
|
||||
const content::MediaStreamRequest& request,
|
||||
const content::MediaResponseCallback& callback) override;
|
||||
|
||||
// content::WebContentsObserver:
|
||||
void BeforeUnloadFired(const base::TimeTicks& proceed_time) override;
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
#include "atom/browser/atom_resource_dispatcher_host_delegate.h"
|
||||
#include "atom/browser/atom_speech_recognition_manager_delegate.h"
|
||||
#include "atom/browser/native_window.h"
|
||||
#include "atom/browser/web_contents_permission_helper.h"
|
||||
#include "atom/browser/web_contents_preferences.h"
|
||||
#include "atom/browser/window_list.h"
|
||||
#include "atom/common/options_switches.h"
|
||||
|
@ -281,6 +282,23 @@ brightray::BrowserMainParts* AtomBrowserClient::OverrideCreateBrowserMainParts(
|
|||
return new AtomBrowserMainParts;
|
||||
}
|
||||
|
||||
void AtomBrowserClient::WebNotificationAllowed(int render_process_id,
|
||||
const base::Closure& callback) {
|
||||
content::WebContents* web_contents = content::WebContents::FromRenderViewHost(
|
||||
content::RenderViewHost::FromID(render_process_id, kDefaultRoutingID));
|
||||
if (!web_contents) {
|
||||
callback.Run();
|
||||
return;
|
||||
}
|
||||
auto permission_helper =
|
||||
WebContentsPermissionHelper::FromWebContents(web_contents);
|
||||
if (!permission_helper) {
|
||||
callback.Run();
|
||||
return;
|
||||
}
|
||||
permission_helper->RequestWebNotificationPermission(callback);
|
||||
}
|
||||
|
||||
void AtomBrowserClient::RenderProcessHostDestroyed(
|
||||
content::RenderProcessHost* host) {
|
||||
int process_id = host->GetID();
|
||||
|
|
|
@ -81,6 +81,8 @@ class AtomBrowserClient : public brightray::BrowserClient,
|
|||
// brightray::BrowserClient:
|
||||
brightray::BrowserMainParts* OverrideCreateBrowserMainParts(
|
||||
const content::MainFunctionParams&) override;
|
||||
void WebNotificationAllowed(int render_process_id,
|
||||
const base::Closure& callback) override;
|
||||
|
||||
// content::RenderProcessHostObserver:
|
||||
void RenderProcessHostDestroyed(content::RenderProcessHost* host) override;
|
||||
|
|
|
@ -6,10 +6,12 @@ var slice = [].slice;
|
|||
// Doesn't exist in early initialization.
|
||||
var webViewManager = null;
|
||||
|
||||
var supportedWebViewEvents = ['load-commit', 'did-finish-load', 'did-fail-load', 'did-frame-finish-load', 'did-start-loading', 'did-stop-loading', 'did-get-response-details', 'did-get-redirect-request', 'dom-ready', 'console-message', 'devtools-opened', 'devtools-closed', 'devtools-focused', 'new-window', 'will-navigate', 'did-navigate', 'did-navigate-in-page', 'close', 'crashed', 'gpu-crashed', 'plugin-crashed', 'destroyed', 'page-title-updated', 'page-favicon-updated', 'enter-html-full-screen', 'leave-html-full-screen', 'media-started-playing', 'media-paused', 'found-in-page', 'did-change-theme-color'];
|
||||
var supportedWebViewEvents = ['load-commit', 'did-finish-load', 'did-fail-load', 'did-frame-finish-load', 'did-start-loading', 'did-stop-loading', 'did-get-response-details', 'did-get-redirect-request', 'dom-ready', 'console-message', 'devtools-opened', 'devtools-closed', 'devtools-focused', 'new-window', 'will-navigate', 'did-navigate', 'did-navigate-in-page', 'close', 'crashed', 'gpu-crashed', 'plugin-crashed', 'destroyed', 'page-title-updated', 'page-favicon-updated', 'enter-html-full-screen', 'leave-html-full-screen', 'media-started-playing', 'media-paused', 'found-in-page', 'did-change-theme-color', 'permission-request'];
|
||||
|
||||
var nextInstanceId = 0;
|
||||
var permissionRequests;
|
||||
var guestInstances = {};
|
||||
var guestPermissionRequestsMap = {};
|
||||
var embedderElementsMap = {};
|
||||
var reverseEmbedderElementsMap = {};
|
||||
|
||||
|
@ -110,6 +112,13 @@ var createGuest = function(embedder, params) {
|
|||
fn = function(event) {
|
||||
return guest.on(event, function() {
|
||||
var args = 2 <= arguments.length ? slice.call(arguments, 1) : [];
|
||||
if (event === 'permission-request') {
|
||||
if (!guestPermissionRequestsMap[guest.viewInstanceId])
|
||||
guestPermissionRequestsMap[guest.viewInstanceId] = {};
|
||||
var permission = args[0];
|
||||
guestPermissionRequestsMap[guest.viewInstanceId][permission] = args[1];
|
||||
args.pop();
|
||||
}
|
||||
return embedder.send.apply(embedder, ["ATOM_SHELL_GUEST_VIEW_INTERNAL_DISPATCH_EVENT-" + guest.viewInstanceId, event].concat(slice.call(args)));
|
||||
});
|
||||
};
|
||||
|
@ -157,7 +166,8 @@ var attachGuest = function(embedder, elementInstanceId, guestInstanceId, params)
|
|||
nodeIntegration: (ref1 = params.nodeintegration) != null ? ref1 : false,
|
||||
plugins: params.plugins,
|
||||
webSecurity: !params.disablewebsecurity,
|
||||
blinkFeatures: params.blinkfeatures
|
||||
blinkFeatures: params.blinkfeatures,
|
||||
webNotification: !params.disablewebnotification,
|
||||
};
|
||||
if (params.preload) {
|
||||
webPreferences.preloadURL = params.preload;
|
||||
|
@ -174,6 +184,7 @@ var destroyGuest = function(embedder, id) {
|
|||
webViewManager.removeGuest(embedder, id);
|
||||
guestInstances[id].guest.destroy();
|
||||
delete guestInstances[id];
|
||||
delete permissionRequests[id];
|
||||
key = reverseEmbedderElementsMap[id];
|
||||
if (key != null) {
|
||||
delete reverseEmbedderElementsMap[id];
|
||||
|
@ -193,6 +204,13 @@ ipcMain.on('ATOM_SHELL_GUEST_VIEW_MANAGER_DESTROY_GUEST', function(event, id) {
|
|||
return destroyGuest(event.sender, id);
|
||||
});
|
||||
|
||||
ipcMain.on('ATOM_SHELL_GUEST_VIEW_MANAGER_SET_PERMISSION_RESPONSE', function(event, viewInstanceId, permission, allowed) {
|
||||
permissionRequests = guestPermissionRequestsMap[viewInstanceId];
|
||||
if (permissionRequests && permissionRequests[permission] !== null) {
|
||||
permissionRequests[permission].apply(null, [allowed]);
|
||||
}
|
||||
});
|
||||
|
||||
ipcMain.on('ATOM_SHELL_GUEST_VIEW_MANAGER_SET_SIZE', function(event, id, params) {
|
||||
var ref1;
|
||||
return (ref1 = guestInstances[id]) != null ? ref1.guest.setSize(params) : void 0;
|
||||
|
|
114
atom/browser/web_contents_permission_helper.cc
Normal file
114
atom/browser/web_contents_permission_helper.cc
Normal file
|
@ -0,0 +1,114 @@
|
|||
// Copyright (c) 2016 GitHub, Inc.
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "atom/browser/web_contents_permission_helper.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "atom/browser/api/atom_api_web_contents.h"
|
||||
#include "content/public/browser/media_capture_devices.h"
|
||||
|
||||
DEFINE_WEB_CONTENTS_USER_DATA_KEY(atom::WebContentsPermissionHelper);
|
||||
|
||||
namespace atom {
|
||||
|
||||
namespace {
|
||||
|
||||
const content::MediaStreamDevice* FindDeviceWithId(
|
||||
const content::MediaStreamDevices& devices,
|
||||
const std::string& device_id) {
|
||||
if (device_id.empty())
|
||||
return &(*devices.begin());
|
||||
for (const auto& iter : devices)
|
||||
if (iter.id == device_id)
|
||||
return &(iter);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void MediaAccessAllowed(
|
||||
const content::MediaStreamRequest& request,
|
||||
const content::MediaResponseCallback& callback) {
|
||||
content::MediaStreamDevices devices;
|
||||
content::MediaStreamRequestResult result = content::MEDIA_DEVICE_NO_HARDWARE;
|
||||
|
||||
if (request.audio_type == content::MEDIA_DEVICE_AUDIO_CAPTURE) {
|
||||
const content::MediaStreamDevices& audio_devices =
|
||||
content::MediaCaptureDevices::GetInstance()->GetAudioCaptureDevices();
|
||||
const content::MediaStreamDevice* audio_device =
|
||||
FindDeviceWithId(audio_devices, request.requested_audio_device_id);
|
||||
if (audio_device)
|
||||
devices.push_back(*audio_device);
|
||||
}
|
||||
|
||||
if (request.video_type == content::MEDIA_DEVICE_VIDEO_CAPTURE) {
|
||||
const content::MediaStreamDevices& video_devices =
|
||||
content::MediaCaptureDevices::GetInstance()->GetVideoCaptureDevices();
|
||||
const content::MediaStreamDevice* video_device =
|
||||
FindDeviceWithId(video_devices, request.requested_video_device_id);
|
||||
if (video_device)
|
||||
devices.push_back(*video_device);
|
||||
}
|
||||
|
||||
if (!devices.empty())
|
||||
result = content::MEDIA_DEVICE_OK;
|
||||
|
||||
callback.Run(devices, result, scoped_ptr<content::MediaStreamUI>());
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
WebContentsPermissionHelper::WebContentsPermissionHelper(
|
||||
content::WebContents* web_contents,
|
||||
api::WebContents* api_web_contents)
|
||||
: api_web_contents_(api_web_contents) {
|
||||
web_contents->SetUserData(UserDataKey(), this);
|
||||
}
|
||||
|
||||
WebContentsPermissionHelper::~WebContentsPermissionHelper() {
|
||||
}
|
||||
|
||||
void WebContentsPermissionHelper::RequestMediaAccessPermission(
|
||||
const content::MediaStreamRequest& request,
|
||||
const content::MediaResponseCallback& callback) {
|
||||
if (api_web_contents_->IsGuest()) {
|
||||
const std::string& permission = "media";
|
||||
permission_map_[permission] = base::Bind(&MediaAccessAllowed,
|
||||
request,
|
||||
callback);
|
||||
api_web_contents_->Emit(
|
||||
"permission-request",
|
||||
permission,
|
||||
base::Bind(&WebContentsPermissionHelper::OnPermissionResponse,
|
||||
base::Unretained(this), permission));
|
||||
return;
|
||||
}
|
||||
MediaAccessAllowed(request, callback);
|
||||
}
|
||||
|
||||
void WebContentsPermissionHelper::RequestWebNotificationPermission(
|
||||
const base::Closure& callback) {
|
||||
if (api_web_contents_->IsGuest()) {
|
||||
const std::string& permission = "webNotification";
|
||||
permission_map_[permission] = callback;
|
||||
api_web_contents_->Emit(
|
||||
"permission-request",
|
||||
permission,
|
||||
base::Bind(&WebContentsPermissionHelper::OnPermissionResponse,
|
||||
base::Unretained(this), permission));
|
||||
return;
|
||||
}
|
||||
callback.Run();
|
||||
}
|
||||
|
||||
void WebContentsPermissionHelper::OnPermissionResponse(
|
||||
const std::string& permission, bool allowed) {
|
||||
auto it = permission_map_.find(permission);
|
||||
if (it != permission_map_.end()) {
|
||||
if (allowed)
|
||||
it->second.Run();
|
||||
permission_map_.erase(permission);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace atom
|
48
atom/browser/web_contents_permission_helper.h
Normal file
48
atom/browser/web_contents_permission_helper.h
Normal file
|
@ -0,0 +1,48 @@
|
|||
// Copyright (c) 2016 GitHub, Inc.
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef ATOM_BROWSER_WEB_CONTENTS_PERMISSION_HELPER_H_
|
||||
#define ATOM_BROWSER_WEB_CONTENTS_PERMISSION_HELPER_H_
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
#include "base/callback.h"
|
||||
#include "content/public/browser/web_contents_user_data.h"
|
||||
#include "content/public/common/media_stream_request.h"
|
||||
|
||||
namespace atom {
|
||||
|
||||
namespace api {
|
||||
class WebContents;
|
||||
}
|
||||
|
||||
// Applies the permission requested for WebContents.
|
||||
class WebContentsPermissionHelper
|
||||
: public content::WebContentsUserData<WebContentsPermissionHelper> {
|
||||
public:
|
||||
WebContentsPermissionHelper(content::WebContents* web_contents,
|
||||
api::WebContents* api_web_contents);
|
||||
~WebContentsPermissionHelper() override;
|
||||
|
||||
void RequestMediaAccessPermission(
|
||||
const content::MediaStreamRequest& request,
|
||||
const content::MediaResponseCallback& callback);
|
||||
void RequestWebNotificationPermission(const base::Closure& callback);
|
||||
|
||||
void OnPermissionResponse(const std::string& permission, bool allowed);
|
||||
|
||||
private:
|
||||
friend class content::WebContentsUserData<WebContentsPermissionHelper>;
|
||||
|
||||
std::map<std::string, base::Closure> permission_map_;
|
||||
|
||||
api::WebContents* api_web_contents_; // Weak reference
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(WebContentsPermissionHelper);
|
||||
};
|
||||
|
||||
} // namespace atom
|
||||
|
||||
#endif // ATOM_BROWSER_WEB_CONTENTS_PERMISSION_HELPER_H_
|
Loading…
Add table
Add a link
Reference in a new issue