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 // Store the permission to use media devices for the origin of the request.
106 // This is triggered when the users deny the request or allow the request
108 void StorePermission(bool allowed
) const;
110 // Notifies the content setting UI that the media stream access request or
111 // part of the request is accepted.
112 void NotifyUIRequestAccepted() const;
114 // Notifies the content setting UI that the media stream access request or
115 // part of the request is denied.
116 void NotifyUIRequestDenied();
118 // Return true if the type has been requested and permission is currently set
119 // to allowed. Note that it does not reflect the final permission decision.
120 // This function is called during the filtering steps to check if the type has
121 // been blocked yet or not and the permission may be changed to blocked during
122 // these filterings. See also the initialization in the constructor and
124 bool IsDeviceAudioCaptureRequestedAndAllowed() const;
125 bool IsDeviceVideoCaptureRequestedAndAllowed() const;
127 // Returns true if media capture device is allowed to be used. This could
128 // return false when tab goes to background.
129 bool IsCaptureDeviceRequestAllowed() const;
131 content::WebContents
* web_contents_
;
133 // The owner of this class needs to make sure it does not outlive the profile.
136 // Weak pointer to the tab specific content settings of the tab for which the
137 // MediaStreamDevicesController was created. The tab specific content
138 // settings are associated with a the web contents of the tab. The
139 // MediaStreamDeviceController must not outlive the web contents for which it
141 TabSpecificContentSettings
* content_settings_
;
143 // The original request for access to devices.
144 const content::MediaStreamRequest request_
;
146 // The callback that needs to be Run to notify WebRTC of whether access to
147 // audio/video devices was granted or not.
148 content::MediaResponseCallback callback_
;
150 // Holds the requested media types and the permission for each type. It is
151 // passed to the tab specific content settings when the permissions have been
152 // resolved. Currently only used by MEDIA_DEVICE_AUDIO_CAPTURE and
153 // MEDIA_DEVICE_VIDEO_CAPTURE since those are the only types that require
154 // updates in the settings.
155 MediaStreamTypeSettingsMap request_permissions_
;
157 DISALLOW_COPY_AND_ASSIGN(MediaStreamDevicesController
);
160 #endif // CHROME_BROWSER_MEDIA_MEDIA_STREAM_DEVICES_CONTROLLER_H_