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 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 74 // ContentSetting. | 74 // ContentSetting. |
| 75 void ContentSettingToPermissionStatusCallbackWrapper( | 75 void ContentSettingToPermissionStatusCallbackWrapper( |
| 76 const base::Callback<void(PermissionStatus)>& callback, | 76 const base::Callback<void(PermissionStatus)>& callback, |
| 77 ContentSetting setting) { | 77 ContentSetting setting) { |
| 78 callback.Run(ContentSettingToPermissionStatus(setting)); | 78 callback.Run(ContentSettingToPermissionStatus(setting)); |
| 79 } | 79 } |
| 80 | 80 |
| 81 // Helper method to convert PermissionType to ContentSettingType. | 81 // Helper method to convert PermissionType to ContentSettingType. |
| 82 ContentSettingsType PermissionTypeToContentSetting(PermissionType permission) { | 82 ContentSettingsType PermissionTypeToContentSetting(PermissionType permission) { |
| 83 switch (permission) { | 83 switch (permission) { |
| 84 case PermissionType::MIDI: | |
| 85 return CONTENT_SETTINGS_TYPE_MIDI; | |
| 84 case PermissionType::MIDI_SYSEX: | 86 case PermissionType::MIDI_SYSEX: |
| 85 return CONTENT_SETTINGS_TYPE_MIDI_SYSEX; | 87 return CONTENT_SETTINGS_TYPE_MIDI_SYSEX; |
| 86 case PermissionType::PUSH_MESSAGING: | 88 case PermissionType::PUSH_MESSAGING: |
| 87 return CONTENT_SETTINGS_TYPE_PUSH_MESSAGING; | 89 return CONTENT_SETTINGS_TYPE_PUSH_MESSAGING; |
| 88 case PermissionType::NOTIFICATIONS: | 90 case PermissionType::NOTIFICATIONS: |
| 89 return CONTENT_SETTINGS_TYPE_NOTIFICATIONS; | 91 return CONTENT_SETTINGS_TYPE_NOTIFICATIONS; |
| 90 case PermissionType::GEOLOCATION: | 92 case PermissionType::GEOLOCATION: |
| 91 return CONTENT_SETTINGS_TYPE_GEOLOCATION; | 93 return CONTENT_SETTINGS_TYPE_GEOLOCATION; |
| 92 case PermissionType::PROTECTED_MEDIA_IDENTIFIER: | 94 case PermissionType::PROTECTED_MEDIA_IDENTIFIER: |
| 93 #if defined(OS_ANDROID) || defined(OS_CHROMEOS) | 95 #if defined(OS_ANDROID) || defined(OS_CHROMEOS) |
| 94 return CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER; | 96 return CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER; |
| 95 #else | 97 #else |
| 96 NOTIMPLEMENTED(); | 98 NOTIMPLEMENTED(); |
| 97 break; | 99 break; |
| 98 #endif | 100 #endif |
| 99 case PermissionType::DURABLE_STORAGE: | 101 case PermissionType::DURABLE_STORAGE: |
| 100 return CONTENT_SETTINGS_TYPE_DURABLE_STORAGE; | 102 return CONTENT_SETTINGS_TYPE_DURABLE_STORAGE; |
| 101 case PermissionType::MIDI: | |
| 102 // This will hit the NOTREACHED below. | |
| 103 break; | |
| 104 case PermissionType::AUDIO_CAPTURE: | 103 case PermissionType::AUDIO_CAPTURE: |
| 105 return CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC; | 104 return CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC; |
| 106 case PermissionType::VIDEO_CAPTURE: | 105 case PermissionType::VIDEO_CAPTURE: |
| 107 return CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA; | 106 return CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA; |
| 108 case PermissionType::BACKGROUND_SYNC: | 107 case PermissionType::BACKGROUND_SYNC: |
| 109 return CONTENT_SETTINGS_TYPE_BACKGROUND_SYNC; | 108 return CONTENT_SETTINGS_TYPE_BACKGROUND_SYNC; |
| 110 case PermissionType::FLASH: | 109 case PermissionType::FLASH: |
| 111 return CONTENT_SETTINGS_TYPE_PLUGINS; | 110 return CONTENT_SETTINGS_TYPE_PLUGINS; |
| 112 case PermissionType::NUM: | 111 case PermissionType::NUM: |
| 113 // This will hit the NOTREACHED below. | 112 // This will hit the NOTREACHED below. |
| 114 break; | 113 break; |
| 115 } | 114 } |
| 116 | 115 |
| 117 NOTREACHED() << "Unknown content setting for permission " | 116 NOTREACHED() << "Unknown content setting for permission " |
| 118 << static_cast<int>(permission); | 117 << static_cast<int>(permission); |
| 119 return CONTENT_SETTINGS_TYPE_DEFAULT; | 118 return CONTENT_SETTINGS_TYPE_DEFAULT; |
| 120 } | 119 } |
| 121 | 120 |
| 122 // Returns whether the permission has a constant PermissionStatus value (i.e. | 121 // Returns whether the permission has a constant PermissionStatus value (i.e. |
| 123 // always approved or always denied) | 122 // always approved or always denied) |
| 124 // The PermissionTypes for which true is returned should be exactly those which | 123 // The ContentSettingsTypes for which true is returned will also return nullptr |
| 125 // return nullptr in PermissionManager::GetPermissionContext since they don't | 124 // in PermissionManager::GetPermissionContext since they don't have a context. |
| 126 // have a context. | 125 bool IsConstantPermission(ContentSettingsType type) { |
| 127 bool IsConstantPermission(PermissionType type) { | |
| 128 switch (type) { | 126 switch (type) { |
| 129 case PermissionType::MIDI: | 127 case CONTENT_SETTINGS_TYPE_MIDI: |
| 130 return true; | 128 return true; |
| 131 default: | 129 default: |
| 132 return false; | 130 return false; |
| 133 } | 131 } |
|
raymes
2017/02/23 05:37:08
nit: I wonder if we should just make this:
return
Timothy Loh
2017/02/23 06:17:10
Done.
| |
| 134 } | 132 } |
| 135 | 133 |
| 136 void PermissionRequestResponseCallbackWrapper( | 134 void PermissionRequestResponseCallbackWrapper( |
| 137 const base::Callback<void(PermissionStatus)>& callback, | 135 const base::Callback<void(PermissionStatus)>& callback, |
| 138 const std::vector<PermissionStatus>& vector) { | 136 const std::vector<PermissionStatus>& vector) { |
| 139 DCHECK_EQ(vector.size(), 1ul); | 137 DCHECK_EQ(vector.size(), 1ul); |
| 140 callback.Run(vector[0]); | 138 callback.Run(vector[0]); |
| 141 } | 139 } |
| 142 | 140 |
| 143 // Function used for handling permission types which do not change their | 141 // Function used for handling permission types which do not change their |
| 144 // value i.e. they are always approved or always denied etc. | 142 // value i.e. they are always approved or always denied etc. |
| 145 // CONTENT_SETTING_DEFAULT is returned if the permission needs further handling. | 143 // CONTENT_SETTING_DEFAULT is returned if the permission needs further handling. |
| 146 // This function should only be called when IsConstantPermission has returned | 144 // This function should only be called when IsConstantPermission has returned |
| 147 // true for the PermissionType. | 145 // true for the PermissionType. |
| 148 blink::mojom::PermissionStatus GetPermissionStatusForConstantPermission( | 146 blink::mojom::PermissionStatus GetPermissionStatusForConstantPermission( |
| 149 PermissionType type) { | 147 ContentSettingsType type) { |
| 150 DCHECK(IsConstantPermission(type)); | 148 DCHECK(IsConstantPermission(type)); |
| 151 switch (type) { | 149 switch (type) { |
| 152 case PermissionType::MIDI: | 150 case CONTENT_SETTINGS_TYPE_MIDI: |
| 153 return PermissionStatus::GRANTED; | 151 return PermissionStatus::GRANTED; |
| 154 default: | 152 default: |
| 155 return PermissionStatus::DENIED; | 153 return PermissionStatus::DENIED; |
| 156 } | 154 } |
| 157 } | 155 } |
| 158 | 156 |
| 159 } // anonymous namespace | 157 } // anonymous namespace |
| 160 | 158 |
| 161 class PermissionManager::PendingRequest { | 159 class PermissionManager::PendingRequest { |
| 162 public: | 160 public: |
| 163 PendingRequest(content::RenderFrameHost* render_frame_host, | 161 PendingRequest( |
| 164 const std::vector<PermissionType> permissions, | 162 content::RenderFrameHost* render_frame_host, |
| 165 const base::Callback< | 163 const std::vector<ContentSettingsType>& permissions, |
| 166 void(const std::vector<PermissionStatus>&)>& callback) | 164 const base::Callback<void(const std::vector<PermissionStatus>&)>& |
| 165 callback) | |
| 167 : render_process_id_(render_frame_host->GetProcess()->GetID()), | 166 : render_process_id_(render_frame_host->GetProcess()->GetID()), |
| 168 render_frame_id_(render_frame_host->GetRoutingID()), | 167 render_frame_id_(render_frame_host->GetRoutingID()), |
| 169 callback_(callback), | 168 callback_(callback), |
| 170 permissions_(permissions), | 169 permissions_(permissions), |
| 171 results_(permissions.size(), PermissionStatus::DENIED), | 170 results_(permissions.size(), PermissionStatus::DENIED), |
| 172 remaining_results_(permissions.size()) {} | 171 remaining_results_(permissions.size()) {} |
| 173 | 172 |
| 174 void SetPermissionStatus(int permission_id, PermissionStatus status) { | 173 void SetPermissionStatus(int permission_id, PermissionStatus status) { |
| 175 DCHECK(!IsComplete()); | 174 DCHECK(!IsComplete()); |
| 176 | 175 |
| 177 results_[permission_id] = status; | 176 results_[permission_id] = status; |
| 178 --remaining_results_; | 177 --remaining_results_; |
| 179 } | 178 } |
| 180 | 179 |
| 181 bool IsComplete() const { | 180 bool IsComplete() const { |
| 182 return remaining_results_ == 0; | 181 return remaining_results_ == 0; |
| 183 } | 182 } |
| 184 | 183 |
| 185 int render_process_id() const { return render_process_id_; } | 184 int render_process_id() const { return render_process_id_; } |
| 186 int render_frame_id() const { return render_frame_id_; } | 185 int render_frame_id() const { return render_frame_id_; } |
| 187 | 186 |
| 188 const base::Callback<void(const std::vector<PermissionStatus>&)> | 187 const base::Callback<void(const std::vector<PermissionStatus>&)> |
| 189 callback() const { | 188 callback() const { |
| 190 return callback_; | 189 return callback_; |
| 191 } | 190 } |
| 192 | 191 |
| 193 std::vector<PermissionType> permissions() const { | 192 std::vector<ContentSettingsType> permissions() const { |
| 194 return permissions_; | 193 return permissions_; |
| 195 } | 194 } |
| 196 | 195 |
| 197 std::vector<PermissionStatus> results() const { | 196 std::vector<PermissionStatus> results() const { |
| 198 return results_; | 197 return results_; |
| 199 } | 198 } |
| 200 | 199 |
| 201 private: | 200 private: |
| 202 int render_process_id_; | 201 int render_process_id_; |
| 203 int render_frame_id_; | 202 int render_frame_id_; |
| 204 const base::Callback<void(const std::vector<PermissionStatus>&)> callback_; | 203 const base::Callback<void(const std::vector<PermissionStatus>&)> callback_; |
| 205 std::vector<PermissionType> permissions_; | 204 std::vector<ContentSettingsType> permissions_; |
| 206 std::vector<PermissionStatus> results_; | 205 std::vector<PermissionStatus> results_; |
| 207 size_t remaining_results_; | 206 size_t remaining_results_; |
| 208 }; | 207 }; |
| 209 | 208 |
| 210 struct PermissionManager::Subscription { | 209 struct PermissionManager::Subscription { |
| 211 PermissionType permission; | 210 ContentSettingsType permission; |
| 212 GURL requesting_origin; | 211 GURL requesting_origin; |
| 213 GURL embedding_origin; | 212 GURL embedding_origin; |
| 214 base::Callback<void(PermissionStatus)> callback; | 213 base::Callback<void(PermissionStatus)> callback; |
| 215 PermissionStatus current_value; | 214 PermissionStatus current_value; |
| 216 }; | 215 }; |
| 217 | 216 |
| 218 // static | 217 // static |
| 219 PermissionManager* PermissionManager::Get(Profile* profile) { | 218 PermissionManager* PermissionManager::Get(Profile* profile) { |
| 220 return PermissionManagerFactory::GetForProfile(profile); | 219 return PermissionManagerFactory::GetForProfile(profile); |
| 221 } | 220 } |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 263 HostContentSettingsMapFactory::GetForProfile(profile_) | 262 HostContentSettingsMapFactory::GetForProfile(profile_) |
| 264 ->RemoveObserver(this); | 263 ->RemoveObserver(this); |
| 265 } | 264 } |
| 266 | 265 |
| 267 int PermissionManager::RequestPermission( | 266 int PermissionManager::RequestPermission( |
| 268 ContentSettingsType content_settings_type, | 267 ContentSettingsType content_settings_type, |
| 269 content::RenderFrameHost* render_frame_host, | 268 content::RenderFrameHost* render_frame_host, |
| 270 const GURL& requesting_origin, | 269 const GURL& requesting_origin, |
| 271 bool user_gesture, | 270 bool user_gesture, |
| 272 const base::Callback<void(PermissionStatus)>& callback) { | 271 const base::Callback<void(PermissionStatus)>& callback) { |
| 273 // TODO(timloh): We should be operating on ContentSettingsType instead of | |
| 274 // converting these back to PermissionType. | |
| 275 PermissionType permission_type; | |
| 276 bool success = PermissionUtil::GetPermissionType(content_settings_type, | |
| 277 &permission_type); | |
| 278 DCHECK(success); | |
| 279 return RequestPermissions( | 272 return RequestPermissions( |
| 280 std::vector<PermissionType>(1, permission_type), render_frame_host, | 273 std::vector<ContentSettingsType>(1, content_settings_type), |
| 281 requesting_origin, user_gesture, | 274 render_frame_host, requesting_origin, user_gesture, |
| 282 base::Bind(&PermissionRequestResponseCallbackWrapper, callback)); | 275 base::Bind(&PermissionRequestResponseCallbackWrapper, callback)); |
| 283 } | 276 } |
| 284 | 277 |
| 285 PermissionStatus PermissionManager::GetPermissionStatus( | 278 PermissionStatus PermissionManager::GetPermissionStatus( |
| 286 ContentSettingsType permission, | 279 ContentSettingsType permission, |
| 287 const GURL& requesting_origin, | 280 const GURL& requesting_origin, |
| 288 const GURL& embedding_origin) { | 281 const GURL& embedding_origin) { |
| 282 if (IsConstantPermission(permission)) | |
| 283 return GetPermissionStatusForConstantPermission(permission); | |
| 289 PermissionContextBase* context = GetPermissionContext(permission); | 284 PermissionContextBase* context = GetPermissionContext(permission); |
| 290 return ContentSettingToPermissionStatus( | 285 return ContentSettingToPermissionStatus( |
| 291 context->GetPermissionStatus(requesting_origin.GetOrigin(), | 286 context->GetPermissionStatus(requesting_origin.GetOrigin(), |
| 292 embedding_origin.GetOrigin()) | 287 embedding_origin.GetOrigin()) |
| 293 .content_setting); | 288 .content_setting); |
| 294 } | 289 } |
| 295 | 290 |
| 296 int PermissionManager::RequestPermission( | 291 int PermissionManager::RequestPermission( |
| 297 PermissionType permission, | 292 PermissionType permission, |
| 298 content::RenderFrameHost* render_frame_host, | 293 content::RenderFrameHost* render_frame_host, |
| 299 const GURL& requesting_origin, | 294 const GURL& requesting_origin, |
| 300 bool user_gesture, | 295 bool user_gesture, |
| 301 const base::Callback<void(PermissionStatus)>& callback) { | 296 const base::Callback<void(PermissionStatus)>& callback) { |
| 297 ContentSettingsType content_settings_type = | |
| 298 PermissionTypeToContentSetting(permission); | |
| 302 return RequestPermissions( | 299 return RequestPermissions( |
| 303 std::vector<PermissionType>(1, permission), | 300 std::vector<ContentSettingsType>(1, content_settings_type), |
| 304 render_frame_host, | 301 render_frame_host, requesting_origin, user_gesture, |
| 305 requesting_origin, | |
| 306 user_gesture, | |
| 307 base::Bind(&PermissionRequestResponseCallbackWrapper, callback)); | 302 base::Bind(&PermissionRequestResponseCallbackWrapper, callback)); |
| 308 } | 303 } |
| 309 | 304 |
| 310 int PermissionManager::RequestPermissions( | 305 int PermissionManager::RequestPermissions( |
| 311 const std::vector<PermissionType>& permissions, | 306 const std::vector<PermissionType>& permissions, |
| 312 content::RenderFrameHost* render_frame_host, | 307 content::RenderFrameHost* render_frame_host, |
| 313 const GURL& requesting_origin, | 308 const GURL& requesting_origin, |
| 314 bool user_gesture, | 309 bool user_gesture, |
| 315 const base::Callback<void( | 310 const base::Callback<void( |
| 316 const std::vector<PermissionStatus>&)>& callback) { | 311 const std::vector<PermissionStatus>&)>& callback) { |
| 312 std::vector<ContentSettingsType> content_settings_types; | |
| 313 std::transform(permissions.begin(), permissions.end(), | |
| 314 back_inserter(content_settings_types), | |
| 315 PermissionTypeToContentSetting); | |
| 316 return RequestPermissions(content_settings_types, render_frame_host, | |
| 317 requesting_origin, user_gesture, callback); | |
| 318 } | |
| 319 | |
| 320 int PermissionManager::RequestPermissions( | |
| 321 const std::vector<ContentSettingsType>& permissions, | |
| 322 content::RenderFrameHost* render_frame_host, | |
| 323 const GURL& requesting_origin, | |
| 324 bool user_gesture, | |
| 325 const base::Callback<void(const std::vector<PermissionStatus>&)>& | |
| 326 callback) { | |
| 317 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 327 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 318 if (permissions.empty()) { | 328 if (permissions.empty()) { |
| 319 callback.Run(std::vector<PermissionStatus>()); | 329 callback.Run(std::vector<PermissionStatus>()); |
| 320 return kNoPendingOperation; | 330 return kNoPendingOperation; |
| 321 } | 331 } |
| 322 | 332 |
| 323 content::WebContents* web_contents = | 333 content::WebContents* web_contents = |
| 324 content::WebContents::FromRenderFrameHost(render_frame_host); | 334 content::WebContents::FromRenderFrameHost(render_frame_host); |
| 325 GURL embedding_origin = web_contents->GetLastCommittedURL().GetOrigin(); | 335 GURL embedding_origin = web_contents->GetLastCommittedURL().GetOrigin(); |
| 326 | 336 |
| 327 int request_id = pending_requests_.Add(base::MakeUnique<PendingRequest>( | 337 int request_id = pending_requests_.Add(base::MakeUnique<PendingRequest>( |
| 328 render_frame_host, permissions, callback)); | 338 render_frame_host, permissions, callback)); |
| 329 | 339 |
| 330 const PermissionRequestID request(render_frame_host, request_id); | 340 const PermissionRequestID request(render_frame_host, request_id); |
| 331 | 341 |
| 332 for (size_t i = 0; i < permissions.size(); ++i) { | 342 for (size_t i = 0; i < permissions.size(); ++i) { |
| 333 const PermissionType permission = permissions[i]; | 343 const ContentSettingsType permission = permissions[i]; |
| 334 | 344 |
| 335 if (IsConstantPermission(permission) || | 345 if (IsConstantPermission(permission) || !GetPermissionContext(permission)) { |
| 336 !GetPermissionContext(PermissionTypeToContentSetting(permission))) { | |
| 337 // Track permission request usages even for constant permissions. | 346 // Track permission request usages even for constant permissions. |
| 338 PermissionUmaUtil::PermissionRequested(permission, requesting_origin, | 347 PermissionUmaUtil::PermissionRequested(permission, requesting_origin, |
| 339 embedding_origin, profile_); | 348 embedding_origin, profile_); |
| 340 OnPermissionsRequestResponseStatus( | 349 OnPermissionsRequestResponseStatus( |
| 341 request_id, i, GetPermissionStatusForConstantPermission(permission)); | 350 request_id, i, GetPermissionStatusForConstantPermission(permission)); |
| 342 continue; | 351 continue; |
| 343 } | 352 } |
| 344 | 353 |
| 345 PermissionContextBase* context = | 354 PermissionContextBase* context = GetPermissionContext(permission); |
| 346 GetPermissionContext(PermissionTypeToContentSetting(permission)); | |
| 347 context->RequestPermission( | 355 context->RequestPermission( |
| 348 web_contents, request, requesting_origin, user_gesture, | 356 web_contents, request, requesting_origin, user_gesture, |
| 349 base::Bind(&ContentSettingToPermissionStatusCallbackWrapper, | 357 base::Bind(&ContentSettingToPermissionStatusCallbackWrapper, |
| 350 base::Bind(&PermissionManager::OnPermissionsRequestResponseStatus, | 358 base::Bind(&PermissionManager::OnPermissionsRequestResponseStatus, |
| 351 weak_ptr_factory_.GetWeakPtr(), request_id, i))); | 359 weak_ptr_factory_.GetWeakPtr(), request_id, i))); |
| 352 } | 360 } |
| 353 | 361 |
| 354 // The request might have been resolved already. | 362 // The request might have been resolved already. |
| 355 if (!pending_requests_.Lookup(request_id)) | 363 if (!pending_requests_.Lookup(request_id)) |
| 356 return kNoPendingOperation; | 364 return kNoPendingOperation; |
| 357 | 365 |
| 358 return request_id; | 366 return request_id; |
| 359 } | 367 } |
|
raymes
2017/02/23 05:37:08
nit: I think the order of these functions is all o
Timothy Loh
2017/02/23 06:17:10
As discussed, I've made this new function public a
| |
| 360 | 368 |
| 361 PermissionContextBase* PermissionManager::GetPermissionContext( | 369 PermissionContextBase* PermissionManager::GetPermissionContext( |
| 362 ContentSettingsType type) { | 370 ContentSettingsType type) { |
| 363 const auto& it = permission_contexts_.find(type); | 371 const auto& it = permission_contexts_.find(type); |
| 364 return it == permission_contexts_.end() ? nullptr : it->second.get(); | 372 return it == permission_contexts_.end() ? nullptr : it->second.get(); |
| 365 } | 373 } |
| 366 | 374 |
| 367 void PermissionManager::OnPermissionsRequestResponseStatus( | 375 void PermissionManager::OnPermissionsRequestResponseStatus( |
| 368 int request_id, | 376 int request_id, |
| 369 int permission_id, | 377 int permission_id, |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 384 if (!pending_request) | 392 if (!pending_request) |
| 385 return; | 393 return; |
| 386 | 394 |
| 387 content::WebContents* web_contents = tab_util::GetWebContentsByFrameID( | 395 content::WebContents* web_contents = tab_util::GetWebContentsByFrameID( |
| 388 pending_request->render_process_id(), pending_request->render_frame_id()); | 396 pending_request->render_process_id(), pending_request->render_frame_id()); |
| 389 DCHECK(web_contents); | 397 DCHECK(web_contents); |
| 390 | 398 |
| 391 const PermissionRequestID request(pending_request->render_process_id(), | 399 const PermissionRequestID request(pending_request->render_process_id(), |
| 392 pending_request->render_frame_id(), | 400 pending_request->render_frame_id(), |
| 393 request_id); | 401 request_id); |
| 394 for (PermissionType permission : pending_request->permissions()) { | 402 for (ContentSettingsType permission : pending_request->permissions()) { |
| 395 PermissionContextBase* context = | 403 PermissionContextBase* context = GetPermissionContext(permission); |
| 396 GetPermissionContext(PermissionTypeToContentSetting(permission)); | |
| 397 if (!context) | 404 if (!context) |
| 398 continue; | 405 continue; |
| 399 context->CancelPermissionRequest(web_contents, request); | 406 context->CancelPermissionRequest(web_contents, request); |
| 400 } | 407 } |
| 401 pending_requests_.Remove(request_id); | 408 pending_requests_.Remove(request_id); |
| 402 } | 409 } |
| 403 | 410 |
| 404 void PermissionManager::ResetPermission(PermissionType permission, | 411 void PermissionManager::ResetPermission(PermissionType permission, |
| 405 const GURL& requesting_origin, | 412 const GURL& requesting_origin, |
| 406 const GURL& embedding_origin) { | 413 const GURL& embedding_origin) { |
| 407 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 414 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 408 PermissionContextBase* context = | 415 PermissionContextBase* context = |
| 409 GetPermissionContext(PermissionTypeToContentSetting(permission)); | 416 GetPermissionContext(PermissionTypeToContentSetting(permission)); |
| 410 if (!context) | 417 if (!context) |
| 411 return; | 418 return; |
| 412 | 419 |
| 413 context->ResetPermission(requesting_origin.GetOrigin(), | 420 context->ResetPermission(requesting_origin.GetOrigin(), |
| 414 embedding_origin.GetOrigin()); | 421 embedding_origin.GetOrigin()); |
| 415 } | 422 } |
| 416 | 423 |
| 417 PermissionStatus PermissionManager::GetPermissionStatus( | 424 PermissionStatus PermissionManager::GetPermissionStatus( |
| 418 PermissionType permission, | 425 PermissionType permission, |
| 419 const GURL& requesting_origin, | 426 const GURL& requesting_origin, |
| 420 const GURL& embedding_origin) { | 427 const GURL& embedding_origin) { |
| 421 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 428 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 422 if (IsConstantPermission(permission)) | |
| 423 return GetPermissionStatusForConstantPermission(permission); | |
| 424 return GetPermissionStatus(PermissionTypeToContentSetting(permission), | 429 return GetPermissionStatus(PermissionTypeToContentSetting(permission), |
| 425 requesting_origin, embedding_origin); | 430 requesting_origin, embedding_origin); |
| 426 } | 431 } |
| 427 | 432 |
| 428 int PermissionManager::SubscribePermissionStatusChange( | 433 int PermissionManager::SubscribePermissionStatusChange( |
| 429 PermissionType permission, | 434 PermissionType permission, |
| 430 const GURL& requesting_origin, | 435 const GURL& requesting_origin, |
| 431 const GURL& embedding_origin, | 436 const GURL& embedding_origin, |
| 432 const base::Callback<void(PermissionStatus)>& callback) { | 437 const base::Callback<void(PermissionStatus)>& callback) { |
| 433 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 438 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 434 if (subscriptions_.IsEmpty()) | 439 if (subscriptions_.IsEmpty()) |
| 435 HostContentSettingsMapFactory::GetForProfile(profile_)->AddObserver(this); | 440 HostContentSettingsMapFactory::GetForProfile(profile_)->AddObserver(this); |
| 436 | 441 |
| 442 ContentSettingsType content_type = PermissionTypeToContentSetting(permission); | |
| 437 auto subscription = base::MakeUnique<Subscription>(); | 443 auto subscription = base::MakeUnique<Subscription>(); |
| 438 subscription->permission = permission; | 444 subscription->permission = content_type; |
| 439 subscription->requesting_origin = requesting_origin; | 445 subscription->requesting_origin = requesting_origin; |
| 440 subscription->embedding_origin = embedding_origin; | 446 subscription->embedding_origin = embedding_origin; |
| 441 subscription->callback = callback; | 447 subscription->callback = callback; |
| 442 | 448 |
| 443 subscription->current_value = | 449 subscription->current_value = |
| 444 GetPermissionStatus(permission, requesting_origin, embedding_origin); | 450 GetPermissionStatus(content_type, requesting_origin, embedding_origin); |
| 445 | 451 |
| 446 return subscriptions_.Add(std::move(subscription)); | 452 return subscriptions_.Add(std::move(subscription)); |
| 447 } | 453 } |
| 448 | 454 |
| 449 void PermissionManager::UnsubscribePermissionStatusChange(int subscription_id) { | 455 void PermissionManager::UnsubscribePermissionStatusChange(int subscription_id) { |
| 450 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 456 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 451 // Whether |subscription_id| is known will be checked by the Remove() call. | 457 // Whether |subscription_id| is known will be checked by the Remove() call. |
| 452 subscriptions_.Remove(subscription_id); | 458 subscriptions_.Remove(subscription_id); |
| 453 | 459 |
| 454 if (subscriptions_.IsEmpty()) | 460 if (subscriptions_.IsEmpty()) |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 468 ContentSettingsType content_type, | 474 ContentSettingsType content_type, |
| 469 std::string resource_identifier) { | 475 std::string resource_identifier) { |
| 470 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 476 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 471 std::list<base::Closure> callbacks; | 477 std::list<base::Closure> callbacks; |
| 472 | 478 |
| 473 for (SubscriptionsMap::iterator iter(&subscriptions_); | 479 for (SubscriptionsMap::iterator iter(&subscriptions_); |
| 474 !iter.IsAtEnd(); iter.Advance()) { | 480 !iter.IsAtEnd(); iter.Advance()) { |
| 475 Subscription* subscription = iter.GetCurrentValue(); | 481 Subscription* subscription = iter.GetCurrentValue(); |
| 476 if (IsConstantPermission(subscription->permission)) | 482 if (IsConstantPermission(subscription->permission)) |
| 477 continue; | 483 continue; |
| 478 if (PermissionTypeToContentSetting(subscription->permission) != | 484 if (subscription->permission != content_type) |
| 479 content_type) { | |
| 480 continue; | 485 continue; |
| 481 } | |
| 482 | 486 |
| 483 if (primary_pattern.IsValid() && | 487 if (primary_pattern.IsValid() && |
| 484 !primary_pattern.Matches(subscription->requesting_origin)) | 488 !primary_pattern.Matches(subscription->requesting_origin)) |
| 485 continue; | 489 continue; |
| 486 if (secondary_pattern.IsValid() && | 490 if (secondary_pattern.IsValid() && |
| 487 !secondary_pattern.Matches(subscription->embedding_origin)) | 491 !secondary_pattern.Matches(subscription->embedding_origin)) |
| 488 continue; | 492 continue; |
| 489 | 493 |
| 490 PermissionStatus new_value = GetPermissionStatus( | 494 PermissionStatus new_value = GetPermissionStatus( |
| 491 subscription->permission, subscription->requesting_origin, | 495 subscription->permission, subscription->requesting_origin, |
| 492 subscription->embedding_origin); | 496 subscription->embedding_origin); |
| 493 if (subscription->current_value == new_value) | 497 if (subscription->current_value == new_value) |
| 494 continue; | 498 continue; |
| 495 | 499 |
| 496 subscription->current_value = new_value; | 500 subscription->current_value = new_value; |
| 497 | 501 |
| 498 // Add the callback to |callbacks| which will be run after the loop to | 502 // Add the callback to |callbacks| which will be run after the loop to |
| 499 // prevent re-entrance issues. | 503 // prevent re-entrance issues. |
| 500 callbacks.push_back(base::Bind(subscription->callback, new_value)); | 504 callbacks.push_back(base::Bind(subscription->callback, new_value)); |
| 501 } | 505 } |
| 502 | 506 |
| 503 for (const auto& callback : callbacks) | 507 for (const auto& callback : callbacks) |
| 504 callback.Run(); | 508 callback.Run(); |
| 505 } | 509 } |
| OLD | NEW |