• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2012 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 #ifndef CHROME_BROWSER_MEDIA_MEDIA_STREAM_DEVICES_CONTROLLER_H_
6 #define CHROME_BROWSER_MEDIA_MEDIA_STREAM_DEVICES_CONTROLLER_H_
7 
8 #include <map>
9 #include <string>
10 
11 #include "content/public/browser/web_contents_delegate.h"
12 
13 class Profile;
14 class TabSpecificContentSettings;
15 
16 namespace content {
17 class WebContents;
18 }
19 
20 namespace user_prefs {
21 class PrefRegistrySyncable;
22 }
23 
24 class MediaStreamDevicesController {
25  public:
26   // Permissions for media stream types.
27   enum Permission {
28     MEDIA_NONE,
29     MEDIA_ALLOWED,
30     MEDIA_BLOCKED_BY_POLICY,
31     MEDIA_BLOCKED_BY_USER_SETTING,
32     MEDIA_BLOCKED_BY_USER,
33   };
34 
35   struct MediaStreamTypeSettings {
36     MediaStreamTypeSettings(Permission permission,
37                             const std::string& requested_device_id);
38     MediaStreamTypeSettings();
39     ~MediaStreamTypeSettings();
40 
41     Permission permission;
42     std::string requested_device_id;
43   };
44 
45   typedef std::map<content::MediaStreamType, MediaStreamTypeSettings>
46       MediaStreamTypeSettingsMap;
47 
48   MediaStreamDevicesController(content::WebContents* web_contents,
49                                const content::MediaStreamRequest& request,
50                                const content::MediaResponseCallback& callback);
51 
52   virtual ~MediaStreamDevicesController();
53 
54   // TODO(tommi): Clean up all the policy code and integrate with
55   // HostContentSettingsMap instead.  This will make creating the UI simpler
56   // and the code cleaner.  crbug.com/244389.
57 
58   // Registers the prefs backing the audio and video policies.
59   static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
60 
61   // Public method to be called before creating the MediaStreamInfoBarDelegate.
62   // This function will check the content settings exceptions and take the
63   // corresponding action on exception which matches the request.
64   bool DismissInfoBarAndTakeActionOnSettings();
65 
66   // Public methods to be called by MediaStreamInfoBarDelegate;
67   bool HasAudio() const;
68   bool HasVideo() const;
69   const std::string& GetSecurityOriginSpec() const;
70   void Accept(bool update_content_setting);
71   void Deny(bool update_content_setting);
72 
73  private:
74   enum DevicePolicy {
75     POLICY_NOT_SET,
76     ALWAYS_DENY,
77     ALWAYS_ALLOW,
78   };
79 
80   // Called by GetAudioDevicePolicy and GetVideoDevicePolicy to check
81   // the currently set capture device policy.
82   DevicePolicy GetDevicePolicy(const char* policy_name,
83                                const char* whitelist_policy_name) const;
84 
85   // Returns true if the origin of the request has been granted the media
86   // access before, otherwise returns false.
87   bool IsRequestAllowedByDefault() const;
88 
89   // Check if any device of the request has been blocked for the origin of the
90   // request and clears |microphone_requested_| or |webcam_requested_| flags if
91   // they are not allowed anymore. Returns the number of devices that are
92   // allowed after this step. If the count reaches zero the request can be
93   // denied completely, else it still has to be partially fullfilled.
94   int FilterBlockedByDefaultDevices();
95 
96   // Returns true if the media section in content settings is set to
97   // |CONTENT_SETTING_BLOCK|, otherwise returns false.
98   bool IsDefaultMediaAccessBlocked() const;
99 
100   // Returns true if the origin is a secure scheme, otherwise returns false.
101   bool IsSchemeSecure() const;
102 
103   // Returns true if request's origin is from internal objects like
104   // chrome://URLs, otherwise returns false.
105   bool ShouldAlwaysAllowOrigin() const;
106 
107   // Sets the permission of the origin of the request. This is triggered when
108   // the users deny the request or allow the request for https sites.
109   void SetPermission(bool allowed) const;
110 
111   // Notifies the content setting UI that the media stream access request or
112   // part of the request is accepted.
113   void NotifyUIRequestAccepted() const;
114 
115   // Notifies the content setting UI that the media stream access request or
116   // part of the request is denied.
117   void NotifyUIRequestDenied();
118 
119   // Return true if the type has been requested and permission is currently set
120   // to allowed. Note that it does not reflect the final permission decision.
121   // This function is called during the filtering steps to check if the type has
122   // been blocked yet or not and the permission may be changed to blocked during
123   // these filterings. See also the initialization in the constructor and
124   // comments on that.
125   bool IsDeviceAudioCaptureRequestedAndAllowed() const;
126   bool IsDeviceVideoCaptureRequestedAndAllowed() const;
127 
128   content::WebContents* web_contents_;
129 
130   // The owner of this class needs to make sure it does not outlive the profile.
131   Profile* profile_;
132 
133   // Weak pointer to the tab specific content settings of the tab for which the
134   // MediaStreamDevicesController was created. The tab specific content
135   // settings are associated with a the web contents of the tab. The
136   // MediaStreamDeviceController must not outlive the web contents for which it
137   // was created.
138   TabSpecificContentSettings* content_settings_;
139 
140   // The original request for access to devices.
141   const content::MediaStreamRequest request_;
142 
143   // The callback that needs to be Run to notify WebRTC of whether access to
144   // audio/video devices was granted or not.
145   content::MediaResponseCallback callback_;
146 
147   // Holds the requested media types and the permission for each type. It is
148   // passed to the tab specific content settings when the permissions have been
149   // resolved. Currently only used by MEDIA_DEVICE_AUDIO_CAPTURE and
150   // MEDIA_DEVICE_VIDEO_CAPTURE since those are the only types that require
151   // updates in the settings.
152   MediaStreamTypeSettingsMap request_permissions_;
153 
154   DISALLOW_COPY_AND_ASSIGN(MediaStreamDevicesController);
155 };
156 
157 #endif  // CHROME_BROWSER_MEDIA_MEDIA_STREAM_DEVICES_CONTROLLER_H_
158