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.
5 #ifndef CHROME_BROWSER_MEDIA_MEDIA_STREAM_DEVICES_CONTROLLER_H_
6 #define CHROME_BROWSER_MEDIA_MEDIA_STREAM_DEVICES_CONTROLLER_H_
11 #include "chrome/browser/ui/website_settings/permission_bubble_request.h"
12 #include "content/public/browser/web_contents_delegate.h"
15 class TabSpecificContentSettings
;
21 namespace user_prefs
{
22 class PrefRegistrySyncable
;
25 class MediaStreamDevicesController
: public PermissionBubbleRequest
{
27 // Permissions for media stream types.
31 MEDIA_BLOCKED_BY_POLICY
,
32 MEDIA_BLOCKED_BY_USER_SETTING
,
33 MEDIA_BLOCKED_BY_USER
,
36 struct MediaStreamTypeSettings
{
37 MediaStreamTypeSettings(Permission permission
,
38 const std::string
& requested_device_id
);
39 MediaStreamTypeSettings();
40 ~MediaStreamTypeSettings();
42 Permission permission
;
43 std::string requested_device_id
;
46 typedef std::map
<content::MediaStreamType
, MediaStreamTypeSettings
>
47 MediaStreamTypeSettingsMap
;
49 MediaStreamDevicesController(content::WebContents
* web_contents
,
50 const content::MediaStreamRequest
& request
,
51 const content::MediaResponseCallback
& callback
);
53 ~MediaStreamDevicesController() override
;
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.
59 // Registers the prefs backing the audio and video policies.
60 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable
* registry
);
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();
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
);
75 // PermissionBubbleRequest:
76 int GetIconID() const override
;
77 base::string16
GetMessageText() const override
;
78 base::string16
GetMessageTextFragment() const override
;
79 bool HasUserGesture() const override
;
80 GURL
GetRequestingHostname() const override
;
81 void PermissionGranted() override
;
82 void PermissionDenied() override
;
83 void Cancelled() override
;
84 void RequestFinished() override
;
87 // Returns true if the origin of the request has been granted the media
88 // access before, otherwise returns false.
89 bool IsRequestAllowedByDefault() const;
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();
98 // Returns true if the media section in content settings is set to
99 // |CONTENT_SETTING_BLOCK|, otherwise returns false.
100 bool IsDefaultMediaAccessBlocked() const;
102 // Returns true if the origin is a secure scheme, otherwise returns false.
103 bool IsSchemeSecure() const;
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;
109 // Notifies the content setting UI that the media stream access request or
110 // part of the request is accepted.
111 void NotifyUIRequestAccepted() const;
113 // Notifies the content setting UI that the media stream access request or
114 // part of the request is denied.
115 void NotifyUIRequestDenied();
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
123 bool IsDeviceAudioCaptureRequestedAndAllowed() const;
124 bool IsDeviceVideoCaptureRequestedAndAllowed() const;
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;
130 content::WebContents
* web_contents_
;
132 // The owner of this class needs to make sure it does not outlive the profile.
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
140 TabSpecificContentSettings
* content_settings_
;
142 // The original request for access to devices.
143 const content::MediaStreamRequest request_
;
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_
;
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_
;
156 DISALLOW_COPY_AND_ASSIGN(MediaStreamDevicesController
);
159 #endif // CHROME_BROWSER_MEDIA_MEDIA_STREAM_DEVICES_CONTROLLER_H_