ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / chrome / browser / media / protected_media_identifier_permission_context.cc
blob53496febc38ba2cadbf2db45a57fe21a1dec6f93
1 // Copyright 2013 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.
5 #include "chrome/browser/media/protected_media_identifier_permission_context.h"
7 #include "base/prefs/pref_service.h"
8 #include "chrome/browser/content_settings/tab_specific_content_settings.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/common/pref_names.h"
11 #include "components/content_settings/core/common/permission_request_id.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "content/public/browser/web_contents.h"
15 #if defined(OS_CHROMEOS)
16 #include <utility>
18 #include "chrome/browser/chromeos/attestation/platform_verification_dialog.h"
19 #include "chrome/browser/chromeos/settings/cros_settings.h"
20 #include "chromeos/settings/cros_settings_names.h"
21 #include "ui/views/widget/widget.h"
23 using chromeos::attestation::PlatformVerificationDialog;
24 using chromeos::attestation::PlatformVerificationFlow;
25 #endif
27 ProtectedMediaIdentifierPermissionContext::
28 ProtectedMediaIdentifierPermissionContext(Profile* profile)
29 : PermissionContextBase(profile,
30 CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER)
31 #if defined(OS_CHROMEOS)
33 weak_factory_(this)
34 #endif
38 ProtectedMediaIdentifierPermissionContext::
39 ~ProtectedMediaIdentifierPermissionContext() {
42 void ProtectedMediaIdentifierPermissionContext::RequestPermission(
43 content::WebContents* web_contents,
44 const PermissionRequestID& id,
45 const GURL& requesting_origin,
46 bool user_gesture,
47 const BrowserPermissionCallback& callback) {
48 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
50 GURL embedding_origin = web_contents->GetLastCommittedURL().GetOrigin();
52 if (!requesting_origin.is_valid() || !embedding_origin.is_valid() ||
53 !IsProtectedMediaIdentifierEnabled()) {
54 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback,
55 false /* persist */, CONTENT_SETTING_BLOCK);
56 return;
59 #if defined(OS_CHROMEOS)
60 // On ChromeOS, we don't use PermissionContextBase::RequestPermission() which
61 // uses the standard permission infobar/bubble UI. See http://crbug.com/454847
62 // Instead, we check the content setting and show the existing platform
63 // verification UI.
64 // TODO(xhwang): Remove when http://crbug.com/454847 is fixed.
65 ContentSetting content_setting =
66 GetPermissionStatus(requesting_origin, embedding_origin);
68 if (content_setting == CONTENT_SETTING_ALLOW ||
69 content_setting == CONTENT_SETTING_BLOCK) {
70 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback,
71 false /* persist */, content_setting);
72 return;
75 // Since the dialog is modal, we only support one prompt per |web_contents|.
76 // Reject the new one if there is already one pending. See
77 // http://crbug.com/447005
78 if (pending_requests_.count(web_contents)) {
79 callback.Run(CONTENT_SETTING_DEFAULT);
80 return;
83 views::Widget* widget = PlatformVerificationDialog::ShowDialog(
84 web_contents, requesting_origin,
85 base::Bind(&ProtectedMediaIdentifierPermissionContext::
86 OnPlatformVerificationResult,
87 weak_factory_.GetWeakPtr(), web_contents, id,
88 requesting_origin, embedding_origin, callback));
89 pending_requests_.insert(
90 std::make_pair(web_contents, std::make_pair(widget, id)));
91 #else
92 PermissionContextBase::RequestPermission(web_contents, id, requesting_origin,
93 user_gesture, callback);
94 #endif
97 ContentSetting ProtectedMediaIdentifierPermissionContext::GetPermissionStatus(
98 const GURL& requesting_origin,
99 const GURL& embedding_origin) const {
100 if (!IsProtectedMediaIdentifierEnabled())
101 return CONTENT_SETTING_BLOCK;
103 return PermissionContextBase::GetPermissionStatus(requesting_origin,
104 embedding_origin);
107 void ProtectedMediaIdentifierPermissionContext::CancelPermissionRequest(
108 content::WebContents* web_contents,
109 const PermissionRequestID& id) {
110 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
112 #if defined(OS_CHROMEOS)
113 PendingRequestMap::iterator request = pending_requests_.find(web_contents);
114 if (request == pending_requests_.end() || !request->second.second.Equals(id))
115 return;
117 // Close the |widget_|. OnPlatformVerificationResult() will be fired
118 // during this process, but since |web_contents| is removed from
119 // |pending_requests_|, the callback will simply be dropped.
120 views::Widget* widget = request->second.first;
121 pending_requests_.erase(request);
122 widget->Close();
123 #else
124 PermissionContextBase::CancelPermissionRequest(web_contents, id);
125 #endif
128 void ProtectedMediaIdentifierPermissionContext::UpdateTabContext(
129 const PermissionRequestID& id,
130 const GURL& requesting_frame,
131 bool allowed) {
132 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
134 // WebContents may have gone away.
135 TabSpecificContentSettings* content_settings =
136 TabSpecificContentSettings::Get(id.render_process_id(),
137 id.render_view_id());
138 if (content_settings) {
139 content_settings->OnProtectedMediaIdentifierPermissionSet(
140 requesting_frame.GetOrigin(), allowed);
144 // TODO(xhwang): We should consolidate the "protected content" related pref
145 // across platforms.
146 bool ProtectedMediaIdentifierPermissionContext::
147 IsProtectedMediaIdentifierEnabled() const {
148 bool enabled = false;
150 #if defined(OS_ANDROID)
151 enabled = profile()->GetPrefs()->GetBoolean(
152 prefs::kProtectedMediaIdentifierEnabled);
153 #endif
155 #if defined(OS_CHROMEOS)
156 // This could be disabled by the device policy.
157 bool enabled_for_device = false;
158 enabled = chromeos::CrosSettings::Get()->GetBoolean(
159 chromeos::kAttestationForContentProtectionEnabled,
160 &enabled_for_device) &&
161 enabled_for_device &&
162 profile()->GetPrefs()->GetBoolean(prefs::kEnableDRM);
163 #endif
165 DVLOG_IF(1, !enabled)
166 << "Protected media identifier disabled by the user or by device policy.";
167 return enabled;
170 #if defined(OS_CHROMEOS)
171 void ProtectedMediaIdentifierPermissionContext::OnPlatformVerificationResult(
172 content::WebContents* web_contents,
173 const PermissionRequestID& id,
174 const GURL& requesting_origin,
175 const GURL& embedding_origin,
176 const BrowserPermissionCallback& callback,
177 chromeos::attestation::PlatformVerificationFlow::ConsentResponse response) {
178 // The request may have been canceled. Drop the callback in that case.
179 PendingRequestMap::iterator request = pending_requests_.find(web_contents);
180 if (request == pending_requests_.end())
181 return;
183 DCHECK(request->second.second.Equals(id));
184 pending_requests_.erase(request);
186 ContentSetting content_setting;
187 bool persist; // Whether the ContentSetting should be saved.
188 switch (response) {
189 case PlatformVerificationFlow::CONSENT_RESPONSE_NONE:
190 content_setting = CONTENT_SETTING_DEFAULT;
191 persist = false;
192 break;
193 case PlatformVerificationFlow::CONSENT_RESPONSE_ALLOW:
194 content_setting = CONTENT_SETTING_ALLOW;
195 persist = true;
196 break;
197 case PlatformVerificationFlow::CONSENT_RESPONSE_DENY:
198 content_setting = CONTENT_SETTING_BLOCK;
199 persist = true;
200 break;
203 NotifyPermissionSet(
204 id, requesting_origin, embedding_origin, callback,
205 persist, content_setting);
207 #endif