| 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 "content/shell/browser/layout_test/layout_test_permission_manager.h" | 5 #include "content/shell/browser/layout_test/layout_test_permission_manager.h" |
| 6 | 6 |
| 7 #include <list> | 7 #include <list> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| 11 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
| 12 #include "content/public/browser/permission_type.h" | 12 #include "content/public/browser/permission_type.h" |
| 13 #include "content/public/browser/web_contents.h" | 13 #include "content/public/browser/web_contents.h" |
| 14 #include "content/shell/browser/layout_test/layout_test_content_browser_client.h
" | 14 #include "content/shell/browser/layout_test/layout_test_content_browser_client.h
" |
| 15 #include "content/shell/browser/layout_test/layout_test_notification_manager.h" | 15 #include "content/shell/browser/layout_test/layout_test_notification_manager.h" |
| 16 | 16 |
| 17 namespace content { | 17 namespace content { |
| 18 | 18 |
| 19 struct LayoutTestPermissionManager::Subscription { | 19 struct LayoutTestPermissionManager::Subscription { |
| 20 PermissionDescription permission; | 20 PermissionDescription permission; |
| 21 base::Callback<void(content::PermissionStatus)> callback; | 21 base::Callback<void(permission::Status)> callback; |
| 22 PermissionStatus current_value; | 22 permission::Status current_value; |
| 23 }; | 23 }; |
| 24 | 24 |
| 25 LayoutTestPermissionManager::PermissionDescription::PermissionDescription( | 25 LayoutTestPermissionManager::PermissionDescription::PermissionDescription( |
| 26 PermissionType type, | 26 PermissionType type, |
| 27 const GURL& origin, | 27 const GURL& origin, |
| 28 const GURL& embedding_origin) | 28 const GURL& embedding_origin) |
| 29 : type(type), | 29 : type(type), |
| 30 origin(origin), | 30 origin(origin), |
| 31 embedding_origin(embedding_origin) { | 31 embedding_origin(embedding_origin) { |
| 32 } | 32 } |
| (...skipping 26 matching lines...) Expand all Loading... |
| 59 } | 59 } |
| 60 | 60 |
| 61 LayoutTestPermissionManager::~LayoutTestPermissionManager() { | 61 LayoutTestPermissionManager::~LayoutTestPermissionManager() { |
| 62 } | 62 } |
| 63 | 63 |
| 64 int LayoutTestPermissionManager::RequestPermission( | 64 int LayoutTestPermissionManager::RequestPermission( |
| 65 PermissionType permission, | 65 PermissionType permission, |
| 66 RenderFrameHost* render_frame_host, | 66 RenderFrameHost* render_frame_host, |
| 67 const GURL& requesting_origin, | 67 const GURL& requesting_origin, |
| 68 bool user_gesture, | 68 bool user_gesture, |
| 69 const base::Callback<void(PermissionStatus)>& callback) { | 69 const base::Callback<void(permission::Status)>& callback) { |
| 70 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 70 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 71 | 71 |
| 72 callback.Run(GetPermissionStatus( | 72 callback.Run(GetPermissionStatus( |
| 73 permission, requesting_origin, | 73 permission, requesting_origin, |
| 74 WebContents::FromRenderFrameHost(render_frame_host) | 74 WebContents::FromRenderFrameHost(render_frame_host) |
| 75 ->GetLastCommittedURL().GetOrigin())); | 75 ->GetLastCommittedURL().GetOrigin())); |
| 76 return kNoPendingOperation; | 76 return kNoPendingOperation; |
| 77 } | 77 } |
| 78 | 78 |
| 79 void LayoutTestPermissionManager::CancelPermissionRequest(int request_id) { | 79 void LayoutTestPermissionManager::CancelPermissionRequest(int request_id) { |
| 80 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 80 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 81 } | 81 } |
| 82 | 82 |
| 83 void LayoutTestPermissionManager::ResetPermission( | 83 void LayoutTestPermissionManager::ResetPermission( |
| 84 PermissionType permission, | 84 PermissionType permission, |
| 85 const GURL& requesting_origin, | 85 const GURL& requesting_origin, |
| 86 const GURL& embedding_origin) { | 86 const GURL& embedding_origin) { |
| 87 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 87 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 88 | 88 |
| 89 base::AutoLock lock(permissions_lock_); | 89 base::AutoLock lock(permissions_lock_); |
| 90 | 90 |
| 91 auto it = permissions_.find( | 91 auto it = permissions_.find( |
| 92 PermissionDescription(permission, requesting_origin, embedding_origin)); | 92 PermissionDescription(permission, requesting_origin, embedding_origin)); |
| 93 if (it == permissions_.end()) | 93 if (it == permissions_.end()) |
| 94 return; | 94 return; |
| 95 permissions_.erase(it);; | 95 permissions_.erase(it);; |
| 96 } | 96 } |
| 97 | 97 |
| 98 PermissionStatus LayoutTestPermissionManager::GetPermissionStatus( | 98 permission::Status LayoutTestPermissionManager::GetPermissionStatus( |
| 99 PermissionType permission, | 99 PermissionType permission, |
| 100 const GURL& requesting_origin, | 100 const GURL& requesting_origin, |
| 101 const GURL& embedding_origin) { | 101 const GURL& embedding_origin) { |
| 102 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || | 102 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || |
| 103 BrowserThread::CurrentlyOn(BrowserThread::IO)); | 103 BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 104 | 104 |
| 105 base::AutoLock lock(permissions_lock_); | 105 base::AutoLock lock(permissions_lock_); |
| 106 | 106 |
| 107 auto it = permissions_.find( | 107 auto it = permissions_.find( |
| 108 PermissionDescription(permission, requesting_origin, embedding_origin)); | 108 PermissionDescription(permission, requesting_origin, embedding_origin)); |
| 109 if (it == permissions_.end()) | 109 if (it == permissions_.end()) |
| 110 return PERMISSION_STATUS_DENIED; | 110 return permission::STATUS_DENIED; |
| 111 return it->second; | 111 return it->second; |
| 112 } | 112 } |
| 113 | 113 |
| 114 void LayoutTestPermissionManager::RegisterPermissionUsage( | 114 void LayoutTestPermissionManager::RegisterPermissionUsage( |
| 115 PermissionType permission, | 115 PermissionType permission, |
| 116 const GURL& requesting_origin, | 116 const GURL& requesting_origin, |
| 117 const GURL& embedding_origin) { | 117 const GURL& embedding_origin) { |
| 118 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 118 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 119 } | 119 } |
| 120 | 120 |
| 121 int LayoutTestPermissionManager::SubscribePermissionStatusChange( | 121 int LayoutTestPermissionManager::SubscribePermissionStatusChange( |
| 122 PermissionType permission, | 122 PermissionType permission, |
| 123 const GURL& requesting_origin, | 123 const GURL& requesting_origin, |
| 124 const GURL& embedding_origin, | 124 const GURL& embedding_origin, |
| 125 const base::Callback<void(PermissionStatus)>& callback) { | 125 const base::Callback<void(permission::Status)>& callback) { |
| 126 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 126 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 127 | 127 |
| 128 Subscription* subscription = new Subscription(); | 128 Subscription* subscription = new Subscription(); |
| 129 subscription->permission = | 129 subscription->permission = |
| 130 PermissionDescription(permission, requesting_origin, embedding_origin); | 130 PermissionDescription(permission, requesting_origin, embedding_origin); |
| 131 subscription->callback = callback; | 131 subscription->callback = callback; |
| 132 subscription->current_value = | 132 subscription->current_value = |
| 133 GetPermissionStatus(permission, | 133 GetPermissionStatus(permission, |
| 134 subscription->permission.origin, | 134 subscription->permission.origin, |
| 135 subscription->permission.embedding_origin); | 135 subscription->permission.embedding_origin); |
| 136 | 136 |
| 137 return subscriptions_.Add(subscription); | 137 return subscriptions_.Add(subscription); |
| 138 } | 138 } |
| 139 | 139 |
| 140 void LayoutTestPermissionManager::UnsubscribePermissionStatusChange( | 140 void LayoutTestPermissionManager::UnsubscribePermissionStatusChange( |
| 141 int subscription_id) { | 141 int subscription_id) { |
| 142 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 142 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 143 | 143 |
| 144 // Whether |subscription_id| is known will be checked by the Remove() call. | 144 // Whether |subscription_id| is known will be checked by the Remove() call. |
| 145 subscriptions_.Remove(subscription_id); | 145 subscriptions_.Remove(subscription_id); |
| 146 } | 146 } |
| 147 | 147 |
| 148 void LayoutTestPermissionManager::SetPermission(PermissionType permission, | 148 void LayoutTestPermissionManager::SetPermission(PermissionType permission, |
| 149 PermissionStatus status, | 149 permission::Status status, |
| 150 const GURL& origin, | 150 const GURL& origin, |
| 151 const GURL& embedding_origin) { | 151 const GURL& embedding_origin) { |
| 152 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 152 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 153 | 153 |
| 154 PermissionDescription description(permission, origin, embedding_origin); | 154 PermissionDescription description(permission, origin, embedding_origin); |
| 155 | 155 |
| 156 base::AutoLock lock(permissions_lock_); | 156 base::AutoLock lock(permissions_lock_); |
| 157 | 157 |
| 158 auto it = permissions_.find(description); | 158 auto it = permissions_.find(description); |
| 159 if (it == permissions_.end()) { | 159 if (it == permissions_.end()) { |
| 160 permissions_.insert(std::pair<PermissionDescription, PermissionStatus>( | 160 permissions_.insert(std::pair<PermissionDescription, permission::Status>( |
| 161 description, status)); | 161 description, status)); |
| 162 } else { | 162 } else { |
| 163 it->second = status; | 163 it->second = status; |
| 164 } | 164 } |
| 165 | 165 |
| 166 OnPermissionChanged(description, status); | 166 OnPermissionChanged(description, status); |
| 167 } | 167 } |
| 168 | 168 |
| 169 void LayoutTestPermissionManager::ResetPermissions() { | 169 void LayoutTestPermissionManager::ResetPermissions() { |
| 170 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 170 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 171 | 171 |
| 172 base::AutoLock lock(permissions_lock_); | 172 base::AutoLock lock(permissions_lock_); |
| 173 permissions_.clear(); | 173 permissions_.clear(); |
| 174 } | 174 } |
| 175 | 175 |
| 176 void LayoutTestPermissionManager::OnPermissionChanged( | 176 void LayoutTestPermissionManager::OnPermissionChanged( |
| 177 const PermissionDescription& permission, | 177 const PermissionDescription& permission, |
| 178 PermissionStatus status) { | 178 permission::Status status) { |
| 179 std::list<base::Closure> callbacks; | 179 std::list<base::Closure> callbacks; |
| 180 | 180 |
| 181 for (SubscriptionsMap::iterator iter(&subscriptions_); | 181 for (SubscriptionsMap::iterator iter(&subscriptions_); |
| 182 !iter.IsAtEnd(); iter.Advance()) { | 182 !iter.IsAtEnd(); iter.Advance()) { |
| 183 Subscription* subscription = iter.GetCurrentValue(); | 183 Subscription* subscription = iter.GetCurrentValue(); |
| 184 if (subscription->permission != permission) | 184 if (subscription->permission != permission) |
| 185 continue; | 185 continue; |
| 186 | 186 |
| 187 if (subscription->current_value == status) | 187 if (subscription->current_value == status) |
| 188 continue; | 188 continue; |
| 189 | 189 |
| 190 subscription->current_value = status; | 190 subscription->current_value = status; |
| 191 | 191 |
| 192 // Add the callback to |callbacks| which will be run after the loop to | 192 // Add the callback to |callbacks| which will be run after the loop to |
| 193 // prevent re-entrance issues. | 193 // prevent re-entrance issues. |
| 194 callbacks.push_back(base::Bind(subscription->callback, status)); | 194 callbacks.push_back(base::Bind(subscription->callback, status)); |
| 195 } | 195 } |
| 196 | 196 |
| 197 for (const auto& callback : callbacks) | 197 for (const auto& callback : callbacks) |
| 198 callback.Run(); | 198 callback.Run(); |
| 199 } | 199 } |
| 200 | 200 |
| 201 } // namespace content | 201 } // namespace content |
| OLD | NEW |