2016-01-23 13:29:47 +00:00
|
|
|
// 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>
|
|
|
|
|
2016-01-30 11:19:18 +00:00
|
|
|
#include "atom/browser/atom_permission_manager.h"
|
2016-01-31 19:13:29 +00:00
|
|
|
#include "content/public/browser/browser_context.h"
|
2016-01-23 13:29:47 +00:00
|
|
|
#include "content/public/browser/media_capture_devices.h"
|
2016-01-30 11:19:18 +00:00
|
|
|
#include "content/public/browser/render_process_host.h"
|
2016-01-23 13:29:47 +00:00
|
|
|
|
|
|
|
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,
|
2016-01-25 16:37:15 +00:00
|
|
|
const content::MediaResponseCallback& callback,
|
|
|
|
bool allowed) {
|
|
|
|
if (!allowed) {
|
|
|
|
callback.Run(content::MediaStreamDevices(),
|
|
|
|
content::MEDIA_DEVICE_PERMISSION_DENIED,
|
|
|
|
scoped_ptr<content::MediaStreamUI>());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-23 13:29:47 +00:00
|
|
|
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>());
|
|
|
|
}
|
|
|
|
|
2016-02-01 09:43:49 +00:00
|
|
|
void OnPointerLockResponse(content::WebContents* web_contents, bool allowed) {
|
|
|
|
if (web_contents)
|
|
|
|
web_contents->GotResponseToLockMouseRequest(allowed);
|
|
|
|
}
|
|
|
|
|
2016-01-31 21:35:34 +00:00
|
|
|
void OnPermissionResponse(const base::Callback<void(bool)>& callback,
|
|
|
|
content::PermissionStatus status) {
|
|
|
|
if (status == content::PERMISSION_STATUS_GRANTED)
|
|
|
|
callback.Run(true);
|
|
|
|
else
|
|
|
|
callback.Run(false);
|
|
|
|
}
|
|
|
|
|
2016-01-23 13:29:47 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
WebContentsPermissionHelper::WebContentsPermissionHelper(
|
2016-01-30 11:19:18 +00:00
|
|
|
content::WebContents* web_contents)
|
|
|
|
: web_contents_(web_contents) {
|
2016-01-23 13:29:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
WebContentsPermissionHelper::~WebContentsPermissionHelper() {
|
|
|
|
}
|
|
|
|
|
2016-01-30 11:19:18 +00:00
|
|
|
void WebContentsPermissionHelper::RequestPermission(
|
|
|
|
content::PermissionType permission,
|
2016-02-01 09:43:49 +00:00
|
|
|
const base::Callback<void(bool)>& callback,
|
|
|
|
bool user_gesture) {
|
2016-01-30 11:19:18 +00:00
|
|
|
auto rfh = web_contents_->GetMainFrame();
|
2016-01-31 19:13:29 +00:00
|
|
|
auto permission_manager = static_cast<AtomPermissionManager*>(
|
|
|
|
web_contents_->GetBrowserContext()->GetPermissionManager());
|
2016-01-30 11:19:18 +00:00
|
|
|
auto origin = web_contents_->GetLastCommittedURL();
|
2016-01-31 21:35:34 +00:00
|
|
|
permission_manager->RequestPermission(
|
|
|
|
permission, rfh, origin, user_gesture,
|
|
|
|
base::Bind(&OnPermissionResponse, callback));
|
2016-01-30 11:19:18 +00:00
|
|
|
}
|
|
|
|
|
2016-01-23 13:29:47 +00:00
|
|
|
void WebContentsPermissionHelper::RequestMediaAccessPermission(
|
|
|
|
const content::MediaStreamRequest& request,
|
2016-01-30 11:19:18 +00:00
|
|
|
const content::MediaResponseCallback& response_callback) {
|
|
|
|
auto callback = base::Bind(&MediaAccessAllowed, request, response_callback);
|
2016-01-31 21:35:34 +00:00
|
|
|
// The permission type doesn't matter here, AUDIO_CAPTURE/VIDEO_CAPTURE
|
|
|
|
// are presented as same type in content_converter.h.
|
2016-01-30 11:19:18 +00:00
|
|
|
RequestPermission(content::PermissionType::AUDIO_CAPTURE, callback);
|
2016-01-23 13:29:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebContentsPermissionHelper::RequestWebNotificationPermission(
|
2016-01-25 16:37:15 +00:00
|
|
|
const base::Callback<void(bool)>& callback) {
|
2016-01-30 11:19:18 +00:00
|
|
|
RequestPermission(content::PermissionType::NOTIFICATIONS, callback);
|
2016-01-23 13:29:47 +00:00
|
|
|
}
|
|
|
|
|
2016-02-01 09:43:49 +00:00
|
|
|
void WebContentsPermissionHelper::RequestPointerLockPermission(
|
|
|
|
bool user_gesture) {
|
|
|
|
RequestPermission((content::PermissionType)(PermissionType::POINTER_LOCK),
|
|
|
|
base::Bind(&OnPointerLockResponse, web_contents_),
|
|
|
|
user_gesture);
|
|
|
|
}
|
|
|
|
|
2016-01-23 13:29:47 +00:00
|
|
|
} // namespace atom
|