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.
4
5#include "chrome/browser/media/media_stream_device_permissions.h"
6
7#include "base/command_line.h"
8#include "base/prefs/pref_service.h"
9#include "base/values.h"
10#include "chrome/browser/content_settings/host_content_settings_map.h"
11#include "chrome/browser/profiles/profile.h"
12#include "chrome/common/chrome_switches.h"
13#include "components/content_settings/core/common/content_settings_pattern.h"
14#include "content/public/browser/browser_thread.h"
15#include "url/gurl.h"
16
17#if defined(OS_CHROMEOS)
18#include "components/user_manager/user_manager.h"
19#endif
20
21namespace {
22
23bool IsInKioskMode() {
24  if (base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kKioskMode))
25    return true;
26
27#if defined(OS_CHROMEOS)
28  const user_manager::UserManager* user_manager =
29      user_manager::UserManager::Get();
30  return user_manager && user_manager->IsLoggedInAsKioskApp();
31#else
32  return false;
33#endif
34}
35
36}  // namespace
37
38bool CheckAllowAllMediaStreamContentForOrigin(Profile* profile,
39                                              const GURL& security_origin) {
40  // TODO(markusheintz): Replace CONTENT_SETTINGS_TYPE_MEDIA_STREAM with the
41  // appropriate new CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC and
42  // CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA.
43  return profile->GetHostContentSettingsMap()->ShouldAllowAllContent(
44      security_origin, security_origin, CONTENT_SETTINGS_TYPE_MEDIASTREAM);
45}
46
47MediaStreamDevicePolicy GetDevicePolicy(Profile* profile,
48                                        const GURL& security_origin,
49                                        const char* policy_name,
50                                        const char* whitelist_policy_name) {
51  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
52
53  // If the security origin policy matches a value in the whitelist, allow it.
54  // Otherwise, check the |policy_name| master switch for the default behavior.
55
56  PrefService* prefs = profile->GetPrefs();
57
58  // TODO(tommi): Remove the kiosk mode check when the whitelist below
59  // is visible in the media exceptions UI.
60  // See discussion here: https://codereview.chromium.org/15738004/
61  if (IsInKioskMode()) {
62    const base::ListValue* list = prefs->GetList(whitelist_policy_name);
63    std::string value;
64    for (size_t i = 0; i < list->GetSize(); ++i) {
65      if (list->GetString(i, &value)) {
66        ContentSettingsPattern pattern =
67            ContentSettingsPattern::FromString(value);
68        if (pattern == ContentSettingsPattern::Wildcard()) {
69          DLOG(WARNING) << "Ignoring wildcard URL pattern: " << value;
70          continue;
71        }
72        DLOG_IF(ERROR, !pattern.IsValid()) << "Invalid URL pattern: " << value;
73        if (pattern.IsValid() && pattern.Matches(security_origin))
74          return ALWAYS_ALLOW;
75      }
76    }
77  }
78
79  // If a match was not found, check if audio capture is otherwise disallowed
80  // or if the user should be prompted.  Setting the policy value to "true"
81  // is equal to not setting it at all, so from hereon out, we will return
82  // either POLICY_NOT_SET (prompt) or ALWAYS_DENY (no prompt, no access).
83  if (!prefs->GetBoolean(policy_name))
84    return ALWAYS_DENY;
85
86  return POLICY_NOT_SET;
87}
88