Update broken references to image assets
[chromium-blink-merge.git] / chrome / browser / media / media_stream_device_permissions.cc
blob04766db6f450f8fe38dcb62b2ccd84bfe734e16b
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"
17 #include "url/gurl.h"
19 #if defined(OS_CHROMEOS)
20 #include "components/user_manager/user_manager.h"
21 #endif
23 namespace {
25 bool IsInKioskMode() {
26 if (base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kKioskMode))
27 return true;
29 #if defined(OS_CHROMEOS)
30 const user_manager::UserManager* user_manager =
31 user_manager::UserManager::Get();
32 return user_manager && user_manager->IsLoggedInAsKioskApp();
33 #else
34 return false;
35 #endif
38 } // namespace
40 bool ShouldPersistContentSetting(ContentSetting setting,
41 const GURL& origin,
42 content::MediaStreamRequestType type) {
43 // When the request is from an invalid scheme we don't persist it.
44 if (!ContentSettingsPattern::FromURLNoWildcard(origin).IsValid())
45 return false;
47 // It's safe to persist block settings all the time.
48 if (setting == CONTENT_SETTING_BLOCK)
49 return true;
51 // Pepper requests should always be persisted to prevent annoying users of
52 // plugins.
53 if (type == content::MEDIA_OPEN_DEVICE)
54 return true;
56 // We persist requests from secure origins.
57 if (content::IsOriginSecure(origin))
58 return true;
60 return false;
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);
79 std::string value;
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;
86 continue;
88 DLOG_IF(ERROR, !pattern.IsValid()) << "Invalid URL pattern: " << value;
89 if (pattern.IsValid() && pattern.Matches(security_origin))
90 return ALWAYS_ALLOW;
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))
100 return ALWAYS_DENY;
102 return POLICY_NOT_SET;