Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2175)

Unified Diff: chrome/browser/permissions/permission_manager.cc

Issue 1316863010: browser: implement multiple permission requesting (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@request-multiple-content
Patch Set: Rebase on master Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/browser/permissions/permission_manager.h ('k') | chrome/browser/permissions/permission_request_id.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/permissions/permission_manager.cc
diff --git a/chrome/browser/permissions/permission_manager.cc b/chrome/browser/permissions/permission_manager.cc
index 1f18104f03f3b1f6895c52da134ccf382aac173c..9608213a48bdaa1327198f07985304127db17667 100644
--- a/chrome/browser/permissions/permission_manager.cc
+++ b/chrome/browser/permissions/permission_manager.cc
@@ -93,6 +93,13 @@ bool IsConstantPermission(PermissionType type) {
}
}
+void PermissionRequestResponseCallbackWrapper(
+ const base::Callback<void(PermissionStatus)>& callback,
+ const std::vector<PermissionStatus>& vector) {
+ DCHECK_EQ(1ul, vector.size());
+ callback.Run(vector[0]);
+}
+
// Function used for handling permission types which do not change their
// value i.e. they are always approved or always denied etc.
// CONTENT_SETTING_DEFAULT is returned if the permission needs further handling.
@@ -116,16 +123,37 @@ PermissionStatus GetPermissionStatusForConstantPermission(PermissionType type) {
} // anonymous namespace
struct PermissionManager::PendingRequest {
- PendingRequest(PermissionType permission,
- content::RenderFrameHost* render_frame_host)
- : permission(permission),
+ PendingRequest(content::RenderFrameHost* render_frame_host,
+ const GURL& requesting_origin,
+ const std::vector<PermissionType> permissions,
+ const base::Callback<void(
+ const std::vector<PermissionStatus>&)>& callback)
+ : pending_responses(permissions.size()),
render_process_id(render_frame_host->GetProcess()->GetID()),
- render_frame_id(render_frame_host->GetRoutingID()) {
+ render_frame_id(render_frame_host->GetRoutingID()),
+ requesting_origin(requesting_origin),
+ callback(callback),
+ permissions(permissions),
+ result(permissions.size(), content::PERMISSION_STATUS_DENIED) {
}
- PermissionType permission;
+ void SetPermission(int type_index, PermissionStatus status) {
+ result[type_index] = status;
+ pending_responses--;
+ }
+
+ bool IsComplete() {
+ return pending_responses == 0;
+ }
+
+ size_t pending_responses;
int render_process_id;
int render_frame_id;
+ const GURL requesting_origin;
+ const base::Callback<void(
+ const std::vector<PermissionStatus>&)> callback;
+ std::vector<PermissionType> permissions;
+ std::vector<PermissionStatus> result;
};
struct PermissionManager::Subscription {
@@ -153,75 +181,103 @@ int PermissionManager::RequestPermission(
const GURL& requesting_origin,
bool user_gesture,
const base::Callback<void(PermissionStatus)>& callback) {
- if (IsConstantPermission(permission)) {
- callback.Run(GetPermissionStatusForConstantPermission(permission));
- return kNoPendingOperation;
- }
+ return RequestPermissions(
+ std::vector<PermissionType>(1, permission),
+ render_frame_host,
+ requesting_origin,
+ user_gesture,
+ base::Bind(&PermissionRequestResponseCallbackWrapper, callback));
+}
- PermissionContextBase* context = PermissionContext::Get(profile_, permission);
- if (!context) {
- callback.Run(content::PERMISSION_STATUS_DENIED);
+int PermissionManager::RequestPermissions(
+ const std::vector<PermissionType>& permissions,
+ content::RenderFrameHost* render_frame_host,
+ const GURL& requesting_origin,
+ bool user_gesture,
+ const base::Callback<void(
+ const std::vector<PermissionStatus>&)>& callback) {
+ if (permissions.empty()) {
+ callback.Run(std::vector<PermissionStatus>());
return kNoPendingOperation;
}
content::WebContents* web_contents =
content::WebContents::FromRenderFrameHost(render_frame_host);
- if (IsPermissionBubbleManagerMissing(web_contents)) {
- callback.Run(
- GetPermissionStatus(permission, requesting_origin,
- web_contents->GetLastCommittedURL().GetOrigin()));
- return kNoPendingOperation;
- }
+ GURL embedding_origin = web_contents->GetLastCommittedURL().GetOrigin();
PendingRequest* pending_request = new PendingRequest(
- permission, render_frame_host);
+ render_frame_host, requesting_origin, permissions, callback);
int request_id = pending_requests_.Add(pending_request);
- const PermissionRequestID request(pending_request->render_process_id,
- pending_request->render_frame_id,
- request_id);
- context->RequestPermission(
- web_contents, request, requesting_origin, user_gesture,
- base::Bind(&PermissionManager::OnPermissionRequestResponse,
- weak_ptr_factory_.GetWeakPtr(),
- request_id,
- callback));
+ const PermissionRequestID request(render_frame_host, request_id);
+
+ for (size_t i = 0; i < permissions.size(); ++i) {
+ const PermissionType permission = permissions[i];
+
+ if (IsConstantPermission(permission) ||
+ IsPermissionBubbleManagerMissing(web_contents) ||
+ !PermissionContext::Get(profile_, permission)) {
+ OnPermissionsRequestResponseStatus(request_id, i,
+ GetPermissionStatus(
+ permission, requesting_origin, embedding_origin));
+ continue;
+ }
+
+ PermissionContextBase* context = PermissionContext::Get(
+ profile_, permission);
+ context->RequestPermission(
+ web_contents, request, requesting_origin, user_gesture,
+ base::Bind(&PermissionManager::OnPermissionsRequestResponse,
+ weak_ptr_factory_.GetWeakPtr(),
+ request_id, i));
+ }
return request_id;
}
-void PermissionManager::OnPermissionRequestResponse(
+void PermissionManager::OnPermissionsRequestResponse(
int request_id,
- const base::Callback<void(PermissionStatus)>& callback,
+ int type_index,
ContentSetting content_setting) {
- pending_requests_.Remove(request_id);
- callback.Run(ContentSettingToPermissionStatus(content_setting));
+ OnPermissionsRequestResponseStatus(request_id, type_index,
+ ContentSettingToPermissionStatus(content_setting));
}
-void PermissionManager::CancelPermissionRequest(
- PermissionType permission,
- content::RenderFrameHost* render_frame_host,
+void PermissionManager::OnPermissionsRequestResponseStatus(
int request_id,
- const GURL& requesting_origin) {
+ int type_index,
+ PermissionStatus status) {
PendingRequest* pending_request = pending_requests_.Lookup(request_id);
- if (!pending_request)
- return;
+ pending_request->SetPermission(type_index, status);
- PermissionContextBase* context = PermissionContext::Get(profile_, permission);
- if (!context)
+ if (pending_request->IsComplete()) {
+ pending_request->callback.Run(pending_request->result);
+ pending_requests_.Remove(request_id);
+ }
+}
+
+void PermissionManager::CancelPermissionRequest(int request_id) {
+ PendingRequest* pending_request = pending_requests_.Lookup(request_id);
+ if (!pending_request)
return;
- content::WebContents* web_contents =
- content::WebContents::FromRenderFrameHost(render_frame_host);
- if (IsPermissionBubbleManagerMissing(web_contents))
+ content::WebContents* web_contents = tab_util::GetWebContentsByFrameID(
+ pending_request->render_process_id, pending_request->render_frame_id);
+ DCHECK(web_contents);
+ if (IsPermissionBubbleManagerMissing(web_contents)) {
+ pending_requests_.Remove(request_id);
return;
+ }
- int render_process_id = render_frame_host->GetProcess()->GetID();
- int render_frame_id = render_frame_host->GetRoutingID();
- const PermissionRequestID request(render_process_id,
- render_frame_id,
+ const PermissionRequestID request(pending_request->render_process_id,
+ pending_request->render_frame_id,
request_id);
-
- context->CancelPermissionRequest(web_contents, request);
+ for (PermissionType permission : pending_request->permissions) {
+ PermissionContextBase* context = PermissionContext::Get(
+ profile_, permission);
+ if (!context)
+ continue;
+ context->CancelPermissionRequest(web_contents, request);
+ }
pending_requests_.Remove(request_id);
}
« no previous file with comments | « chrome/browser/permissions/permission_manager.h ('k') | chrome/browser/permissions/permission_request_id.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698