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

Side by Side Diff: chrome/browser/permissions/permission_manager.cc

Issue 1342833002: permissions: handle request ids for permissions in permission manager (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address review comments 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 unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/permissions/permission_manager.h" 5 #include "chrome/browser/permissions/permission_manager.h"
6 6
7 #include "base/callback.h" 7 #include "base/callback.h"
8 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" 8 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
9 #include "chrome/browser/permissions/permission_context.h" 9 #include "chrome/browser/permissions/permission_context.h"
10 #include "chrome/browser/permissions/permission_context_base.h" 10 #include "chrome/browser/permissions/permission_context_base.h"
11 #include "chrome/browser/permissions/permission_request_id.h" 11 #include "chrome/browser/permissions/permission_request_id.h"
12 #include "chrome/browser/profiles/profile.h" 12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/tab_contents/tab_util.h"
13 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h" 14 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h"
14 #include "components/content_settings/core/browser/host_content_settings_map.h" 15 #include "components/content_settings/core/browser/host_content_settings_map.h"
15 #include "content/public/browser/permission_type.h" 16 #include "content/public/browser/permission_type.h"
16 #include "content/public/browser/render_frame_host.h" 17 #include "content/public/browser/render_frame_host.h"
17 #include "content/public/browser/render_process_host.h" 18 #include "content/public/browser/render_process_host.h"
18 #include "content/public/browser/web_contents.h" 19 #include "content/public/browser/web_contents.h"
19 20
20 using content::PermissionStatus; 21 using content::PermissionStatus;
21 using content::PermissionType; 22 using content::PermissionType;
22 23
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
72 case PermissionType::NUM: 73 case PermissionType::NUM:
73 // This will hit the NOTREACHED below. 74 // This will hit the NOTREACHED below.
74 break; 75 break;
75 } 76 }
76 77
77 NOTREACHED() << "Unknown content setting for permission " 78 NOTREACHED() << "Unknown content setting for permission "
78 << static_cast<int>(permission); 79 << static_cast<int>(permission);
79 return CONTENT_SETTINGS_TYPE_DEFAULT; 80 return CONTENT_SETTINGS_TYPE_DEFAULT;
80 } 81 }
81 82
82 // Helper method that wraps a callback a void(PermissionStatus)
83 // callback into a void(ContentSetting) callback.
84 void PermissionStatusCallbackWrapper(
85 const base::Callback<void(PermissionStatus)>& callback,
86 ContentSetting content_setting) {
87 callback.Run(ContentSettingToPermissionStatus(content_setting));
88 }
89
90 // Returns whether the permission has a constant PermissionStatus value (i.e. 83 // Returns whether the permission has a constant PermissionStatus value (i.e.
91 // always approved or always denied) 84 // always approved or always denied)
92 // The PermissionTypes for which true is returned should be exactly those which 85 // The PermissionTypes for which true is returned should be exactly those which
93 // return nullptr in PermissionContext::Get since they don't have a context. 86 // return nullptr in PermissionContext::Get since they don't have a context.
94 bool IsConstantPermission(PermissionType type) { 87 bool IsConstantPermission(PermissionType type) {
95 switch (type) { 88 switch (type) {
96 case PermissionType::MIDI: 89 case PermissionType::MIDI:
97 return true; 90 return true;
98 default: 91 default:
99 return false; 92 return false;
(...skipping 15 matching lines...) Expand all
115 } 108 }
116 } 109 }
117 110
118 PermissionStatus GetPermissionStatusForConstantPermission(PermissionType type) { 111 PermissionStatus GetPermissionStatusForConstantPermission(PermissionType type) {
119 return ContentSettingToPermissionStatus( 112 return ContentSettingToPermissionStatus(
120 GetContentSettingForConstantPermission(type)); 113 GetContentSettingForConstantPermission(type));
121 } 114 }
122 115
123 } // anonymous namespace 116 } // anonymous namespace
124 117
118 struct PermissionManager::PendingRequest {
119 PendingRequest(PermissionType permission,
120 content::RenderFrameHost* render_frame_host)
121 : permission(permission),
122 render_process_id(render_frame_host->GetProcess()->GetID()),
123 render_frame_id(render_frame_host->GetRoutingID()) {
124 }
125
126 PermissionType permission;
127 int render_process_id;
128 int render_frame_id;
129 };
130
125 struct PermissionManager::Subscription { 131 struct PermissionManager::Subscription {
126 PermissionType permission; 132 PermissionType permission;
127 GURL requesting_origin; 133 GURL requesting_origin;
128 GURL embedding_origin; 134 GURL embedding_origin;
129 base::Callback<void(PermissionStatus)> callback; 135 base::Callback<void(PermissionStatus)> callback;
130 ContentSetting current_value; 136 ContentSetting current_value;
131 }; 137 };
132 138
133 PermissionManager::PermissionManager(Profile* profile) 139 PermissionManager::PermissionManager(Profile* profile)
134 : profile_(profile) { 140 : profile_(profile),
141 weak_ptr_factory_(this) {
135 } 142 }
136 143
137 PermissionManager::~PermissionManager() { 144 PermissionManager::~PermissionManager() {
138 if (!subscriptions_.IsEmpty()) 145 if (!subscriptions_.IsEmpty())
139 HostContentSettingsMapFactory::GetForProfile(profile_) 146 HostContentSettingsMapFactory::GetForProfile(profile_)
140 ->RemoveObserver(this); 147 ->RemoveObserver(this);
141 } 148 }
142 149
143 void PermissionManager::RequestPermission( 150 int PermissionManager::RequestPermission(
144 PermissionType permission, 151 PermissionType permission,
145 content::RenderFrameHost* render_frame_host, 152 content::RenderFrameHost* render_frame_host,
146 int request_id,
147 const GURL& requesting_origin, 153 const GURL& requesting_origin,
148 bool user_gesture, 154 bool user_gesture,
149 const base::Callback<void(PermissionStatus)>& callback) { 155 const base::Callback<void(PermissionStatus)>& callback) {
150 if (IsConstantPermission(permission)) { 156 if (IsConstantPermission(permission)) {
151 callback.Run(GetPermissionStatusForConstantPermission(permission)); 157 callback.Run(GetPermissionStatusForConstantPermission(permission));
152 return; 158 return kNoPendingOperation;
153 } 159 }
154 160
155 PermissionContextBase* context = PermissionContext::Get(profile_, permission); 161 PermissionContextBase* context = PermissionContext::Get(profile_, permission);
156 if (!context) { 162 if (!context) {
157 callback.Run(content::PERMISSION_STATUS_DENIED); 163 callback.Run(content::PERMISSION_STATUS_DENIED);
158 return; 164 return kNoPendingOperation;
159 } 165 }
160 166
161 content::WebContents* web_contents = 167 content::WebContents* web_contents =
162 content::WebContents::FromRenderFrameHost(render_frame_host); 168 content::WebContents::FromRenderFrameHost(render_frame_host);
163 if (IsPermissionBubbleManagerMissing(web_contents)) { 169 if (IsPermissionBubbleManagerMissing(web_contents)) {
164 callback.Run( 170 callback.Run(
165 GetPermissionStatus(permission, requesting_origin, 171 GetPermissionStatus(permission, requesting_origin,
166 web_contents->GetLastCommittedURL().GetOrigin())); 172 web_contents->GetLastCommittedURL().GetOrigin()));
167 return; 173 return kNoPendingOperation;
168 } 174 }
169 175
170 int render_process_id = render_frame_host->GetProcess()->GetID(); 176 PendingRequest* pending_request = new PendingRequest(
171 int render_frame_id = render_frame_host->GetRoutingID(); 177 permission, render_frame_host);
172 const PermissionRequestID request(render_process_id, 178 int request_id = pending_requests_.Add(pending_request);
173 render_frame_id, 179 const PermissionRequestID request(pending_request->render_process_id,
180 pending_request->render_frame_id,
174 request_id); 181 request_id);
175 182
176 context->RequestPermission( 183 context->RequestPermission(
177 web_contents, request, requesting_origin, user_gesture, 184 web_contents, request, requesting_origin, user_gesture,
178 base::Bind(&PermissionStatusCallbackWrapper, callback)); 185 base::Bind(&PermissionManager::OnPermissionRequestResponse,
186 weak_ptr_factory_.GetWeakPtr(),
187 request_id,
188 callback));
189 return request_id;
179 } 190 }
180 191
181 void PermissionManager::CancelPermissionRequest( 192 void PermissionManager::OnPermissionRequestResponse(
182 PermissionType permission,
183 content::RenderFrameHost* render_frame_host,
184 int request_id, 193 int request_id,
185 const GURL& requesting_origin) { 194 const base::Callback<void(PermissionStatus)>& callback,
186 PermissionContextBase* context = PermissionContext::Get(profile_, permission); 195 ContentSetting content_setting) {
196 pending_requests_.Remove(request_id);
197 callback.Run(ContentSettingToPermissionStatus(content_setting));
198 }
199
200 void PermissionManager::CancelPermissionRequest(int request_id) {
201 PendingRequest* pending_request = pending_requests_.Lookup(request_id);
202 if (!pending_request)
203 return;
204
205 PermissionContextBase* context = PermissionContext::Get(
206 profile_, pending_request->permission);
187 if (!context) 207 if (!context)
188 return; 208 return;
189 209
190 content::WebContents* web_contents = 210 content::WebContents* web_contents = tab_util::GetWebContentsByFrameID(
191 content::WebContents::FromRenderFrameHost(render_frame_host); 211 pending_request->render_process_id, pending_request->render_frame_id);
192 if (IsPermissionBubbleManagerMissing(web_contents)) 212 DCHECK(web_contents);
213 if (IsPermissionBubbleManagerMissing(web_contents)) {
214 pending_requests_.Remove(request_id);
193 return; 215 return;
216 }
194 217
195 int render_process_id = render_frame_host->GetProcess()->GetID(); 218 const PermissionRequestID request(pending_request->render_process_id,
196 int render_frame_id = render_frame_host->GetRoutingID(); 219 pending_request->render_frame_id,
197 const PermissionRequestID request(render_process_id,
198 render_frame_id,
199 request_id); 220 request_id);
200
201 context->CancelPermissionRequest(web_contents, request); 221 context->CancelPermissionRequest(web_contents, request);
222 pending_requests_.Remove(request_id);
202 } 223 }
203 224
204 void PermissionManager::ResetPermission(PermissionType permission, 225 void PermissionManager::ResetPermission(PermissionType permission,
205 const GURL& requesting_origin, 226 const GURL& requesting_origin,
206 const GURL& embedding_origin) { 227 const GURL& embedding_origin) {
207 PermissionContextBase* context = PermissionContext::Get(profile_, permission); 228 PermissionContextBase* context = PermissionContext::Get(profile_, permission);
208 if (!context) 229 if (!context)
209 return; 230 return;
210 231
211 context->ResetPermission(requesting_origin.GetOrigin(), 232 context->ResetPermission(requesting_origin.GetOrigin(),
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
319 // Add the callback to |callbacks| which will be run after the loop to 340 // Add the callback to |callbacks| which will be run after the loop to
320 // prevent re-entrance issues. 341 // prevent re-entrance issues.
321 callbacks.push_back( 342 callbacks.push_back(
322 base::Bind(subscription->callback, 343 base::Bind(subscription->callback,
323 ContentSettingToPermissionStatus(new_value))); 344 ContentSettingToPermissionStatus(new_value)));
324 } 345 }
325 346
326 for (const auto& callback : callbacks) 347 for (const auto& callback : callbacks)
327 callback.Run(); 348 callback.Run();
328 } 349 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698