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  // Returns true if the origin of the request has been granted the media
88  // access before, otherwise returns false.
89  bool IsRequestAllowedByDefault() const;
90
91  // Check if any device of the request has been blocked for the origin of the
92  // request and clears |microphone_requested_| or |webcam_requested_| flags if
93  // they are not allowed anymore. Returns the number of devices that are
94  // allowed after this step. If the count reaches zero the request can be
95  // denied completely, else it still has to be partially fullfilled.
96  int FilterBlockedByDefaultDevices();
97
98  // Returns true if the media section in content settings is set to
99  // |CONTENT_SETTING_BLOCK|, otherwise returns false.
100  bool IsDefaultMediaAccessBlocked() const;
101
102  // Returns true if the origin is a secure scheme, otherwise returns false.
103  bool IsSchemeSecure() const;
104
105  // Sets the permission of the origin of the request. This is triggered when
106  // the users deny the request or allow the request for https sites.
107  void SetPermission(bool allowed) const;
108
109  // Notifies the content setting UI that the media stream access request or
110  // part of the request is accepted.
111  void NotifyUIRequestAccepted() const;
112
113  // Notifies the content setting UI that the media stream access request or
114  // part of the request is denied.
115  void NotifyUIRequestDenied();
116
117  // Return true if the type has been requested and permission is currently set
118  // to allowed. Note that it does not reflect the final permission decision.
119  // This function is called during the filtering steps to check if the type has
120  // been blocked yet or not and the permission may be changed to blocked during
121  // these filterings. See also the initialization in the constructor and
122  // comments on that.
123  bool IsDeviceAudioCaptureRequestedAndAllowed() const;
124  bool IsDeviceVideoCaptureRequestedAndAllowed() const;
125
126  // Returns true if media capture device is allowed to be used. This could
127  // return false when tab goes to background.
128  bool IsCaptureDeviceRequestAllowed() const;
129
130  content::WebContents* web_contents_;
131
132  // The owner of this class needs to make sure it does not outlive the profile.
133  Profile* profile_;
134
135  // Weak pointer to the tab specific content settings of the tab for which the
136  // MediaStreamDevicesController was created. The tab specific content
137  // settings are associated with a the web contents of the tab. The
138  // MediaStreamDeviceController must not outlive the web contents for which it
139  // was created.
140  TabSpecificContentSettings* content_settings_;
141
142  // The original request for access to devices.
143  const content::MediaStreamRequest request_;
144
145  // The callback that needs to be Run to notify WebRTC of whether access to
146  // audio/video devices was granted or not.
147  content::MediaResponseCallback callback_;
148
149  // Holds the requested media types and the permission for each type. It is
150  // passed to the tab specific content settings when the permissions have been
151  // resolved. Currently only used by MEDIA_DEVICE_AUDIO_CAPTURE and
152  // MEDIA_DEVICE_VIDEO_CAPTURE since those are the only types that require
153  // updates in the settings.
154  MediaStreamTypeSettingsMap request_permissions_;
155
156  DISALLOW_COPY_AND_ASSIGN(MediaStreamDevicesController);
157};
158
159#endif  // CHROME_BROWSER_MEDIA_MEDIA_STREAM_DEVICES_CONTROLLER_H_
160