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/webrtc/media_stream_device_permissions.h"
6 
7 #include <stddef.h>
8 
9 #include "base/values.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "components/content_settings/core/browser/host_content_settings_map.h"
12 #include "components/content_settings/core/common/content_settings_pattern.h"
13 #include "components/prefs/pref_service.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"
18 
GetDevicePolicy(const Profile * profile,const GURL & security_origin,const char * policy_name,const char * allowed_urls_pref_name)19 MediaStreamDevicePolicy GetDevicePolicy(const Profile* profile,
20                                         const GURL& security_origin,
21                                         const char* policy_name,
22                                         const char* allowed_urls_pref_name) {
23   DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
24 
25   // If the security origin policy matches a value in allowed urls list, allow
26   // it.  Otherwise, check the |policy_name| master switch for the default
27   // behavior.
28 
29   const PrefService* prefs = profile->GetPrefs();
30 
31   const base::ListValue* list = prefs->GetList(allowed_urls_pref_name);
32   std::string value;
33   for (size_t i = 0; i < list->GetSize(); ++i) {
34     if (list->GetString(i, &value)) {
35       ContentSettingsPattern pattern =
36           ContentSettingsPattern::FromString(value);
37       if (pattern == ContentSettingsPattern::Wildcard()) {
38         DLOG(WARNING) << "Ignoring wildcard URL pattern: " << value;
39         continue;
40       }
41       DLOG_IF(ERROR, !pattern.IsValid()) << "Invalid URL pattern: " << value;
42       if (pattern.IsValid() && pattern.Matches(security_origin))
43         return ALWAYS_ALLOW;
44     }
45   }
46 
47   // If a match was not found, check if audio capture is otherwise disallowed
48   // or if the user should be prompted.  Setting the policy value to "true"
49   // is equal to not setting it at all, so from hereon out, we will return
50   // either POLICY_NOT_SET (prompt) or ALWAYS_DENY (no prompt, no access).
51   if (!prefs->GetBoolean(policy_name))
52     return ALWAYS_DENY;
53 
54   return POLICY_NOT_SET;
55 }
56