1 // Copyright 2014 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/media_stream_device_permissions.h"
7 #include "base/command_line.h"
8 #include "base/prefs/pref_service.h"
9 #include "base/values.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/common/chrome_switches.h"
12 #include "components/content_settings/core/browser/host_content_settings_map.h"
13 #include "components/content_settings/core/common/content_settings_pattern.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "extensions/common/constants.h"
18 #if defined(OS_CHROMEOS)
19 #include "components/user_manager/user_manager.h"
24 bool IsInKioskMode() {
25 if (base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kKioskMode
))
28 #if defined(OS_CHROMEOS)
29 const user_manager::UserManager
* user_manager
=
30 user_manager::UserManager::Get();
31 return user_manager
&& user_manager
->IsLoggedInAsKioskApp();
39 bool ShouldPersistContentSetting(ContentSetting setting
,
41 content::MediaStreamRequestType type
) {
42 // When the request is from an invalid scheme we don't persist it.
43 if (!ContentSettingsPattern::FromURLNoWildcard(origin
).IsValid())
46 // It's safe to persist block settings all the time.
47 if (setting
== CONTENT_SETTING_BLOCK
)
50 // Pepper requests should always be persisted to prevent annoying users of
52 if (type
== content::MEDIA_OPEN_DEVICE
)
55 // We persist requests from secure origins.
56 if (origin
.SchemeIsSecure())
62 bool CheckAllowAllMediaStreamContentForOrigin(Profile
* profile
,
63 const GURL
& security_origin
,
64 ContentSettingsType type
) {
65 DCHECK(type
== CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
||
66 type
== CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
);
67 return profile
->GetHostContentSettingsMap()->ShouldAllowAllContent(
68 security_origin
, security_origin
, type
);
71 MediaStreamDevicePolicy
GetDevicePolicy(const Profile
* profile
,
72 const GURL
& security_origin
,
73 const char* policy_name
,
74 const char* whitelist_policy_name
) {
75 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
77 // If the security origin policy matches a value in the whitelist, allow it.
78 // Otherwise, check the |policy_name| master switch for the default behavior.
80 const PrefService
* prefs
= profile
->GetPrefs();
82 // TODO(tommi): Remove the kiosk mode check when the whitelist below
83 // is visible in the media exceptions UI.
84 // See discussion here: https://codereview.chromium.org/15738004/
85 if (IsInKioskMode()) {
86 const base::ListValue
* list
= prefs
->GetList(whitelist_policy_name
);
88 for (size_t i
= 0; i
< list
->GetSize(); ++i
) {
89 if (list
->GetString(i
, &value
)) {
90 ContentSettingsPattern pattern
=
91 ContentSettingsPattern::FromString(value
);
92 if (pattern
== ContentSettingsPattern::Wildcard()) {
93 DLOG(WARNING
) << "Ignoring wildcard URL pattern: " << value
;
96 DLOG_IF(ERROR
, !pattern
.IsValid()) << "Invalid URL pattern: " << value
;
97 if (pattern
.IsValid() && pattern
.Matches(security_origin
))
103 // If a match was not found, check if audio capture is otherwise disallowed
104 // or if the user should be prompted. Setting the policy value to "true"
105 // is equal to not setting it at all, so from hereon out, we will return
106 // either POLICY_NOT_SET (prompt) or ALWAYS_DENY (no prompt, no access).
107 if (!prefs
->GetBoolean(policy_name
))
110 return POLICY_NOT_SET
;