Refactor WebsiteSettings to operate on a SecurityInfo
[chromium-blink-merge.git] / chrome / browser / media / media_stream_device_permissions.cc
blob71b77ae150da8ac58a2a0dc93b95bb8bde2e6f6f
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/prefs/pref_service.h"
8 #include "base/values.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "components/content_settings/core/browser/host_content_settings_map.h"
11 #include "components/content_settings/core/common/content_settings_pattern.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "content/public/common/origin_util.h"
14 #include "extensions/common/constants.h"
15 #include "url/gurl.h"
17 bool ShouldPersistContentSetting(ContentSetting setting,
18 const GURL& origin,
19 bool is_pepper_request) {
20 // When the request is from an invalid scheme we don't persist it.
21 if (!ContentSettingsPattern::FromURLNoWildcard(origin).IsValid())
22 return false;
24 // It's safe to persist block settings all the time.
25 if (setting == CONTENT_SETTING_BLOCK)
26 return true;
28 // Pepper requests should always be persisted to prevent annoying users of
29 // plugins.
30 if (is_pepper_request)
31 return true;
33 // We persist requests from secure origins.
34 if (content::IsOriginSecure(origin))
35 return true;
37 return false;
40 MediaStreamDevicePolicy GetDevicePolicy(const Profile* profile,
41 const GURL& security_origin,
42 const char* policy_name,
43 const char* whitelist_policy_name) {
44 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
46 // If the security origin policy matches a value in the whitelist, allow it.
47 // Otherwise, check the |policy_name| master switch for the default behavior.
49 const PrefService* prefs = profile->GetPrefs();
51 const base::ListValue* list = prefs->GetList(whitelist_policy_name);
52 std::string value;
53 for (size_t i = 0; i < list->GetSize(); ++i) {
54 if (list->GetString(i, &value)) {
55 ContentSettingsPattern pattern =
56 ContentSettingsPattern::FromString(value);
57 if (pattern == ContentSettingsPattern::Wildcard()) {
58 DLOG(WARNING) << "Ignoring wildcard URL pattern: " << value;
59 continue;
61 DLOG_IF(ERROR, !pattern.IsValid()) << "Invalid URL pattern: " << value;
62 if (pattern.IsValid() && pattern.Matches(security_origin))
63 return ALWAYS_ALLOW;
67 // If a match was not found, check if audio capture is otherwise disallowed
68 // or if the user should be prompted. Setting the policy value to "true"
69 // is equal to not setting it at all, so from hereon out, we will return
70 // either POLICY_NOT_SET (prompt) or ALWAYS_DENY (no prompt, no access).
71 if (!prefs->GetBoolean(policy_name))
72 return ALWAYS_DENY;
74 return POLICY_NOT_SET;