2016-01-30 11:19:18 +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/atom_permission_manager.h"
|
|
|
|
|
2016-01-30 13:31:10 +00:00
|
|
|
#include "content/public/browser/child_process_security_policy.h"
|
2016-01-30 11:19:18 +00:00
|
|
|
#include "content/public/browser/permission_type.h"
|
|
|
|
#include "content/public/browser/render_frame_host.h"
|
|
|
|
#include "content/public/browser/render_process_host.h"
|
2016-02-01 09:43:49 +00:00
|
|
|
#include "content/public/browser/render_view_host.h"
|
2016-01-31 21:35:34 +00:00
|
|
|
#include "content/public/browser/web_contents.h"
|
2016-01-30 11:19:18 +00:00
|
|
|
|
|
|
|
namespace atom {
|
|
|
|
|
2016-02-01 09:43:49 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Must be kept in sync with atom_browser_client.cc
|
|
|
|
int kDefaultRoutingID = 2;
|
|
|
|
|
|
|
|
bool WebContentsDestroyed(int process_id) {
|
|
|
|
auto rvh = content::RenderViewHost::FromID(process_id, kDefaultRoutingID);
|
|
|
|
if (rvh) {
|
|
|
|
auto contents = content::WebContents::FromRenderViewHost(rvh);
|
|
|
|
return contents->IsBeingDestroyed();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2016-01-30 11:19:18 +00:00
|
|
|
AtomPermissionManager::AtomPermissionManager()
|
|
|
|
: request_id_(0) {
|
|
|
|
}
|
|
|
|
|
|
|
|
AtomPermissionManager::~AtomPermissionManager() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void AtomPermissionManager::SetPermissionRequestHandler(
|
|
|
|
const RequestHandler& handler) {
|
2016-01-31 21:35:34 +00:00
|
|
|
if (handler.is_null() && !pending_requests_.empty()) {
|
2016-02-01 09:43:49 +00:00
|
|
|
for (const auto& request : pending_requests_) {
|
|
|
|
if (!WebContentsDestroyed(request.second.render_process_id))
|
|
|
|
request.second.callback.Run(content::PERMISSION_STATUS_DENIED);
|
|
|
|
}
|
2016-01-31 21:35:34 +00:00
|
|
|
pending_requests_.clear();
|
2016-01-30 11:19:18 +00:00
|
|
|
}
|
2016-01-31 21:35:34 +00:00
|
|
|
request_handler_ = handler;
|
2016-01-30 11:19:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int AtomPermissionManager::RequestPermission(
|
|
|
|
content::PermissionType permission,
|
|
|
|
content::RenderFrameHost* render_frame_host,
|
|
|
|
const GURL& requesting_origin,
|
|
|
|
bool user_gesture,
|
2016-01-31 21:35:34 +00:00
|
|
|
const ResponseCallback& response_callback) {
|
2016-02-01 09:43:49 +00:00
|
|
|
int process_id = render_frame_host->GetProcess()->GetID();
|
|
|
|
|
2016-01-30 13:31:10 +00:00
|
|
|
if (permission == content::PermissionType::MIDI_SYSEX) {
|
|
|
|
content::ChildProcessSecurityPolicy::GetInstance()->
|
2016-02-01 09:43:49 +00:00
|
|
|
GrantSendMidiSysExMessage(process_id);
|
2016-01-30 13:31:10 +00:00
|
|
|
}
|
|
|
|
|
2016-01-31 21:35:34 +00:00
|
|
|
if (!request_handler_.is_null()) {
|
|
|
|
auto web_contents =
|
|
|
|
content::WebContents::FromRenderFrameHost(render_frame_host);
|
|
|
|
++request_id_;
|
|
|
|
auto callback = base::Bind(&AtomPermissionManager::OnPermissionResponse,
|
|
|
|
base::Unretained(this),
|
|
|
|
request_id_,
|
|
|
|
requesting_origin,
|
|
|
|
response_callback);
|
2016-02-01 09:43:49 +00:00
|
|
|
pending_requests_[request_id_] = { process_id, callback };
|
2016-01-31 21:35:34 +00:00
|
|
|
request_handler_.Run(web_contents, permission, callback);
|
2016-01-30 11:19:18 +00:00
|
|
|
return request_id_;
|
|
|
|
}
|
|
|
|
|
2016-01-31 21:35:34 +00:00
|
|
|
response_callback.Run(content::PERMISSION_STATUS_GRANTED);
|
2016-01-30 11:19:18 +00:00
|
|
|
return kNoPendingOperation;
|
|
|
|
}
|
|
|
|
|
2016-01-31 21:35:34 +00:00
|
|
|
void AtomPermissionManager::OnPermissionResponse(
|
|
|
|
int request_id,
|
|
|
|
const GURL& origin,
|
|
|
|
const ResponseCallback& callback,
|
|
|
|
content::PermissionStatus status) {
|
2016-02-01 09:43:49 +00:00
|
|
|
auto request = pending_requests_.find(request_id);
|
|
|
|
if (request != pending_requests_.end()) {
|
|
|
|
if (!WebContentsDestroyed(request->second.render_process_id))
|
|
|
|
callback.Run(status);
|
|
|
|
pending_requests_.erase(request);
|
|
|
|
}
|
2016-01-31 21:35:34 +00:00
|
|
|
}
|
|
|
|
|
2016-01-30 11:19:18 +00:00
|
|
|
void AtomPermissionManager::CancelPermissionRequest(int request_id) {
|
|
|
|
auto request = pending_requests_.find(request_id);
|
|
|
|
if (request != pending_requests_.end()) {
|
2016-02-01 09:43:49 +00:00
|
|
|
if (!WebContentsDestroyed(request->second.render_process_id))
|
|
|
|
request->second.callback.Run(content::PERMISSION_STATUS_DENIED);
|
2016-01-30 11:19:18 +00:00
|
|
|
pending_requests_.erase(request);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AtomPermissionManager::ResetPermission(
|
|
|
|
content::PermissionType permission,
|
|
|
|
const GURL& requesting_origin,
|
|
|
|
const GURL& embedding_origin) {
|
|
|
|
}
|
|
|
|
|
|
|
|
content::PermissionStatus AtomPermissionManager::GetPermissionStatus(
|
|
|
|
content::PermissionType permission,
|
|
|
|
const GURL& requesting_origin,
|
|
|
|
const GURL& embedding_origin) {
|
2016-01-30 13:31:10 +00:00
|
|
|
return content::PERMISSION_STATUS_GRANTED;
|
2016-01-30 11:19:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AtomPermissionManager::RegisterPermissionUsage(
|
|
|
|
content::PermissionType permission,
|
|
|
|
const GURL& requesting_origin,
|
|
|
|
const GURL& embedding_origin) {
|
|
|
|
}
|
|
|
|
|
|
|
|
int AtomPermissionManager::SubscribePermissionStatusChange(
|
|
|
|
content::PermissionType permission,
|
|
|
|
const GURL& requesting_origin,
|
|
|
|
const GURL& embedding_origin,
|
|
|
|
const ResponseCallback& callback) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AtomPermissionManager::UnsubscribePermissionStatusChange(
|
|
|
|
int subscription_id) {
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace atom
|