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