| OLD | NEW |
| 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/permissions/permission_context.h" | 8 #include "chrome/browser/permissions/permission_context.h" |
| 9 #include "chrome/browser/permissions/permission_context_base.h" | 9 #include "chrome/browser/permissions/permission_context_base.h" |
| 10 #include "chrome/browser/permissions/permission_request_id.h" | 10 #include "chrome/browser/permissions/permission_request_id.h" |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 case PermissionType::NUM: | 71 case PermissionType::NUM: |
| 72 // This will hit the NOTREACHED below. | 72 // This will hit the NOTREACHED below. |
| 73 break; | 73 break; |
| 74 } | 74 } |
| 75 | 75 |
| 76 NOTREACHED() << "Unknown content setting for permission " | 76 NOTREACHED() << "Unknown content setting for permission " |
| 77 << static_cast<int>(permission); | 77 << static_cast<int>(permission); |
| 78 return CONTENT_SETTINGS_TYPE_DEFAULT; | 78 return CONTENT_SETTINGS_TYPE_DEFAULT; |
| 79 } | 79 } |
| 80 | 80 |
| 81 // Helper method that wraps a callback a void(PermissionStatus) | |
| 82 // callback into a void(ContentSetting) callback. | |
| 83 void PermissionStatusCallbackWrapper( | |
| 84 const base::Callback<void(PermissionStatus)>& callback, | |
| 85 ContentSetting content_setting) { | |
| 86 callback.Run(ContentSettingToPermissionStatus(content_setting)); | |
| 87 } | |
| 88 | |
| 89 // Returns whether the permission has a constant PermissionStatus value (i.e. | 81 // Returns whether the permission has a constant PermissionStatus value (i.e. |
| 90 // always approved or always denied) | 82 // always approved or always denied) |
| 91 // The PermissionTypes for which true is returned should be exactly those which | 83 // The PermissionTypes for which true is returned should be exactly those which |
| 92 // return nullptr in PermissionContext::Get since they don't have a context. | 84 // return nullptr in PermissionContext::Get since they don't have a context. |
| 93 bool IsConstantPermission(PermissionType type) { | 85 bool IsConstantPermission(PermissionType type) { |
| 94 switch (type) { | 86 switch (type) { |
| 95 case PermissionType::MIDI: | 87 case PermissionType::MIDI: |
| 96 return true; | 88 return true; |
| 97 default: | 89 default: |
| 98 return false; | 90 return false; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 114 } | 106 } |
| 115 } | 107 } |
| 116 | 108 |
| 117 PermissionStatus GetPermissionStatusForConstantPermission(PermissionType type) { | 109 PermissionStatus GetPermissionStatusForConstantPermission(PermissionType type) { |
| 118 return ContentSettingToPermissionStatus( | 110 return ContentSettingToPermissionStatus( |
| 119 GetContentSettingForConstantPermission(type)); | 111 GetContentSettingForConstantPermission(type)); |
| 120 } | 112 } |
| 121 | 113 |
| 122 } // anonymous namespace | 114 } // anonymous namespace |
| 123 | 115 |
| 116 struct PermissionManager::PendingRequest { |
| 117 PermissionType permission; |
| 118 }; |
| 119 |
| 124 struct PermissionManager::Subscription { | 120 struct PermissionManager::Subscription { |
| 125 PermissionType permission; | 121 PermissionType permission; |
| 126 GURL requesting_origin; | 122 GURL requesting_origin; |
| 127 GURL embedding_origin; | 123 GURL embedding_origin; |
| 128 base::Callback<void(PermissionStatus)> callback; | 124 base::Callback<void(PermissionStatus)> callback; |
| 129 ContentSetting current_value; | 125 ContentSetting current_value; |
| 130 }; | 126 }; |
| 131 | 127 |
| 132 PermissionManager::PermissionManager(Profile* profile) | 128 PermissionManager::PermissionManager(Profile* profile) |
| 133 : profile_(profile) { | 129 : profile_(profile), |
| 130 weak_ptr_factory_(this) { |
| 134 } | 131 } |
| 135 | 132 |
| 136 PermissionManager::~PermissionManager() { | 133 PermissionManager::~PermissionManager() { |
| 137 if (!subscriptions_.IsEmpty()) | 134 if (!subscriptions_.IsEmpty()) |
| 138 profile_->GetHostContentSettingsMap()->RemoveObserver(this); | 135 profile_->GetHostContentSettingsMap()->RemoveObserver(this); |
| 139 } | 136 } |
| 140 | 137 |
| 141 void PermissionManager::RequestPermission( | 138 int PermissionManager::RequestPermission( |
| 142 PermissionType permission, | 139 PermissionType permission, |
| 143 content::RenderFrameHost* render_frame_host, | 140 content::RenderFrameHost* render_frame_host, |
| 144 int request_id, | |
| 145 const GURL& requesting_origin, | 141 const GURL& requesting_origin, |
| 146 bool user_gesture, | 142 bool user_gesture, |
| 147 const base::Callback<void(PermissionStatus)>& callback) { | 143 const base::Callback<void(PermissionStatus)>& callback) { |
| 148 if (IsConstantPermission(permission)) { | 144 if (IsConstantPermission(permission)) { |
| 149 callback.Run(GetPermissionStatusForConstantPermission(permission)); | 145 callback.Run(GetPermissionStatusForConstantPermission(permission)); |
| 150 return; | 146 return -1; |
| 151 } | 147 } |
| 152 | 148 |
| 153 PermissionContextBase* context = PermissionContext::Get(profile_, permission); | 149 PermissionContextBase* context = PermissionContext::Get(profile_, permission); |
| 154 if (!context) { | 150 if (!context) { |
| 155 callback.Run(content::PERMISSION_STATUS_DENIED); | 151 callback.Run(content::PERMISSION_STATUS_DENIED); |
| 156 return; | 152 return -1; |
| 157 } | 153 } |
| 158 | 154 |
| 159 content::WebContents* web_contents = | 155 content::WebContents* web_contents = |
| 160 content::WebContents::FromRenderFrameHost(render_frame_host); | 156 content::WebContents::FromRenderFrameHost(render_frame_host); |
| 161 if (IsPermissionBubbleManagerMissing(web_contents)) { | 157 if (IsPermissionBubbleManagerMissing(web_contents)) { |
| 162 callback.Run( | 158 callback.Run( |
| 163 GetPermissionStatus(permission, requesting_origin, | 159 GetPermissionStatus(permission, requesting_origin, |
| 164 web_contents->GetLastCommittedURL().GetOrigin())); | 160 web_contents->GetLastCommittedURL().GetOrigin())); |
| 165 return; | 161 return -1; |
| 166 } | 162 } |
| 167 | 163 |
| 164 PendingRequest* pending_request = new PendingRequest(); |
| 165 pending_request->permission = permission; |
| 166 int request_id = pending_requests_.Add(pending_request); |
| 167 |
| 168 int render_process_id = render_frame_host->GetProcess()->GetID(); | 168 int render_process_id = render_frame_host->GetProcess()->GetID(); |
| 169 int render_frame_id = render_frame_host->GetRoutingID(); | 169 int render_frame_id = render_frame_host->GetRoutingID(); |
| 170 const PermissionRequestID request(render_process_id, | 170 const PermissionRequestID request(render_process_id, |
| 171 render_frame_id, | 171 render_frame_id, |
| 172 request_id); | 172 request_id); |
| 173 | 173 |
| 174 context->RequestPermission( | 174 context->RequestPermission( |
| 175 web_contents, request, requesting_origin, user_gesture, | 175 web_contents, request, requesting_origin, user_gesture, |
| 176 base::Bind(&PermissionStatusCallbackWrapper, callback)); | 176 base::Bind(&PermissionManager::OnPermissionRequestResponse, |
| 177 weak_ptr_factory_.GetWeakPtr(), |
| 178 request_id, |
| 179 callback)); |
| 180 return request_id; |
| 181 } |
| 182 |
| 183 void PermissionManager::OnPermissionRequestResponse( |
| 184 int request_id, |
| 185 const base::Callback<void(PermissionStatus)>& callback, |
| 186 ContentSetting content_setting) { |
| 187 pending_requests_.Remove(request_id); |
| 188 callback.Run(ContentSettingToPermissionStatus(content_setting)); |
| 177 } | 189 } |
| 178 | 190 |
| 179 void PermissionManager::CancelPermissionRequest( | 191 void PermissionManager::CancelPermissionRequest( |
| 180 PermissionType permission, | |
| 181 content::RenderFrameHost* render_frame_host, | 192 content::RenderFrameHost* render_frame_host, |
| 182 int request_id, | 193 int request_id) { |
| 183 const GURL& requesting_origin) { | 194 PendingRequest* pending_request = pending_requests_.Lookup(request_id); |
| 184 PermissionContextBase* context = PermissionContext::Get(profile_, permission); | 195 if (!pending_request) |
| 196 return; |
| 197 |
| 198 PermissionContextBase* context = PermissionContext::Get( |
| 199 profile_, pending_request->permission); |
| 200 pending_requests_.Remove(request_id); |
| 185 if (!context) | 201 if (!context) |
| 186 return; | 202 return; |
| 187 | 203 |
| 188 content::WebContents* web_contents = | 204 content::WebContents* web_contents = |
| 189 content::WebContents::FromRenderFrameHost(render_frame_host); | 205 content::WebContents::FromRenderFrameHost(render_frame_host); |
| 190 if (IsPermissionBubbleManagerMissing(web_contents)) | 206 if (IsPermissionBubbleManagerMissing(web_contents)) |
| 191 return; | 207 return; |
| 192 | 208 |
| 193 int render_process_id = render_frame_host->GetProcess()->GetID(); | 209 int render_process_id = render_frame_host->GetProcess()->GetID(); |
| 194 int render_frame_id = render_frame_host->GetRoutingID(); | 210 int render_frame_id = render_frame_host->GetRoutingID(); |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 316 // Add the callback to |callbacks| which will be run after the loop to | 332 // Add the callback to |callbacks| which will be run after the loop to |
| 317 // prevent re-entrance issues. | 333 // prevent re-entrance issues. |
| 318 callbacks.push_back( | 334 callbacks.push_back( |
| 319 base::Bind(subscription->callback, | 335 base::Bind(subscription->callback, |
| 320 ContentSettingToPermissionStatus(new_value))); | 336 ContentSettingToPermissionStatus(new_value))); |
| 321 } | 337 } |
| 322 | 338 |
| 323 for (const auto& callback : callbacks) | 339 for (const auto& callback : callbacks) |
| 324 callback.Run(); | 340 callback.Run(); |
| 325 } | 341 } |
| OLD | NEW |