OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/permissions/permission_manager.h" |
| 6 |
| 7 #include "base/callback.h" |
| 8 #include "chrome/browser/content_settings/permission_context_base.h" |
| 9 #include "chrome/browser/permissions/permission_context.h" |
| 10 #include "chrome/browser/profiles/profile.h" |
| 11 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 12 #include "components/content_settings/core/common/permission_request_id.h" |
| 13 #include "content/public/browser/permission_type.h" |
| 14 #include "content/public/browser/render_process_host.h" |
| 15 #include "content/public/browser/render_view_host.h" |
| 16 #include "content/public/browser/web_contents.h" |
| 17 |
| 18 namespace { |
| 19 |
| 20 // Helper method to convert ContentSetting to content::PermissionStatus. |
| 21 content::PermissionStatus |
| 22 ContentSettingToPermissionStatus(ContentSetting setting) { |
| 23 switch (setting) { |
| 24 case CONTENT_SETTING_ALLOW: |
| 25 case CONTENT_SETTING_SESSION_ONLY: |
| 26 return content::PERMISSION_STATUS_GRANTED; |
| 27 case CONTENT_SETTING_BLOCK: |
| 28 return content::PERMISSION_STATUS_DENIED; |
| 29 case CONTENT_SETTING_ASK: |
| 30 return content::PERMISSION_STATUS_ASK; |
| 31 case CONTENT_SETTING_DETECT_IMPORTANT_CONTENT: |
| 32 case CONTENT_SETTING_DEFAULT: |
| 33 case CONTENT_SETTING_NUM_SETTINGS: |
| 34 break; |
| 35 } |
| 36 |
| 37 NOTREACHED(); |
| 38 return content::PERMISSION_STATUS_DENIED; |
| 39 } |
| 40 |
| 41 // Helper method to convert content::PermissionType to ContentSettingType. |
| 42 ContentSettingsType PermissionTypeToContentSetting( |
| 43 content::PermissionType permission) { |
| 44 switch (permission) { |
| 45 case content::PermissionType::MIDI_SYSEX: |
| 46 return CONTENT_SETTINGS_TYPE_MIDI_SYSEX; |
| 47 case content::PermissionType::PUSH_MESSAGING: |
| 48 return CONTENT_SETTINGS_TYPE_PUSH_MESSAGING; |
| 49 case content::PermissionType::NOTIFICATIONS: |
| 50 return CONTENT_SETTINGS_TYPE_NOTIFICATIONS; |
| 51 case content::PermissionType::GEOLOCATION: |
| 52 return CONTENT_SETTINGS_TYPE_GEOLOCATION; |
| 53 case content::PermissionType::PROTECTED_MEDIA_IDENTIFIER: |
| 54 #if defined(OS_ANDROID) || defined(OS_CHROMEOS) |
| 55 return CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER; |
| 56 #else |
| 57 NOTIMPLEMENTED(); |
| 58 break; |
| 59 #endif |
| 60 case content::PermissionType::NUM: |
| 61 // This will hit the NOTREACHED below. |
| 62 break; |
| 63 } |
| 64 |
| 65 NOTREACHED() << "Unknown content setting for permission " |
| 66 << static_cast<int>(permission); |
| 67 return CONTENT_SETTINGS_TYPE_DEFAULT; |
| 68 } |
| 69 |
| 70 // Helper method that wraps a callback a void(content::PermissionStatus) |
| 71 // callback into a void(ContentSetting) callback. |
| 72 void PermissionStatusCallbackWrapper( |
| 73 const base::Callback<void(content::PermissionStatus)>& callback, |
| 74 ContentSetting content_setting) { |
| 75 callback.Run(ContentSettingToPermissionStatus(content_setting)); |
| 76 } |
| 77 |
| 78 } // anonymous namespace |
| 79 |
| 80 PermissionManager::PermissionManager(Profile* profile) |
| 81 : profile_(profile) { |
| 82 } |
| 83 |
| 84 PermissionManager::~PermissionManager() { |
| 85 } |
| 86 |
| 87 void PermissionManager::RequestPermission( |
| 88 content::PermissionType permission, |
| 89 content::WebContents* web_contents, |
| 90 int request_id, |
| 91 const GURL& requesting_origin, |
| 92 bool user_gesture, |
| 93 const base::Callback<void(content::PermissionStatus)>& callback) { |
| 94 PermissionContextBase* context = PermissionContext::Get(profile_, permission); |
| 95 if (!context) { |
| 96 callback.Run(content::PERMISSION_STATUS_DENIED); |
| 97 return; |
| 98 } |
| 99 |
| 100 int render_process_id = web_contents->GetRenderProcessHost()->GetID(); |
| 101 int render_view_id = web_contents->GetRenderViewHost()->GetRoutingID(); |
| 102 const PermissionRequestID request(render_process_id, |
| 103 render_view_id, |
| 104 request_id, |
| 105 requesting_origin); |
| 106 |
| 107 context->RequestPermission(web_contents, request, requesting_origin, |
| 108 user_gesture, |
| 109 base::Bind(&PermissionStatusCallbackWrapper, |
| 110 callback)); |
| 111 } |
| 112 |
| 113 void PermissionManager::CancelPermissionRequest( |
| 114 content::PermissionType permission, |
| 115 content::WebContents* web_contents, |
| 116 int request_id, |
| 117 const GURL& requesting_origin) { |
| 118 PermissionContextBase* context = PermissionContext::Get(profile_, permission); |
| 119 if (!context) |
| 120 return; |
| 121 |
| 122 int render_process_id = web_contents->GetRenderProcessHost()->GetID(); |
| 123 int render_view_id = web_contents->GetRenderViewHost()->GetRoutingID(); |
| 124 const PermissionRequestID request(render_process_id, |
| 125 render_view_id, |
| 126 request_id, |
| 127 requesting_origin); |
| 128 |
| 129 context->CancelPermissionRequest(web_contents, request); |
| 130 } |
| 131 |
| 132 void PermissionManager::ResetPermission( |
| 133 content::PermissionType permission, |
| 134 const GURL& requesting_origin, |
| 135 const GURL& embedding_origin) { |
| 136 PermissionContextBase* context = PermissionContext::Get(profile_, permission); |
| 137 if (!context) |
| 138 return; |
| 139 |
| 140 context->ResetPermission(requesting_origin.GetOrigin(), |
| 141 embedding_origin.GetOrigin()); |
| 142 } |
| 143 |
| 144 content::PermissionStatus PermissionManager::GetPermissionStatus( |
| 145 content::PermissionType permission, |
| 146 const GURL& requesting_origin, |
| 147 const GURL& embedding_origin) { |
| 148 PermissionContextBase* context = PermissionContext::Get(profile_, permission); |
| 149 if (!context) |
| 150 return content::PERMISSION_STATUS_DENIED; |
| 151 |
| 152 return ContentSettingToPermissionStatus( |
| 153 context->GetPermissionStatus(requesting_origin.GetOrigin(), |
| 154 embedding_origin.GetOrigin())); |
| 155 } |
| 156 |
| 157 void PermissionManager::RegisterPermissionUsage( |
| 158 content::PermissionType permission, |
| 159 const GURL& requesting_origin, |
| 160 const GURL& embedding_origin) { |
| 161 profile_->GetHostContentSettingsMap()->UpdateLastUsage( |
| 162 requesting_origin, |
| 163 embedding_origin, |
| 164 PermissionTypeToContentSetting(permission)); |
| 165 } |
OLD | NEW |