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

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: Fix test failures Created 5 years, 3 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 PermissionType permission;
120 int render_process_id;
121 int render_frame_id;
122 };
123
125 struct PermissionManager::Subscription { 124 struct PermissionManager::Subscription {
126 PermissionType permission; 125 PermissionType permission;
127 GURL requesting_origin; 126 GURL requesting_origin;
128 GURL embedding_origin; 127 GURL embedding_origin;
129 base::Callback<void(PermissionStatus)> callback; 128 base::Callback<void(PermissionStatus)> callback;
130 ContentSetting current_value; 129 ContentSetting current_value;
131 }; 130 };
132 131
133 PermissionManager::PermissionManager(Profile* profile) 132 PermissionManager::PermissionManager(Profile* profile)
134 : profile_(profile) { 133 : profile_(profile),
134 weak_ptr_factory_(this) {
135 } 135 }
136 136
137 PermissionManager::~PermissionManager() { 137 PermissionManager::~PermissionManager() {
138 if (!subscriptions_.IsEmpty()) 138 if (!subscriptions_.IsEmpty())
139 HostContentSettingsMapFactory::GetForProfile(profile_) 139 HostContentSettingsMapFactory::GetForProfile(profile_)
140 ->RemoveObserver(this); 140 ->RemoveObserver(this);
141 } 141 }
142 142
143 void PermissionManager::RequestPermission( 143 int PermissionManager::RequestPermission(
144 PermissionType permission, 144 PermissionType permission,
145 content::RenderFrameHost* render_frame_host, 145 content::RenderFrameHost* render_frame_host,
146 int request_id,
147 const GURL& requesting_origin, 146 const GURL& requesting_origin,
148 bool user_gesture, 147 bool user_gesture,
149 const base::Callback<void(PermissionStatus)>& callback) { 148 const base::Callback<void(PermissionStatus)>& callback) {
150 if (IsConstantPermission(permission)) { 149 if (IsConstantPermission(permission)) {
151 callback.Run(GetPermissionStatusForConstantPermission(permission)); 150 callback.Run(GetPermissionStatusForConstantPermission(permission));
152 return; 151 return kCancelUnsubscribeNoOp;
153 } 152 }
154 153
155 PermissionContextBase* context = PermissionContext::Get(profile_, permission); 154 PermissionContextBase* context = PermissionContext::Get(profile_, permission);
156 if (!context) { 155 if (!context) {
157 callback.Run(content::PERMISSION_STATUS_DENIED); 156 callback.Run(content::PERMISSION_STATUS_DENIED);
158 return; 157 return kCancelUnsubscribeNoOp;
159 } 158 }
160 159
161 content::WebContents* web_contents = 160 content::WebContents* web_contents =
162 content::WebContents::FromRenderFrameHost(render_frame_host); 161 content::WebContents::FromRenderFrameHost(render_frame_host);
163 if (IsPermissionBubbleManagerMissing(web_contents)) { 162 if (IsPermissionBubbleManagerMissing(web_contents)) {
164 callback.Run( 163 callback.Run(
165 GetPermissionStatus(permission, requesting_origin, 164 GetPermissionStatus(permission, requesting_origin,
166 web_contents->GetLastCommittedURL().GetOrigin())); 165 web_contents->GetLastCommittedURL().GetOrigin()));
167 return; 166 return kCancelUnsubscribeNoOp;
168 } 167 }
169 168
170 int render_process_id = render_frame_host->GetProcess()->GetID(); 169 int render_process_id = render_frame_host->GetProcess()->GetID();
171 int render_frame_id = render_frame_host->GetRoutingID(); 170 int render_frame_id = render_frame_host->GetRoutingID();
171
172 PendingRequest* pending_request = new PendingRequest();
173 pending_request->render_process_id = render_process_id;
174 pending_request->render_frame_id = render_frame_id;
175 pending_request->permission = permission;
176
177 int request_id = pending_requests_.Add(pending_request);
172 const PermissionRequestID request(render_process_id, 178 const PermissionRequestID request(render_process_id,
mlamouri (slow - plz ping) 2015/09/16 14:42:56 Will we be able to remove that at some point?
Lalit Maganti 2015/09/16 16:41:10 Hoping so yes. But not unless the infobar manager
173 render_frame_id, 179 render_frame_id,
174 request_id); 180 request_id);
175 181
176 context->RequestPermission( 182 context->RequestPermission(
177 web_contents, request, requesting_origin, user_gesture, 183 web_contents, request, requesting_origin, user_gesture,
178 base::Bind(&PermissionStatusCallbackWrapper, callback)); 184 base::Bind(&PermissionManager::OnPermissionRequestResponse,
185 weak_ptr_factory_.GetWeakPtr(),
186 request_id,
187 callback));
188 return request_id;
179 } 189 }
180 190
181 void PermissionManager::CancelPermissionRequest( 191 void PermissionManager::OnPermissionRequestResponse(
182 PermissionType permission,
183 content::RenderFrameHost* render_frame_host,
184 int request_id, 192 int request_id,
185 const GURL& requesting_origin) { 193 const base::Callback<void(PermissionStatus)>& callback,
186 PermissionContextBase* context = PermissionContext::Get(profile_, permission); 194 ContentSetting content_setting) {
195 pending_requests_.Remove(request_id);
196 callback.Run(ContentSettingToPermissionStatus(content_setting));
197 }
198
199 void PermissionManager::CancelPermissionRequest(int request_id) {
200 PendingRequest* pending_request = pending_requests_.Lookup(request_id);
201 if (!pending_request)
202 return;
203
204 PermissionContextBase* context = PermissionContext::Get(
205 profile_, pending_request->permission);
187 if (!context) 206 if (!context)
188 return; 207 return;
189 208
190 content::WebContents* web_contents = 209 content::WebContents* web_contents = tab_util::GetWebContentsByFrameID(
191 content::WebContents::FromRenderFrameHost(render_frame_host); 210 pending_request->render_process_id, pending_request->render_frame_id);
192 if (IsPermissionBubbleManagerMissing(web_contents)) 211 DCHECK(web_contents);
212 if (IsPermissionBubbleManagerMissing(web_contents)) {
213 pending_requests_.Remove(request_id);
193 return; 214 return;
215 }
194 216
195 int render_process_id = render_frame_host->GetProcess()->GetID(); 217 const PermissionRequestID request(pending_request->render_process_id,
196 int render_frame_id = render_frame_host->GetRoutingID(); 218 pending_request->render_frame_id,
197 const PermissionRequestID request(render_process_id,
198 render_frame_id,
199 request_id); 219 request_id);
200
201 context->CancelPermissionRequest(web_contents, request); 220 context->CancelPermissionRequest(web_contents, request);
221 pending_requests_.Remove(request_id);
202 } 222 }
203 223
204 void PermissionManager::ResetPermission(PermissionType permission, 224 void PermissionManager::ResetPermission(PermissionType permission,
205 const GURL& requesting_origin, 225 const GURL& requesting_origin,
206 const GURL& embedding_origin) { 226 const GURL& embedding_origin) {
207 PermissionContextBase* context = PermissionContext::Get(profile_, permission); 227 PermissionContextBase* context = PermissionContext::Get(profile_, permission);
208 if (!context) 228 if (!context)
209 return; 229 return;
210 230
211 context->ResetPermission(requesting_origin.GetOrigin(), 231 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 339 // Add the callback to |callbacks| which will be run after the loop to
320 // prevent re-entrance issues. 340 // prevent re-entrance issues.
321 callbacks.push_back( 341 callbacks.push_back(
322 base::Bind(subscription->callback, 342 base::Bind(subscription->callback,
323 ContentSettingToPermissionStatus(new_value))); 343 ContentSettingToPermissionStatus(new_value)));
324 } 344 }
325 345
326 for (const auto& callback : callbacks) 346 for (const auto& callback : callbacks)
327 callback.Run(); 347 callback.Run();
328 } 348 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698