| 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/tab_contents/tab_util.h" |
| 14 #include "components/content_settings/core/browser/host_content_settings_map.h" | 14 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 15 #include "content/public/browser/permission_type.h" | 15 #include "content/public/browser/permission_type.h" |
| 16 #include "content/public/browser/render_frame_host.h" | 16 #include "content/public/browser/render_frame_host.h" |
| 17 #include "content/public/browser/render_process_host.h" | 17 #include "content/public/browser/render_process_host.h" |
| 18 #include "content/public/browser/web_contents.h" | 18 #include "content/public/browser/web_contents.h" |
| 19 | 19 |
| 20 #if !defined(OS_ANDROID) | 20 #if !defined(OS_ANDROID) |
| 21 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h" | 21 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h" |
| 22 #endif | 22 #endif |
| 23 | 23 |
| 24 using content::PermissionStatus; | |
| 25 using content::PermissionType; | 24 using content::PermissionType; |
| 26 | 25 |
| 27 namespace { | 26 namespace { |
| 28 | 27 |
| 29 // Helper method to convert ContentSetting to PermissionStatus. | 28 // Helper method to convert ContentSetting to permission::Status. |
| 30 PermissionStatus ContentSettingToPermissionStatus(ContentSetting setting) { | 29 permission::Status ContentSettingToPermissionStatus(ContentSetting setting) { |
| 31 switch (setting) { | 30 switch (setting) { |
| 32 case CONTENT_SETTING_ALLOW: | 31 case CONTENT_SETTING_ALLOW: |
| 33 case CONTENT_SETTING_SESSION_ONLY: | 32 case CONTENT_SETTING_SESSION_ONLY: |
| 34 return content::PERMISSION_STATUS_GRANTED; | 33 return permission::STATUS_GRANTED; |
| 35 case CONTENT_SETTING_BLOCK: | 34 case CONTENT_SETTING_BLOCK: |
| 36 return content::PERMISSION_STATUS_DENIED; | 35 return permission::STATUS_DENIED; |
| 37 case CONTENT_SETTING_ASK: | 36 case CONTENT_SETTING_ASK: |
| 38 return content::PERMISSION_STATUS_ASK; | 37 return permission::STATUS_ASK; |
| 39 case CONTENT_SETTING_DETECT_IMPORTANT_CONTENT: | 38 case CONTENT_SETTING_DETECT_IMPORTANT_CONTENT: |
| 40 case CONTENT_SETTING_DEFAULT: | 39 case CONTENT_SETTING_DEFAULT: |
| 41 case CONTENT_SETTING_NUM_SETTINGS: | 40 case CONTENT_SETTING_NUM_SETTINGS: |
| 42 break; | 41 break; |
| 43 } | 42 } |
| 44 | 43 |
| 45 NOTREACHED(); | 44 NOTREACHED(); |
| 46 return content::PERMISSION_STATUS_DENIED; | 45 return permission::STATUS_DENIED; |
| 47 } | 46 } |
| 48 | 47 |
| 49 // Helper method to convert PermissionType to ContentSettingType. | 48 // Helper method to convert PermissionType to ContentSettingType. |
| 50 ContentSettingsType PermissionTypeToContentSetting(PermissionType permission) { | 49 ContentSettingsType PermissionTypeToContentSetting(PermissionType permission) { |
| 51 switch (permission) { | 50 switch (permission) { |
| 52 case PermissionType::MIDI_SYSEX: | 51 case PermissionType::MIDI_SYSEX: |
| 53 return CONTENT_SETTINGS_TYPE_MIDI_SYSEX; | 52 return CONTENT_SETTINGS_TYPE_MIDI_SYSEX; |
| 54 case PermissionType::PUSH_MESSAGING: | 53 case PermissionType::PUSH_MESSAGING: |
| 55 return CONTENT_SETTINGS_TYPE_PUSH_MESSAGING; | 54 return CONTENT_SETTINGS_TYPE_PUSH_MESSAGING; |
| 56 case PermissionType::NOTIFICATIONS: | 55 case PermissionType::NOTIFICATIONS: |
| (...skipping 19 matching lines...) Expand all Loading... |
| 76 case PermissionType::NUM: | 75 case PermissionType::NUM: |
| 77 // This will hit the NOTREACHED below. | 76 // This will hit the NOTREACHED below. |
| 78 break; | 77 break; |
| 79 } | 78 } |
| 80 | 79 |
| 81 NOTREACHED() << "Unknown content setting for permission " | 80 NOTREACHED() << "Unknown content setting for permission " |
| 82 << static_cast<int>(permission); | 81 << static_cast<int>(permission); |
| 83 return CONTENT_SETTINGS_TYPE_DEFAULT; | 82 return CONTENT_SETTINGS_TYPE_DEFAULT; |
| 84 } | 83 } |
| 85 | 84 |
| 86 // Returns whether the permission has a constant PermissionStatus value (i.e. | 85 // Returns whether the permission has a constant permission::Status value (i.e. |
| 87 // always approved or always denied) | 86 // always approved or always denied) |
| 88 // The PermissionTypes for which true is returned should be exactly those which | 87 // The PermissionTypes for which true is returned should be exactly those which |
| 89 // return nullptr in PermissionContext::Get since they don't have a context. | 88 // return nullptr in PermissionContext::Get since they don't have a context. |
| 90 bool IsConstantPermission(PermissionType type) { | 89 bool IsConstantPermission(PermissionType type) { |
| 91 switch (type) { | 90 switch (type) { |
| 92 case PermissionType::MIDI: | 91 case PermissionType::MIDI: |
| 93 return true; | 92 return true; |
| 94 default: | 93 default: |
| 95 return false; | 94 return false; |
| 96 } | 95 } |
| 97 } | 96 } |
| 98 | 97 |
| 99 // Function used for handling permission types which do not change their | 98 // Function used for handling permission types which do not change their |
| 100 // value i.e. they are always approved or always denied etc. | 99 // value i.e. they are always approved or always denied etc. |
| 101 // CONTENT_SETTING_DEFAULT is returned if the permission needs further handling. | 100 // CONTENT_SETTING_DEFAULT is returned if the permission needs further handling. |
| 102 // This function should only be called when IsConstantPermission has returned | 101 // This function should only be called when IsConstantPermission has returned |
| 103 // true for the PermissionType. | 102 // true for the PermissionType. |
| 104 ContentSetting GetContentSettingForConstantPermission(PermissionType type) { | 103 ContentSetting GetContentSettingForConstantPermission(PermissionType type) { |
| 105 DCHECK(IsConstantPermission(type)); | 104 DCHECK(IsConstantPermission(type)); |
| 106 switch (type) { | 105 switch (type) { |
| 107 case PermissionType::MIDI: | 106 case PermissionType::MIDI: |
| 108 return CONTENT_SETTING_ALLOW; | 107 return CONTENT_SETTING_ALLOW; |
| 109 default: | 108 default: |
| 110 return CONTENT_SETTING_DEFAULT; | 109 return CONTENT_SETTING_DEFAULT; |
| 111 } | 110 } |
| 112 } | 111 } |
| 113 | 112 |
| 114 PermissionStatus GetPermissionStatusForConstantPermission(PermissionType type) { | 113 permission::Status GetPermissionStatusForConstantPermission( |
| 114 PermissionType type) { |
| 115 return ContentSettingToPermissionStatus( | 115 return ContentSettingToPermissionStatus( |
| 116 GetContentSettingForConstantPermission(type)); | 116 GetContentSettingForConstantPermission(type)); |
| 117 } | 117 } |
| 118 | 118 |
| 119 } // anonymous namespace | 119 } // anonymous namespace |
| 120 | 120 |
| 121 struct PermissionManager::PendingRequest { | 121 struct PermissionManager::PendingRequest { |
| 122 PendingRequest(PermissionType permission, | 122 PendingRequest(PermissionType permission, |
| 123 content::RenderFrameHost* render_frame_host) | 123 content::RenderFrameHost* render_frame_host) |
| 124 : permission(permission), | 124 : permission(permission), |
| 125 render_process_id(render_frame_host->GetProcess()->GetID()), | 125 render_process_id(render_frame_host->GetProcess()->GetID()), |
| 126 render_frame_id(render_frame_host->GetRoutingID()) { | 126 render_frame_id(render_frame_host->GetRoutingID()) { |
| 127 } | 127 } |
| 128 | 128 |
| 129 PermissionType permission; | 129 PermissionType permission; |
| 130 int render_process_id; | 130 int render_process_id; |
| 131 int render_frame_id; | 131 int render_frame_id; |
| 132 }; | 132 }; |
| 133 | 133 |
| 134 struct PermissionManager::Subscription { | 134 struct PermissionManager::Subscription { |
| 135 PermissionType permission; | 135 PermissionType permission; |
| 136 GURL requesting_origin; | 136 GURL requesting_origin; |
| 137 GURL embedding_origin; | 137 GURL embedding_origin; |
| 138 base::Callback<void(PermissionStatus)> callback; | 138 base::Callback<void(permission::Status)> callback; |
| 139 ContentSetting current_value; | 139 ContentSetting current_value; |
| 140 }; | 140 }; |
| 141 | 141 |
| 142 PermissionManager::PermissionManager(Profile* profile) | 142 PermissionManager::PermissionManager(Profile* profile) |
| 143 : profile_(profile), | 143 : profile_(profile), |
| 144 weak_ptr_factory_(this) { | 144 weak_ptr_factory_(this) { |
| 145 } | 145 } |
| 146 | 146 |
| 147 PermissionManager::~PermissionManager() { | 147 PermissionManager::~PermissionManager() { |
| 148 if (!subscriptions_.IsEmpty()) | 148 if (!subscriptions_.IsEmpty()) |
| 149 HostContentSettingsMapFactory::GetForProfile(profile_) | 149 HostContentSettingsMapFactory::GetForProfile(profile_) |
| 150 ->RemoveObserver(this); | 150 ->RemoveObserver(this); |
| 151 } | 151 } |
| 152 | 152 |
| 153 int PermissionManager::RequestPermission( | 153 int PermissionManager::RequestPermission( |
| 154 PermissionType permission, | 154 PermissionType permission, |
| 155 content::RenderFrameHost* render_frame_host, | 155 content::RenderFrameHost* render_frame_host, |
| 156 const GURL& requesting_origin, | 156 const GURL& requesting_origin, |
| 157 bool user_gesture, | 157 bool user_gesture, |
| 158 const base::Callback<void(PermissionStatus)>& callback) { | 158 const base::Callback<void(permission::Status)>& callback) { |
| 159 if (IsConstantPermission(permission)) { | 159 if (IsConstantPermission(permission)) { |
| 160 callback.Run(GetPermissionStatusForConstantPermission(permission)); | 160 callback.Run(GetPermissionStatusForConstantPermission(permission)); |
| 161 return kNoPendingOperation; | 161 return kNoPendingOperation; |
| 162 } | 162 } |
| 163 | 163 |
| 164 PermissionContextBase* context = PermissionContext::Get(profile_, permission); | 164 PermissionContextBase* context = PermissionContext::Get(profile_, permission); |
| 165 if (!context) { | 165 if (!context) { |
| 166 callback.Run(content::PERMISSION_STATUS_DENIED); | 166 callback.Run(permission::STATUS_DENIED); |
| 167 return kNoPendingOperation; | 167 return kNoPendingOperation; |
| 168 } | 168 } |
| 169 | 169 |
| 170 content::WebContents* web_contents = | 170 content::WebContents* web_contents = |
| 171 content::WebContents::FromRenderFrameHost(render_frame_host); | 171 content::WebContents::FromRenderFrameHost(render_frame_host); |
| 172 if (IsPermissionBubbleManagerMissing(web_contents)) { | 172 if (IsPermissionBubbleManagerMissing(web_contents)) { |
| 173 callback.Run( | 173 callback.Run( |
| 174 GetPermissionStatus(permission, requesting_origin, | 174 GetPermissionStatus(permission, requesting_origin, |
| 175 web_contents->GetLastCommittedURL().GetOrigin())); | 175 web_contents->GetLastCommittedURL().GetOrigin())); |
| 176 return kNoPendingOperation; | 176 return kNoPendingOperation; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 187 web_contents, request, requesting_origin, user_gesture, | 187 web_contents, request, requesting_origin, user_gesture, |
| 188 base::Bind(&PermissionManager::OnPermissionRequestResponse, | 188 base::Bind(&PermissionManager::OnPermissionRequestResponse, |
| 189 weak_ptr_factory_.GetWeakPtr(), | 189 weak_ptr_factory_.GetWeakPtr(), |
| 190 request_id, | 190 request_id, |
| 191 callback)); | 191 callback)); |
| 192 return request_id; | 192 return request_id; |
| 193 } | 193 } |
| 194 | 194 |
| 195 void PermissionManager::OnPermissionRequestResponse( | 195 void PermissionManager::OnPermissionRequestResponse( |
| 196 int request_id, | 196 int request_id, |
| 197 const base::Callback<void(PermissionStatus)>& callback, | 197 const base::Callback<void(permission::Status)>& callback, |
| 198 ContentSetting content_setting) { | 198 ContentSetting content_setting) { |
| 199 pending_requests_.Remove(request_id); | 199 pending_requests_.Remove(request_id); |
| 200 callback.Run(ContentSettingToPermissionStatus(content_setting)); | 200 callback.Run(ContentSettingToPermissionStatus(content_setting)); |
| 201 } | 201 } |
| 202 | 202 |
| 203 void PermissionManager::CancelPermissionRequest(int request_id) { | 203 void PermissionManager::CancelPermissionRequest(int request_id) { |
| 204 PendingRequest* pending_request = pending_requests_.Lookup(request_id); | 204 PendingRequest* pending_request = pending_requests_.Lookup(request_id); |
| 205 if (!pending_request) | 205 if (!pending_request) |
| 206 return; | 206 return; |
| 207 | 207 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 229 const GURL& requesting_origin, | 229 const GURL& requesting_origin, |
| 230 const GURL& embedding_origin) { | 230 const GURL& embedding_origin) { |
| 231 PermissionContextBase* context = PermissionContext::Get(profile_, permission); | 231 PermissionContextBase* context = PermissionContext::Get(profile_, permission); |
| 232 if (!context) | 232 if (!context) |
| 233 return; | 233 return; |
| 234 | 234 |
| 235 context->ResetPermission(requesting_origin.GetOrigin(), | 235 context->ResetPermission(requesting_origin.GetOrigin(), |
| 236 embedding_origin.GetOrigin()); | 236 embedding_origin.GetOrigin()); |
| 237 } | 237 } |
| 238 | 238 |
| 239 PermissionStatus PermissionManager::GetPermissionStatus( | 239 permission::Status PermissionManager::GetPermissionStatus( |
| 240 PermissionType permission, | 240 PermissionType permission, |
| 241 const GURL& requesting_origin, | 241 const GURL& requesting_origin, |
| 242 const GURL& embedding_origin) { | 242 const GURL& embedding_origin) { |
| 243 if (IsConstantPermission(permission)) | 243 if (IsConstantPermission(permission)) |
| 244 return GetPermissionStatusForConstantPermission(permission); | 244 return GetPermissionStatusForConstantPermission(permission); |
| 245 | 245 |
| 246 PermissionContextBase* context = PermissionContext::Get(profile_, permission); | 246 PermissionContextBase* context = PermissionContext::Get(profile_, permission); |
| 247 if (!context) | 247 if (!context) |
| 248 return content::PERMISSION_STATUS_DENIED; | 248 return permission::STATUS_DENIED; |
| 249 | 249 |
| 250 return ContentSettingToPermissionStatus(context->GetPermissionStatus( | 250 return ContentSettingToPermissionStatus(context->GetPermissionStatus( |
| 251 requesting_origin.GetOrigin(), embedding_origin.GetOrigin())); | 251 requesting_origin.GetOrigin(), embedding_origin.GetOrigin())); |
| 252 } | 252 } |
| 253 | 253 |
| 254 void PermissionManager::RegisterPermissionUsage(PermissionType permission, | 254 void PermissionManager::RegisterPermissionUsage(PermissionType permission, |
| 255 const GURL& requesting_origin, | 255 const GURL& requesting_origin, |
| 256 const GURL& embedding_origin) { | 256 const GURL& embedding_origin) { |
| 257 // This is required because constant permissions don't have a | 257 // This is required because constant permissions don't have a |
| 258 // ContentSettingsType. | 258 // ContentSettingsType. |
| 259 if (IsConstantPermission(permission)) | 259 if (IsConstantPermission(permission)) |
| 260 return; | 260 return; |
| 261 | 261 |
| 262 HostContentSettingsMapFactory::GetForProfile(profile_)->UpdateLastUsage( | 262 HostContentSettingsMapFactory::GetForProfile(profile_)->UpdateLastUsage( |
| 263 requesting_origin, | 263 requesting_origin, |
| 264 embedding_origin, | 264 embedding_origin, |
| 265 PermissionTypeToContentSetting(permission)); | 265 PermissionTypeToContentSetting(permission)); |
| 266 } | 266 } |
| 267 | 267 |
| 268 int PermissionManager::SubscribePermissionStatusChange( | 268 int PermissionManager::SubscribePermissionStatusChange( |
| 269 PermissionType permission, | 269 PermissionType permission, |
| 270 const GURL& requesting_origin, | 270 const GURL& requesting_origin, |
| 271 const GURL& embedding_origin, | 271 const GURL& embedding_origin, |
| 272 const base::Callback<void(PermissionStatus)>& callback) { | 272 const base::Callback<void(permission::Status)>& callback) { |
| 273 if (subscriptions_.IsEmpty()) | 273 if (subscriptions_.IsEmpty()) |
| 274 HostContentSettingsMapFactory::GetForProfile(profile_)->AddObserver(this); | 274 HostContentSettingsMapFactory::GetForProfile(profile_)->AddObserver(this); |
| 275 | 275 |
| 276 Subscription* subscription = new Subscription(); | 276 Subscription* subscription = new Subscription(); |
| 277 subscription->permission = permission; | 277 subscription->permission = permission; |
| 278 subscription->requesting_origin = requesting_origin; | 278 subscription->requesting_origin = requesting_origin; |
| 279 subscription->embedding_origin = embedding_origin; | 279 subscription->embedding_origin = embedding_origin; |
| 280 subscription->callback = callback; | 280 subscription->callback = callback; |
| 281 | 281 |
| 282 if (IsConstantPermission(permission)) { | 282 if (IsConstantPermission(permission)) { |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 341 // Add the callback to |callbacks| which will be run after the loop to | 341 // Add the callback to |callbacks| which will be run after the loop to |
| 342 // prevent re-entrance issues. | 342 // prevent re-entrance issues. |
| 343 callbacks.push_back( | 343 callbacks.push_back( |
| 344 base::Bind(subscription->callback, | 344 base::Bind(subscription->callback, |
| 345 ContentSettingToPermissionStatus(new_value))); | 345 ContentSettingToPermissionStatus(new_value))); |
| 346 } | 346 } |
| 347 | 347 |
| 348 for (const auto& callback : callbacks) | 348 for (const auto& callback : callbacks) |
| 349 callback.Run(); | 349 callback.Run(); |
| 350 } | 350 } |
| OLD | NEW |