| 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/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 Loading... |
| 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 Loading... |
| 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; |
| 190 } |
| 191 |
| 192 void PermissionManager::OnPermissionRequestResponse( |
| 193 int request_id, |
| 194 const base::Callback<void(PermissionStatus)>& callback, |
| 195 ContentSetting content_setting) { |
| 196 pending_requests_.Remove(request_id); |
| 197 callback.Run(ContentSettingToPermissionStatus(content_setting)); |
| 179 } | 198 } |
| 180 | 199 |
| 181 void PermissionManager::CancelPermissionRequest( | 200 void PermissionManager::CancelPermissionRequest( |
| 182 PermissionType permission, | 201 PermissionType permission, |
| 183 content::RenderFrameHost* render_frame_host, | 202 content::RenderFrameHost* render_frame_host, |
| 184 int request_id, | 203 int request_id, |
| 185 const GURL& requesting_origin) { | 204 const GURL& requesting_origin) { |
| 205 PendingRequest* pending_request = pending_requests_.Lookup(request_id); |
| 206 if (!pending_request) |
| 207 return; |
| 208 |
| 186 PermissionContextBase* context = PermissionContext::Get(profile_, permission); | 209 PermissionContextBase* context = PermissionContext::Get(profile_, permission); |
| 187 if (!context) | 210 if (!context) |
| 188 return; | 211 return; |
| 189 | 212 |
| 190 content::WebContents* web_contents = | 213 content::WebContents* web_contents = |
| 191 content::WebContents::FromRenderFrameHost(render_frame_host); | 214 content::WebContents::FromRenderFrameHost(render_frame_host); |
| 192 if (IsPermissionBubbleManagerMissing(web_contents)) | 215 if (IsPermissionBubbleManagerMissing(web_contents)) |
| 193 return; | 216 return; |
| 194 | 217 |
| 195 int render_process_id = render_frame_host->GetProcess()->GetID(); | 218 int render_process_id = render_frame_host->GetProcess()->GetID(); |
| 196 int render_frame_id = render_frame_host->GetRoutingID(); | 219 int render_frame_id = render_frame_host->GetRoutingID(); |
| 197 const PermissionRequestID request(render_process_id, | 220 const PermissionRequestID request(render_process_id, |
| 198 render_frame_id, | 221 render_frame_id, |
| 199 request_id); | 222 request_id); |
| 200 | 223 |
| 201 context->CancelPermissionRequest(web_contents, request); | 224 context->CancelPermissionRequest(web_contents, request); |
| 225 pending_requests_.Remove(request_id); |
| 202 } | 226 } |
| 203 | 227 |
| 204 void PermissionManager::ResetPermission(PermissionType permission, | 228 void PermissionManager::ResetPermission(PermissionType permission, |
| 205 const GURL& requesting_origin, | 229 const GURL& requesting_origin, |
| 206 const GURL& embedding_origin) { | 230 const GURL& embedding_origin) { |
| 207 PermissionContextBase* context = PermissionContext::Get(profile_, permission); | 231 PermissionContextBase* context = PermissionContext::Get(profile_, permission); |
| 208 if (!context) | 232 if (!context) |
| 209 return; | 233 return; |
| 210 | 234 |
| 211 context->ResetPermission(requesting_origin.GetOrigin(), | 235 context->ResetPermission(requesting_origin.GetOrigin(), |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 319 // Add the callback to |callbacks| which will be run after the loop to | 343 // Add the callback to |callbacks| which will be run after the loop to |
| 320 // prevent re-entrance issues. | 344 // prevent re-entrance issues. |
| 321 callbacks.push_back( | 345 callbacks.push_back( |
| 322 base::Bind(subscription->callback, | 346 base::Bind(subscription->callback, |
| 323 ContentSettingToPermissionStatus(new_value))); | 347 ContentSettingToPermissionStatus(new_value))); |
| 324 } | 348 } |
| 325 | 349 |
| 326 for (const auto& callback : callbacks) | 350 for (const auto& callback : callbacks) |
| 327 callback.Run(); | 351 callback.Run(); |
| 328 } | 352 } |
| OLD | NEW |