Chromium Code Reviews| 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 <memory> | 7 #include <memory> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 47 | 47 |
| 48 using blink::mojom::PermissionStatus; | 48 using blink::mojom::PermissionStatus; |
| 49 using content::PermissionType; | 49 using content::PermissionType; |
| 50 | 50 |
| 51 namespace { | 51 namespace { |
| 52 | 52 |
| 53 // Helper method to convert ContentSetting to PermissionStatus. | 53 // Helper method to convert ContentSetting to PermissionStatus. |
| 54 PermissionStatus ContentSettingToPermissionStatus(ContentSetting setting) { | 54 PermissionStatus ContentSettingToPermissionStatus(ContentSetting setting) { |
| 55 switch (setting) { | 55 switch (setting) { |
| 56 case CONTENT_SETTING_ALLOW: | 56 case CONTENT_SETTING_ALLOW: |
| 57 case CONTENT_SETTING_SESSION_ONLY: | |
| 58 return PermissionStatus::GRANTED; | 57 return PermissionStatus::GRANTED; |
| 59 case CONTENT_SETTING_BLOCK: | 58 case CONTENT_SETTING_BLOCK: |
| 60 return PermissionStatus::DENIED; | 59 return PermissionStatus::DENIED; |
| 61 case CONTENT_SETTING_ASK: | 60 case CONTENT_SETTING_ASK: |
| 62 return PermissionStatus::ASK; | 61 return PermissionStatus::ASK; |
| 62 case CONTENT_SETTING_SESSION_ONLY: | |
| 63 case CONTENT_SETTING_DETECT_IMPORTANT_CONTENT: | 63 case CONTENT_SETTING_DETECT_IMPORTANT_CONTENT: |
| 64 case CONTENT_SETTING_DEFAULT: | 64 case CONTENT_SETTING_DEFAULT: |
| 65 case CONTENT_SETTING_NUM_SETTINGS: | 65 case CONTENT_SETTING_NUM_SETTINGS: |
| 66 break; | 66 break; |
| 67 } | 67 } |
| 68 | 68 |
| 69 NOTREACHED(); | 69 NOTREACHED(); |
| 70 return PermissionStatus::DENIED; | 70 return PermissionStatus::DENIED; |
| 71 } | 71 } |
| 72 | 72 |
| 73 // Wrap a callback taking a PermissionStatus to pass it as a callback taking a | |
| 74 // ContentSetting. | |
| 75 void ContentSettingToPermissionStatusCallbackWrapper( | |
| 76 const base::Callback<void(PermissionStatus)>& callback, | |
| 77 ContentSetting setting) { | |
| 78 callback.Run(ContentSettingToPermissionStatus(setting)); | |
| 79 } | |
| 80 | |
| 81 // Helper method to convert PermissionType to ContentSettingType. | 73 // Helper method to convert PermissionType to ContentSettingType. |
| 82 ContentSettingsType PermissionTypeToContentSetting(PermissionType permission) { | 74 ContentSettingsType PermissionTypeToContentSetting(PermissionType permission) { |
| 83 switch (permission) { | 75 switch (permission) { |
| 84 case PermissionType::MIDI: | 76 case PermissionType::MIDI: |
| 85 return CONTENT_SETTINGS_TYPE_MIDI; | 77 return CONTENT_SETTINGS_TYPE_MIDI; |
| 86 case PermissionType::MIDI_SYSEX: | 78 case PermissionType::MIDI_SYSEX: |
| 87 return CONTENT_SETTINGS_TYPE_MIDI_SYSEX; | 79 return CONTENT_SETTINGS_TYPE_MIDI_SYSEX; |
| 88 case PermissionType::PUSH_MESSAGING: | 80 case PermissionType::PUSH_MESSAGING: |
| 89 return CONTENT_SETTINGS_TYPE_PUSH_MESSAGING; | 81 return CONTENT_SETTINGS_TYPE_PUSH_MESSAGING; |
| 90 case PermissionType::NOTIFICATIONS: | 82 case PermissionType::NOTIFICATIONS: |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 111 case PermissionType::NUM: | 103 case PermissionType::NUM: |
| 112 // This will hit the NOTREACHED below. | 104 // This will hit the NOTREACHED below. |
| 113 break; | 105 break; |
| 114 } | 106 } |
| 115 | 107 |
| 116 NOTREACHED() << "Unknown content setting for permission " | 108 NOTREACHED() << "Unknown content setting for permission " |
| 117 << static_cast<int>(permission); | 109 << static_cast<int>(permission); |
| 118 return CONTENT_SETTINGS_TYPE_DEFAULT; | 110 return CONTENT_SETTINGS_TYPE_DEFAULT; |
| 119 } | 111 } |
| 120 | 112 |
| 121 // Returns whether the permission has a constant PermissionStatus value (i.e. | 113 // Returns whether the permission has a constant ContentSetting value (i.e. |
| 122 // always approved or always denied) | 114 // always approved or always denied) |
| 123 // The ContentSettingsTypes for which true is returned will also return nullptr | 115 // The ContentSettingsTypes for which true is returned will also return nullptr |
| 124 // in PermissionManager::GetPermissionContext since they don't have a context. | 116 // in PermissionManager::GetPermissionContext since they don't have a context. |
| 125 bool IsConstantPermission(ContentSettingsType type) { | 117 bool IsConstantPermission(ContentSettingsType type) { |
| 126 return type == CONTENT_SETTINGS_TYPE_MIDI; | 118 return type == CONTENT_SETTINGS_TYPE_MIDI; |
| 127 } | 119 } |
| 128 | 120 |
| 129 void PermissionRequestResponseCallbackWrapper( | 121 void SubscriptionCallbackWrapper( |
| 130 const base::Callback<void(PermissionStatus)>& callback, | 122 const base::Callback<void(PermissionStatus)>& callback, |
| 131 const std::vector<PermissionStatus>& vector) { | 123 ContentSetting content_setting) { |
| 124 callback.Run(ContentSettingToPermissionStatus(content_setting)); | |
| 125 } | |
| 126 | |
| 127 void PermissionStatusCallbackWrapper( | |
| 128 const base::Callback<void(PermissionStatus)>& callback, | |
| 129 const std::vector<ContentSetting>& vector) { | |
| 130 DCHECK_EQ(vector.size(), 1ul); | |
|
raymes
2017/02/28 01:37:34
nit: reverse params
Timothy Loh
2017/02/28 04:02:46
fixed
| |
| 131 callback.Run(ContentSettingToPermissionStatus(vector[0])); | |
| 132 } | |
| 133 | |
| 134 void PermissionStatusVectorCallbackWrapper( | |
| 135 const base::Callback<void(const std::vector<PermissionStatus>&)>& callback, | |
| 136 const std::vector<ContentSetting>& content_settings) { | |
| 137 std::vector<PermissionStatus> permission_statuses; | |
| 138 std::transform(content_settings.begin(), content_settings.end(), | |
| 139 back_inserter(permission_statuses), | |
| 140 ContentSettingToPermissionStatus); | |
| 141 callback.Run(permission_statuses); | |
| 142 } | |
| 143 | |
| 144 void ContentSettingCallbackWraper( | |
| 145 const base::Callback<void(ContentSetting)>& callback, | |
| 146 const std::vector<ContentSetting>& vector) { | |
| 132 DCHECK_EQ(vector.size(), 1ul); | 147 DCHECK_EQ(vector.size(), 1ul); |
| 133 callback.Run(vector[0]); | 148 callback.Run(vector[0]); |
| 134 } | 149 } |
| 135 | 150 |
| 136 // Function used for handling permission types which do not change their | 151 // Function used for handling permission types which do not change their |
| 137 // value i.e. they are always approved or always denied etc. | 152 // value i.e. they are always approved or always denied etc. |
| 138 // CONTENT_SETTING_DEFAULT is returned if the permission needs further handling. | 153 // CONTENT_SETTING_DEFAULT is returned if the permission needs further handling. |
| 139 // This function should only be called when IsConstantPermission has returned | 154 // This function should only be called when IsConstantPermission has returned |
| 140 // true for the PermissionType. | 155 // true for the PermissionType. |
| 141 blink::mojom::PermissionStatus GetPermissionStatusForConstantPermission( | 156 ContentSetting GetContentSettingForConstantPermission( |
| 142 ContentSettingsType type) { | 157 ContentSettingsType type) { |
| 143 DCHECK(IsConstantPermission(type)); | 158 DCHECK(IsConstantPermission(type)); |
| 144 switch (type) { | 159 switch (type) { |
| 145 case CONTENT_SETTINGS_TYPE_MIDI: | 160 case CONTENT_SETTINGS_TYPE_MIDI: |
| 146 return PermissionStatus::GRANTED; | 161 return CONTENT_SETTING_ALLOW; |
| 147 default: | 162 default: |
| 148 return PermissionStatus::DENIED; | 163 return CONTENT_SETTING_BLOCK; |
| 149 } | 164 } |
| 150 } | 165 } |
| 151 | 166 |
| 152 } // anonymous namespace | 167 } // anonymous namespace |
| 153 | 168 |
| 154 class PermissionManager::PendingRequest { | 169 class PermissionManager::PendingRequest { |
| 155 public: | 170 public: |
| 156 PendingRequest( | 171 PendingRequest( |
| 157 content::RenderFrameHost* render_frame_host, | 172 content::RenderFrameHost* render_frame_host, |
| 158 const std::vector<ContentSettingsType>& permissions, | 173 const std::vector<ContentSettingsType>& permissions, |
| 159 const base::Callback<void(const std::vector<PermissionStatus>&)>& | 174 const base::Callback<void(const std::vector<ContentSetting>&)>& callback) |
| 160 callback) | |
| 161 : render_process_id_(render_frame_host->GetProcess()->GetID()), | 175 : render_process_id_(render_frame_host->GetProcess()->GetID()), |
| 162 render_frame_id_(render_frame_host->GetRoutingID()), | 176 render_frame_id_(render_frame_host->GetRoutingID()), |
| 163 callback_(callback), | 177 callback_(callback), |
| 164 permissions_(permissions), | 178 permissions_(permissions), |
| 165 results_(permissions.size(), PermissionStatus::DENIED), | 179 results_(permissions.size(), CONTENT_SETTING_BLOCK), |
| 166 remaining_results_(permissions.size()) {} | 180 remaining_results_(permissions.size()) {} |
| 167 | 181 |
| 168 void SetPermissionStatus(int permission_id, PermissionStatus status) { | 182 void SetContentSetting(int permission_id, ContentSetting content_setting) { |
| 169 DCHECK(!IsComplete()); | 183 DCHECK(!IsComplete()); |
| 170 | 184 |
| 171 results_[permission_id] = status; | 185 results_[permission_id] = content_setting; |
| 172 --remaining_results_; | 186 --remaining_results_; |
| 173 } | 187 } |
| 174 | 188 |
| 175 bool IsComplete() const { | 189 bool IsComplete() const { |
| 176 return remaining_results_ == 0; | 190 return remaining_results_ == 0; |
| 177 } | 191 } |
| 178 | 192 |
| 179 int render_process_id() const { return render_process_id_; } | 193 int render_process_id() const { return render_process_id_; } |
| 180 int render_frame_id() const { return render_frame_id_; } | 194 int render_frame_id() const { return render_frame_id_; } |
| 181 | 195 |
| 182 const base::Callback<void(const std::vector<PermissionStatus>&)> | 196 const base::Callback<void(const std::vector<ContentSetting>&)> callback() |
| 183 callback() const { | 197 const { |
| 184 return callback_; | 198 return callback_; |
| 185 } | 199 } |
| 186 | 200 |
| 187 std::vector<ContentSettingsType> permissions() const { | 201 std::vector<ContentSettingsType> permissions() const { |
| 188 return permissions_; | 202 return permissions_; |
| 189 } | 203 } |
| 190 | 204 |
| 191 std::vector<PermissionStatus> results() const { | 205 std::vector<ContentSetting> results() const { return results_; } |
| 192 return results_; | |
| 193 } | |
| 194 | 206 |
| 195 private: | 207 private: |
| 196 int render_process_id_; | 208 int render_process_id_; |
| 197 int render_frame_id_; | 209 int render_frame_id_; |
| 198 const base::Callback<void(const std::vector<PermissionStatus>&)> callback_; | 210 const base::Callback<void(const std::vector<ContentSetting>&)> callback_; |
| 199 std::vector<ContentSettingsType> permissions_; | 211 std::vector<ContentSettingsType> permissions_; |
| 200 std::vector<PermissionStatus> results_; | 212 std::vector<ContentSetting> results_; |
| 201 size_t remaining_results_; | 213 size_t remaining_results_; |
| 202 }; | 214 }; |
| 203 | 215 |
| 204 struct PermissionManager::Subscription { | 216 struct PermissionManager::Subscription { |
| 205 ContentSettingsType permission; | 217 ContentSettingsType permission; |
| 206 GURL requesting_origin; | 218 GURL requesting_origin; |
| 207 GURL embedding_origin; | 219 GURL embedding_origin; |
| 208 base::Callback<void(PermissionStatus)> callback; | 220 base::Callback<void(ContentSetting)> callback; |
| 209 PermissionStatus current_value; | 221 ContentSetting current_value; |
| 210 }; | 222 }; |
| 211 | 223 |
| 212 // static | 224 // static |
| 213 PermissionManager* PermissionManager::Get(Profile* profile) { | 225 PermissionManager* PermissionManager::Get(Profile* profile) { |
| 214 return PermissionManagerFactory::GetForProfile(profile); | 226 return PermissionManagerFactory::GetForProfile(profile); |
| 215 } | 227 } |
| 216 | 228 |
| 217 PermissionManager::PermissionManager(Profile* profile) | 229 PermissionManager::PermissionManager(Profile* profile) |
| 218 : profile_(profile), | 230 : profile_(profile), |
| 219 weak_ptr_factory_(this) { | 231 weak_ptr_factory_(this) { |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 256 if (!subscriptions_.IsEmpty()) | 268 if (!subscriptions_.IsEmpty()) |
| 257 HostContentSettingsMapFactory::GetForProfile(profile_) | 269 HostContentSettingsMapFactory::GetForProfile(profile_) |
| 258 ->RemoveObserver(this); | 270 ->RemoveObserver(this); |
| 259 } | 271 } |
| 260 | 272 |
| 261 int PermissionManager::RequestPermission( | 273 int PermissionManager::RequestPermission( |
| 262 ContentSettingsType content_settings_type, | 274 ContentSettingsType content_settings_type, |
| 263 content::RenderFrameHost* render_frame_host, | 275 content::RenderFrameHost* render_frame_host, |
| 264 const GURL& requesting_origin, | 276 const GURL& requesting_origin, |
| 265 bool user_gesture, | 277 bool user_gesture, |
| 266 const base::Callback<void(PermissionStatus)>& callback) { | 278 const base::Callback<void(ContentSetting)>& callback) { |
| 267 return RequestPermissions( | 279 return RequestPermissions( |
| 268 std::vector<ContentSettingsType>(1, content_settings_type), | 280 std::vector<ContentSettingsType>(1, content_settings_type), |
| 269 render_frame_host, requesting_origin, user_gesture, | 281 render_frame_host, requesting_origin, user_gesture, |
| 270 base::Bind(&PermissionRequestResponseCallbackWrapper, callback)); | 282 base::Bind(&ContentSettingCallbackWraper, callback)); |
| 271 } | 283 } |
| 272 | 284 |
| 273 int PermissionManager::RequestPermissions( | 285 int PermissionManager::RequestPermissions( |
| 274 const std::vector<ContentSettingsType>& permissions, | 286 const std::vector<ContentSettingsType>& permissions, |
| 275 content::RenderFrameHost* render_frame_host, | 287 content::RenderFrameHost* render_frame_host, |
| 276 const GURL& requesting_origin, | 288 const GURL& requesting_origin, |
| 277 bool user_gesture, | 289 bool user_gesture, |
| 278 const base::Callback<void(const std::vector<PermissionStatus>&)>& | 290 const base::Callback<void(const std::vector<ContentSetting>&)>& callback) { |
| 279 callback) { | |
| 280 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 291 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 281 if (permissions.empty()) { | 292 if (permissions.empty()) { |
| 282 callback.Run(std::vector<PermissionStatus>()); | 293 callback.Run(std::vector<ContentSetting>()); |
| 283 return kNoPendingOperation; | 294 return kNoPendingOperation; |
| 284 } | 295 } |
| 285 | 296 |
| 286 content::WebContents* web_contents = | 297 content::WebContents* web_contents = |
| 287 content::WebContents::FromRenderFrameHost(render_frame_host); | 298 content::WebContents::FromRenderFrameHost(render_frame_host); |
| 288 GURL embedding_origin = web_contents->GetLastCommittedURL().GetOrigin(); | 299 GURL embedding_origin = web_contents->GetLastCommittedURL().GetOrigin(); |
| 289 | 300 |
| 290 int request_id = pending_requests_.Add(base::MakeUnique<PendingRequest>( | 301 int request_id = pending_requests_.Add(base::MakeUnique<PendingRequest>( |
| 291 render_frame_host, permissions, callback)); | 302 render_frame_host, permissions, callback)); |
| 292 | 303 |
| 293 const PermissionRequestID request(render_frame_host, request_id); | 304 const PermissionRequestID request(render_frame_host, request_id); |
| 294 | 305 |
| 295 for (size_t i = 0; i < permissions.size(); ++i) { | 306 for (size_t i = 0; i < permissions.size(); ++i) { |
| 296 const ContentSettingsType permission = permissions[i]; | 307 const ContentSettingsType permission = permissions[i]; |
| 297 | 308 |
| 298 if (IsConstantPermission(permission) || !GetPermissionContext(permission)) { | 309 if (IsConstantPermission(permission) || !GetPermissionContext(permission)) { |
| 299 // Track permission request usages even for constant permissions. | 310 // Track permission request usages even for constant permissions. |
| 300 PermissionUmaUtil::PermissionRequested(permission, requesting_origin, | 311 PermissionUmaUtil::PermissionRequested(permission, requesting_origin, |
| 301 embedding_origin, profile_); | 312 embedding_origin, profile_); |
| 302 OnPermissionsRequestResponseStatus( | 313 OnPermissionsRequestResponseStatus( |
| 303 request_id, i, GetPermissionStatusForConstantPermission(permission)); | 314 request_id, i, GetContentSettingForConstantPermission(permission)); |
| 304 continue; | 315 continue; |
| 305 } | 316 } |
| 306 | 317 |
| 307 PermissionContextBase* context = GetPermissionContext(permission); | 318 PermissionContextBase* context = GetPermissionContext(permission); |
| 308 context->RequestPermission( | 319 context->RequestPermission( |
| 309 web_contents, request, requesting_origin, user_gesture, | 320 web_contents, request, requesting_origin, user_gesture, |
| 310 base::Bind(&ContentSettingToPermissionStatusCallbackWrapper, | 321 base::Bind(&PermissionManager::OnPermissionsRequestResponseStatus, |
| 311 base::Bind(&PermissionManager::OnPermissionsRequestResponseStatus, | 322 weak_ptr_factory_.GetWeakPtr(), request_id, i)); |
| 312 weak_ptr_factory_.GetWeakPtr(), request_id, i))); | |
| 313 } | 323 } |
| 314 | 324 |
| 315 // The request might have been resolved already. | 325 // The request might have been resolved already. |
| 316 if (!pending_requests_.Lookup(request_id)) | 326 if (!pending_requests_.Lookup(request_id)) |
| 317 return kNoPendingOperation; | 327 return kNoPendingOperation; |
| 318 | 328 |
| 319 return request_id; | 329 return request_id; |
| 320 } | 330 } |
| 321 | 331 |
| 322 PermissionStatus PermissionManager::GetPermissionStatus( | 332 ContentSetting PermissionManager::GetPermissionStatus( |
| 323 ContentSettingsType permission, | 333 ContentSettingsType permission, |
| 324 const GURL& requesting_origin, | 334 const GURL& requesting_origin, |
| 325 const GURL& embedding_origin) { | 335 const GURL& embedding_origin) { |
| 326 if (IsConstantPermission(permission)) | 336 if (IsConstantPermission(permission)) |
| 327 return GetPermissionStatusForConstantPermission(permission); | 337 return GetContentSettingForConstantPermission(permission); |
| 328 PermissionContextBase* context = GetPermissionContext(permission); | 338 PermissionContextBase* context = GetPermissionContext(permission); |
| 329 return ContentSettingToPermissionStatus( | 339 ContentSetting content_setting = |
| 330 context | 340 context |
| 331 ->GetPermissionStatus(requesting_origin.GetOrigin(), | 341 ->GetPermissionStatus(requesting_origin.GetOrigin(), |
| 332 embedding_origin.GetOrigin()) | 342 embedding_origin.GetOrigin()) |
| 333 .content_setting); | 343 .content_setting; |
| 344 DCHECK(content_setting == CONTENT_SETTING_ALLOW || | |
| 345 content_setting == CONTENT_SETTING_ASK || | |
| 346 content_setting == CONTENT_SETTING_BLOCK); | |
| 347 return content_setting; | |
| 334 } | 348 } |
| 335 | 349 |
| 336 int PermissionManager::RequestPermission( | 350 int PermissionManager::RequestPermission( |
| 337 PermissionType permission, | 351 PermissionType permission, |
| 338 content::RenderFrameHost* render_frame_host, | 352 content::RenderFrameHost* render_frame_host, |
| 339 const GURL& requesting_origin, | 353 const GURL& requesting_origin, |
| 340 bool user_gesture, | 354 bool user_gesture, |
| 341 const base::Callback<void(PermissionStatus)>& callback) { | 355 const base::Callback<void(PermissionStatus)>& callback) { |
| 342 ContentSettingsType content_settings_type = | 356 ContentSettingsType content_settings_type = |
| 343 PermissionTypeToContentSetting(permission); | 357 PermissionTypeToContentSetting(permission); |
| 344 return RequestPermissions( | 358 return RequestPermissions( |
| 345 std::vector<ContentSettingsType>(1, content_settings_type), | 359 std::vector<ContentSettingsType>(1, content_settings_type), |
| 346 render_frame_host, requesting_origin, user_gesture, | 360 render_frame_host, requesting_origin, user_gesture, |
| 347 base::Bind(&PermissionRequestResponseCallbackWrapper, callback)); | 361 base::Bind(&PermissionStatusCallbackWrapper, callback)); |
| 348 } | 362 } |
| 349 | 363 |
| 350 int PermissionManager::RequestPermissions( | 364 int PermissionManager::RequestPermissions( |
| 351 const std::vector<PermissionType>& permissions, | 365 const std::vector<PermissionType>& permissions, |
| 352 content::RenderFrameHost* render_frame_host, | 366 content::RenderFrameHost* render_frame_host, |
| 353 const GURL& requesting_origin, | 367 const GURL& requesting_origin, |
| 354 bool user_gesture, | 368 bool user_gesture, |
| 355 const base::Callback<void(const std::vector<PermissionStatus>&)>& | 369 const base::Callback<void(const std::vector<PermissionStatus>&)>& |
| 356 callback) { | 370 callback) { |
| 357 std::vector<ContentSettingsType> content_settings_types; | 371 std::vector<ContentSettingsType> content_settings_types; |
| 358 std::transform(permissions.begin(), permissions.end(), | 372 std::transform(permissions.begin(), permissions.end(), |
| 359 back_inserter(content_settings_types), | 373 back_inserter(content_settings_types), |
| 360 PermissionTypeToContentSetting); | 374 PermissionTypeToContentSetting); |
| 361 return RequestPermissions(content_settings_types, render_frame_host, | 375 return RequestPermissions( |
| 362 requesting_origin, user_gesture, callback); | 376 content_settings_types, render_frame_host, requesting_origin, |
| 377 user_gesture, | |
| 378 base::Bind(&PermissionStatusVectorCallbackWrapper, callback)); | |
| 363 } | 379 } |
| 364 | 380 |
| 365 PermissionContextBase* PermissionManager::GetPermissionContext( | 381 PermissionContextBase* PermissionManager::GetPermissionContext( |
| 366 ContentSettingsType type) { | 382 ContentSettingsType type) { |
| 367 const auto& it = permission_contexts_.find(type); | 383 const auto& it = permission_contexts_.find(type); |
| 368 return it == permission_contexts_.end() ? nullptr : it->second.get(); | 384 return it == permission_contexts_.end() ? nullptr : it->second.get(); |
| 369 } | 385 } |
| 370 | 386 |
| 371 void PermissionManager::OnPermissionsRequestResponseStatus( | 387 void PermissionManager::OnPermissionsRequestResponseStatus( |
| 372 int request_id, | 388 int request_id, |
| 373 int permission_id, | 389 int permission_id, |
| 374 PermissionStatus status) { | 390 ContentSetting content_setting) { |
| 375 PendingRequest* pending_request = pending_requests_.Lookup(request_id); | 391 PendingRequest* pending_request = pending_requests_.Lookup(request_id); |
| 376 pending_request->SetPermissionStatus(permission_id, status); | 392 pending_request->SetContentSetting(permission_id, content_setting); |
| 377 | 393 |
| 378 if (!pending_request->IsComplete()) | 394 if (!pending_request->IsComplete()) |
| 379 return; | 395 return; |
| 380 | 396 |
| 381 pending_request->callback().Run(pending_request->results()); | 397 pending_request->callback().Run(pending_request->results()); |
| 382 pending_requests_.Remove(request_id); | 398 pending_requests_.Remove(request_id); |
| 383 } | 399 } |
| 384 | 400 |
| 385 void PermissionManager::CancelPermissionRequest(int request_id) { | 401 void PermissionManager::CancelPermissionRequest(int request_id) { |
| 386 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 402 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 415 | 431 |
| 416 context->ResetPermission(requesting_origin.GetOrigin(), | 432 context->ResetPermission(requesting_origin.GetOrigin(), |
| 417 embedding_origin.GetOrigin()); | 433 embedding_origin.GetOrigin()); |
| 418 } | 434 } |
| 419 | 435 |
| 420 PermissionStatus PermissionManager::GetPermissionStatus( | 436 PermissionStatus PermissionManager::GetPermissionStatus( |
| 421 PermissionType permission, | 437 PermissionType permission, |
| 422 const GURL& requesting_origin, | 438 const GURL& requesting_origin, |
| 423 const GURL& embedding_origin) { | 439 const GURL& embedding_origin) { |
| 424 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 440 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 425 return GetPermissionStatus(PermissionTypeToContentSetting(permission), | 441 return ContentSettingToPermissionStatus( |
| 426 requesting_origin, embedding_origin); | 442 GetPermissionStatus(PermissionTypeToContentSetting(permission), |
| 443 requesting_origin, embedding_origin)); | |
| 427 } | 444 } |
| 428 | 445 |
| 429 int PermissionManager::SubscribePermissionStatusChange( | 446 int PermissionManager::SubscribePermissionStatusChange( |
| 430 PermissionType permission, | 447 PermissionType permission, |
| 431 const GURL& requesting_origin, | 448 const GURL& requesting_origin, |
| 432 const GURL& embedding_origin, | 449 const GURL& embedding_origin, |
| 433 const base::Callback<void(PermissionStatus)>& callback) { | 450 const base::Callback<void(PermissionStatus)>& callback) { |
| 434 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 451 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 435 if (subscriptions_.IsEmpty()) | 452 if (subscriptions_.IsEmpty()) |
| 436 HostContentSettingsMapFactory::GetForProfile(profile_)->AddObserver(this); | 453 HostContentSettingsMapFactory::GetForProfile(profile_)->AddObserver(this); |
| 437 | 454 |
| 438 ContentSettingsType content_type = PermissionTypeToContentSetting(permission); | 455 ContentSettingsType content_type = PermissionTypeToContentSetting(permission); |
| 439 auto subscription = base::MakeUnique<Subscription>(); | 456 auto subscription = base::MakeUnique<Subscription>(); |
| 440 subscription->permission = content_type; | 457 subscription->permission = content_type; |
| 441 subscription->requesting_origin = requesting_origin; | 458 subscription->requesting_origin = requesting_origin; |
| 442 subscription->embedding_origin = embedding_origin; | 459 subscription->embedding_origin = embedding_origin; |
| 443 subscription->callback = callback; | 460 subscription->callback = base::Bind(&SubscriptionCallbackWrapper, callback); |
| 444 | 461 |
| 445 subscription->current_value = | 462 subscription->current_value = |
| 446 GetPermissionStatus(content_type, requesting_origin, embedding_origin); | 463 GetPermissionStatus(content_type, requesting_origin, embedding_origin); |
| 447 | 464 |
| 448 return subscriptions_.Add(std::move(subscription)); | 465 return subscriptions_.Add(std::move(subscription)); |
| 449 } | 466 } |
| 450 | 467 |
| 451 void PermissionManager::UnsubscribePermissionStatusChange(int subscription_id) { | 468 void PermissionManager::UnsubscribePermissionStatusChange(int subscription_id) { |
| 452 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 469 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 453 // Whether |subscription_id| is known will be checked by the Remove() call. | 470 // Whether |subscription_id| is known will be checked by the Remove() call. |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 480 if (subscription->permission != content_type) | 497 if (subscription->permission != content_type) |
| 481 continue; | 498 continue; |
| 482 | 499 |
| 483 if (primary_pattern.IsValid() && | 500 if (primary_pattern.IsValid() && |
| 484 !primary_pattern.Matches(subscription->requesting_origin)) | 501 !primary_pattern.Matches(subscription->requesting_origin)) |
| 485 continue; | 502 continue; |
| 486 if (secondary_pattern.IsValid() && | 503 if (secondary_pattern.IsValid() && |
| 487 !secondary_pattern.Matches(subscription->embedding_origin)) | 504 !secondary_pattern.Matches(subscription->embedding_origin)) |
| 488 continue; | 505 continue; |
| 489 | 506 |
| 490 PermissionStatus new_value = GetPermissionStatus( | 507 ContentSetting new_value = GetPermissionStatus( |
| 491 subscription->permission, subscription->requesting_origin, | 508 subscription->permission, subscription->requesting_origin, |
| 492 subscription->embedding_origin); | 509 subscription->embedding_origin); |
| 493 if (subscription->current_value == new_value) | 510 if (subscription->current_value == new_value) |
| 494 continue; | 511 continue; |
| 495 | 512 |
| 496 subscription->current_value = new_value; | 513 subscription->current_value = new_value; |
| 497 | 514 |
| 498 // Add the callback to |callbacks| which will be run after the loop to | 515 // Add the callback to |callbacks| which will be run after the loop to |
| 499 // prevent re-entrance issues. | 516 // prevent re-entrance issues. |
| 500 callbacks.push_back(base::Bind(subscription->callback, new_value)); | 517 callbacks.push_back(base::Bind(subscription->callback, new_value)); |
| 501 } | 518 } |
| 502 | 519 |
| 503 for (const auto& callback : callbacks) | 520 for (const auto& callback : callbacks) |
| 504 callback.Run(); | 521 callback.Run(); |
| 505 } | 522 } |
| OLD | NEW |