summaryrefslogtreecommitdiffstats
path: root/chrome/browser/media/media_stream_device_permissions.cc
blob: 1e70d21bf75abe10fc32bd62aef381c8efe04090 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/media/media_stream_device_permissions.h"

#include <stddef.h>

#include "base/prefs/pref_service.h"
#include "base/values.h"
#include "chrome/browser/profiles/profile.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/content_settings/core/common/content_settings_pattern.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/common/origin_util.h"
#include "extensions/common/constants.h"
#include "url/gurl.h"

bool ShouldPersistContentSetting(ContentSetting setting,
                                 const GURL& origin,
                                 bool is_pepper_request) {
  // When the request is from an invalid scheme we don't persist it.
  if (!ContentSettingsPattern::FromURLNoWildcard(origin).IsValid())
    return false;

  // It's safe to persist block settings all the time.
  if (setting == CONTENT_SETTING_BLOCK)
    return true;

  // Pepper requests should always be persisted to prevent annoying users of
  // plugins.
  if (is_pepper_request)
    return true;

  // We persist requests from secure origins.
  if (content::IsOriginSecure(origin))
    return true;

  return false;
}

MediaStreamDevicePolicy GetDevicePolicy(const Profile* profile,
                                        const GURL& security_origin,
                                        const char* policy_name,
                                        const char* whitelist_policy_name) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

  // If the security origin policy matches a value in the whitelist, allow it.
  // Otherwise, check the |policy_name| master switch for the default behavior.

  const PrefService* prefs = profile->GetPrefs();

  const base::ListValue* list = prefs->GetList(whitelist_policy_name);
  std::string value;
  for (size_t i = 0; i < list->GetSize(); ++i) {
    if (list->GetString(i, &value)) {
      ContentSettingsPattern pattern =
          ContentSettingsPattern::FromString(value);
      if (pattern == ContentSettingsPattern::Wildcard()) {
        DLOG(WARNING) << "Ignoring wildcard URL pattern: " << value;
        continue;
      }
      DLOG_IF(ERROR, !pattern.IsValid()) << "Invalid URL pattern: " << value;
      if (pattern.IsValid() && pattern.Matches(security_origin))
        return ALWAYS_ALLOW;
    }
  }

  // If a match was not found, check if audio capture is otherwise disallowed
  // or if the user should be prompted.  Setting the policy value to "true"
  // is equal to not setting it at all, so from hereon out, we will return
  // either POLICY_NOT_SET (prompt) or ALWAYS_DENY (no prompt, no access).
  if (!prefs->GetBoolean(policy_name))
    return ALWAYS_DENY;

  return POLICY_NOT_SET;
}