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 "content/public/common/origin_util.h"
16 #include "extensions/common/constants.h"
19 #if defined(OS_CHROMEOS)
20 #include "components/user_manager/user_manager.h"
25 bool IsInKioskMode() {
26 if (base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kKioskMode
))
29 #if defined(OS_CHROMEOS)
30 const user_manager::UserManager
* user_manager
=
31 user_manager::UserManager::Get();
32 return user_manager
&& user_manager
->IsLoggedInAsKioskApp();
40 bool ShouldPersistContentSetting(ContentSetting setting
,
42 content::MediaStreamRequestType type
) {
43 // When the request is from an invalid scheme we don't persist it.
44 if (!ContentSettingsPattern::FromURLNoWildcard(origin
).IsValid())
47 // It's safe to persist block settings all the time.
48 if (setting
== CONTENT_SETTING_BLOCK
)
51 // Pepper requests should always be persisted to prevent annoying users of
53 if (type
== content::MEDIA_OPEN_DEVICE
)
56 // We persist requests from secure origins.
57 if (content::IsOriginSecure(origin
))
63 MediaStreamDevicePolicy
GetDevicePolicy(const Profile
* profile
,
64 const GURL
& security_origin
,
65 const char* policy_name
,
66 const char* whitelist_policy_name
) {
67 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
69 // If the security origin policy matches a value in the whitelist, allow it.
70 // Otherwise, check the |policy_name| master switch for the default behavior.
72 const PrefService
* prefs
= profile
->GetPrefs();
74 // TODO(tommi): Remove the kiosk mode check when the whitelist below
75 // is visible in the media exceptions UI.
76 // See discussion here: https://codereview.chromium.org/15738004/
77 if (IsInKioskMode()) {
78 const base::ListValue
* list
= prefs
->GetList(whitelist_policy_name
);
80 for (size_t i
= 0; i
< list
->GetSize(); ++i
) {
81 if (list
->GetString(i
, &value
)) {
82 ContentSettingsPattern pattern
=
83 ContentSettingsPattern::FromString(value
);
84 if (pattern
== ContentSettingsPattern::Wildcard()) {
85 DLOG(WARNING
) << "Ignoring wildcard URL pattern: " << value
;
88 DLOG_IF(ERROR
, !pattern
.IsValid()) << "Invalid URL pattern: " << value
;
89 if (pattern
.IsValid() && pattern
.Matches(security_origin
))
95 // If a match was not found, check if audio capture is otherwise disallowed
96 // or if the user should be prompted. Setting the policy value to "true"
97 // is equal to not setting it at all, so from hereon out, we will return
98 // either POLICY_NOT_SET (prompt) or ALWAYS_DENY (no prompt, no access).
99 if (!prefs
->GetBoolean(policy_name
))
102 return POLICY_NOT_SET
;