| 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::mojom::PermissionStatus)> callback; | 21 base::Callback<void(content::mojom::PermissionStatus)> callback; |
| 22 mojom::PermissionStatus current_value; | 22 mojom::PermissionStatus 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 url::Origin& origin, |
| 28 const GURL& embedding_origin) | 28 const url::Origin& embedding_origin) |
| 29 : type(type), | 29 : type(type), origin(origin), embedding_origin(embedding_origin) {} |
| 30 origin(origin), | |
| 31 embedding_origin(embedding_origin) { | |
| 32 } | |
| 33 | 30 |
| 34 bool LayoutTestPermissionManager::PermissionDescription::operator==( | 31 bool LayoutTestPermissionManager::PermissionDescription::operator==( |
| 35 const PermissionDescription& other) const { | 32 const PermissionDescription& other) const { |
| 36 return type == other.type && | 33 return type == other.type && |
| 37 origin == other.origin && | 34 origin == other.origin && |
| 38 embedding_origin == other.embedding_origin; | 35 embedding_origin == other.embedding_origin; |
| 39 } | 36 } |
| 40 | 37 |
| 41 bool LayoutTestPermissionManager::PermissionDescription::operator!=( | 38 bool LayoutTestPermissionManager::PermissionDescription::operator!=( |
| 42 const PermissionDescription& other) const { | 39 const PermissionDescription& other) const { |
| 43 return !this->operator==(other); | 40 return !this->operator==(other); |
| 44 } | 41 } |
| 45 | 42 |
| 46 size_t LayoutTestPermissionManager::PermissionDescription::Hash::operator()( | 43 size_t LayoutTestPermissionManager::PermissionDescription::Hash::operator()( |
| 47 const PermissionDescription& description) const { | 44 const PermissionDescription& description) const { |
| 48 size_t hash = BASE_HASH_NAMESPACE::hash<int>()(static_cast<int>( | 45 size_t hash = BASE_HASH_NAMESPACE::hash<int>()(static_cast<int>( |
| 49 description.type)); | 46 description.type)); |
| 50 hash += BASE_HASH_NAMESPACE::hash<std::string>()( | 47 hash += BASE_HASH_NAMESPACE::hash<std::string>()( |
| 51 description.embedding_origin.spec()); | 48 description.embedding_origin.Serialize()); |
| 52 hash += BASE_HASH_NAMESPACE::hash<std::string>()( | 49 hash += |
| 53 description.origin.spec()); | 50 BASE_HASH_NAMESPACE::hash<std::string>()(description.origin.Serialize()); |
| 54 return hash; | 51 return hash; |
| 55 } | 52 } |
| 56 | 53 |
| 57 LayoutTestPermissionManager::LayoutTestPermissionManager() | 54 LayoutTestPermissionManager::LayoutTestPermissionManager() |
| 58 : PermissionManager() { | 55 : PermissionManager() { |
| 59 } | 56 } |
| 60 | 57 |
| 61 LayoutTestPermissionManager::~LayoutTestPermissionManager() { | 58 LayoutTestPermissionManager::~LayoutTestPermissionManager() { |
| 62 } | 59 } |
| 63 | 60 |
| 64 int LayoutTestPermissionManager::RequestPermission( | 61 int LayoutTestPermissionManager::RequestPermission( |
| 65 PermissionType permission, | 62 PermissionType permission, |
| 66 RenderFrameHost* render_frame_host, | 63 RenderFrameHost* render_frame_host, |
| 67 const GURL& requesting_origin, | 64 const url::Origin& requesting_origin, |
| 68 const base::Callback<void(mojom::PermissionStatus)>& callback) { | 65 const base::Callback<void(mojom::PermissionStatus)>& callback) { |
| 69 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 66 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 70 | 67 |
| 71 callback.Run(GetPermissionStatus( | 68 callback.Run(GetPermissionStatus( |
| 72 permission, requesting_origin, | 69 permission, requesting_origin, |
| 73 WebContents::FromRenderFrameHost(render_frame_host) | 70 url::Origin(WebContents::FromRenderFrameHost(render_frame_host) |
| 74 ->GetLastCommittedURL().GetOrigin())); | 71 ->GetLastCommittedURL()))); |
| 75 return kNoPendingOperation; | 72 return kNoPendingOperation; |
| 76 } | 73 } |
| 77 | 74 |
| 78 int LayoutTestPermissionManager::RequestPermissions( | 75 int LayoutTestPermissionManager::RequestPermissions( |
| 79 const std::vector<PermissionType>& permissions, | 76 const std::vector<PermissionType>& permissions, |
| 80 content::RenderFrameHost* render_frame_host, | 77 content::RenderFrameHost* render_frame_host, |
| 81 const GURL& requesting_origin, | 78 const url::Origin& requesting_origin, |
| 82 const base::Callback<void(const std::vector<mojom::PermissionStatus>&)>& | 79 const base::Callback<void(const std::vector<mojom::PermissionStatus>&)>& |
| 83 callback) { | 80 callback) { |
| 84 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 81 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 85 | 82 |
| 86 std::vector<mojom::PermissionStatus> result(permissions.size()); | 83 std::vector<mojom::PermissionStatus> result(permissions.size()); |
| 87 const GURL& embedding_origin = | 84 const url::Origin embedding_origin( |
| 88 WebContents::FromRenderFrameHost(render_frame_host) | 85 WebContents::FromRenderFrameHost(render_frame_host) |
| 89 ->GetLastCommittedURL().GetOrigin(); | 86 ->GetLastCommittedURL()); |
| 90 for (const auto& permission : permissions) { | 87 for (const auto& permission : permissions) { |
| 91 result.push_back(GetPermissionStatus( | 88 result.push_back(GetPermissionStatus( |
| 92 permission, requesting_origin, embedding_origin)); | 89 permission, requesting_origin, embedding_origin)); |
| 93 } | 90 } |
| 94 | 91 |
| 95 callback.Run(result); | 92 callback.Run(result); |
| 96 return kNoPendingOperation; | 93 return kNoPendingOperation; |
| 97 } | 94 } |
| 98 | 95 |
| 99 void LayoutTestPermissionManager::CancelPermissionRequest(int request_id) { | 96 void LayoutTestPermissionManager::CancelPermissionRequest(int request_id) { |
| 100 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 97 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 101 } | 98 } |
| 102 | 99 |
| 103 void LayoutTestPermissionManager::ResetPermission( | 100 void LayoutTestPermissionManager::ResetPermission( |
| 104 PermissionType permission, | 101 PermissionType permission, |
| 105 const GURL& requesting_origin, | 102 const url::Origin& requesting_origin, |
| 106 const GURL& embedding_origin) { | 103 const url::Origin& embedding_origin) { |
| 107 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 104 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 108 | 105 |
| 109 base::AutoLock lock(permissions_lock_); | 106 base::AutoLock lock(permissions_lock_); |
| 110 | 107 |
| 111 auto it = permissions_.find( | 108 auto it = permissions_.find( |
| 112 PermissionDescription(permission, requesting_origin, embedding_origin)); | 109 PermissionDescription(permission, requesting_origin, embedding_origin)); |
| 113 if (it == permissions_.end()) | 110 if (it == permissions_.end()) |
| 114 return; | 111 return; |
| 115 permissions_.erase(it); | 112 permissions_.erase(it); |
| 116 } | 113 } |
| 117 | 114 |
| 118 mojom::PermissionStatus LayoutTestPermissionManager::GetPermissionStatus( | 115 mojom::PermissionStatus LayoutTestPermissionManager::GetPermissionStatus( |
| 119 PermissionType permission, | 116 PermissionType permission, |
| 120 const GURL& requesting_origin, | 117 const url::Origin& requesting_origin, |
| 121 const GURL& embedding_origin) { | 118 const url::Origin& embedding_origin) { |
| 122 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || | 119 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || |
| 123 BrowserThread::CurrentlyOn(BrowserThread::IO)); | 120 BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 124 | 121 |
| 125 base::AutoLock lock(permissions_lock_); | 122 base::AutoLock lock(permissions_lock_); |
| 126 | 123 |
| 127 auto it = permissions_.find( | 124 auto it = permissions_.find( |
| 128 PermissionDescription(permission, requesting_origin, embedding_origin)); | 125 PermissionDescription(permission, requesting_origin, embedding_origin)); |
| 129 if (it == permissions_.end()) | 126 if (it == permissions_.end()) |
| 130 return mojom::PermissionStatus::DENIED; | 127 return mojom::PermissionStatus::DENIED; |
| 131 return it->second; | 128 return it->second; |
| 132 } | 129 } |
| 133 | 130 |
| 134 void LayoutTestPermissionManager::RegisterPermissionUsage( | 131 void LayoutTestPermissionManager::RegisterPermissionUsage( |
| 135 PermissionType permission, | 132 PermissionType permission, |
| 136 const GURL& requesting_origin, | 133 const url::Origin& requesting_origin, |
| 137 const GURL& embedding_origin) { | 134 const url::Origin& embedding_origin) { |
| 138 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 135 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 139 } | 136 } |
| 140 | 137 |
| 141 int LayoutTestPermissionManager::SubscribePermissionStatusChange( | 138 int LayoutTestPermissionManager::SubscribePermissionStatusChange( |
| 142 PermissionType permission, | 139 PermissionType permission, |
| 143 const GURL& requesting_origin, | 140 const url::Origin& requesting_origin, |
| 144 const GURL& embedding_origin, | 141 const url::Origin& embedding_origin, |
| 145 const base::Callback<void(mojom::PermissionStatus)>& callback) { | 142 const base::Callback<void(mojom::PermissionStatus)>& callback) { |
| 146 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 143 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 147 | 144 |
| 148 Subscription* subscription = new Subscription(); | 145 Subscription* subscription = new Subscription(); |
| 149 subscription->permission = | 146 subscription->permission = |
| 150 PermissionDescription(permission, requesting_origin, embedding_origin); | 147 PermissionDescription(permission, requesting_origin, embedding_origin); |
| 151 subscription->callback = callback; | 148 subscription->callback = callback; |
| 152 subscription->current_value = | 149 subscription->current_value = |
| 153 GetPermissionStatus(permission, | 150 GetPermissionStatus(permission, |
| 154 subscription->permission.origin, | 151 subscription->permission.origin, |
| 155 subscription->permission.embedding_origin); | 152 subscription->permission.embedding_origin); |
| 156 | 153 |
| 157 return subscriptions_.Add(subscription); | 154 return subscriptions_.Add(subscription); |
| 158 } | 155 } |
| 159 | 156 |
| 160 void LayoutTestPermissionManager::UnsubscribePermissionStatusChange( | 157 void LayoutTestPermissionManager::UnsubscribePermissionStatusChange( |
| 161 int subscription_id) { | 158 int subscription_id) { |
| 162 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 159 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 163 | 160 |
| 164 // Whether |subscription_id| is known will be checked by the Remove() call. | 161 // Whether |subscription_id| is known will be checked by the Remove() call. |
| 165 subscriptions_.Remove(subscription_id); | 162 subscriptions_.Remove(subscription_id); |
| 166 } | 163 } |
| 167 | 164 |
| 168 void LayoutTestPermissionManager::SetPermission(PermissionType permission, | 165 void LayoutTestPermissionManager::SetPermission( |
| 169 mojom::PermissionStatus status, | 166 PermissionType permission, |
| 170 const GURL& origin, | 167 mojom::PermissionStatus status, |
| 171 const GURL& embedding_origin) { | 168 const url::Origin& origin, |
| 169 const url::Origin& embedding_origin) { |
| 172 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 170 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 173 | 171 |
| 174 PermissionDescription description(permission, origin, embedding_origin); | 172 PermissionDescription description(permission, origin, embedding_origin); |
| 175 | 173 |
| 176 base::AutoLock lock(permissions_lock_); | 174 base::AutoLock lock(permissions_lock_); |
| 177 | 175 |
| 178 auto it = permissions_.find(description); | 176 auto it = permissions_.find(description); |
| 179 if (it == permissions_.end()) { | 177 if (it == permissions_.end()) { |
| 180 permissions_.insert( | 178 permissions_.insert( |
| 181 std::pair<PermissionDescription, mojom::PermissionStatus>(description, | 179 std::pair<PermissionDescription, mojom::PermissionStatus>(description, |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 // Add the callback to |callbacks| which will be run after the loop to | 211 // Add the callback to |callbacks| which will be run after the loop to |
| 214 // prevent re-entrance issues. | 212 // prevent re-entrance issues. |
| 215 callbacks.push_back(base::Bind(subscription->callback, status)); | 213 callbacks.push_back(base::Bind(subscription->callback, status)); |
| 216 } | 214 } |
| 217 | 215 |
| 218 for (const auto& callback : callbacks) | 216 for (const auto& callback : callbacks) |
| 219 callback.Run(); | 217 callback.Run(); |
| 220 } | 218 } |
| 221 | 219 |
| 222 } // namespace content | 220 } // namespace content |
| OLD | NEW |