1 // Copyright 2014 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_UI_WEBSITE_SETTINGS_PERMISSION_BUBBLE_MANAGER_H_
6 #define CHROME_BROWSER_UI_WEBSITE_SETTINGS_PERMISSION_BUBBLE_MANAGER_H_
10 #include "base/gtest_prod_util.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/observer_list.h"
13 #include "chrome/browser/ui/website_settings/permission_bubble_view.h"
14 #include "content/public/browser/web_contents_observer.h"
15 #include "content/public/browser/web_contents_user_data.h"
17 class PermissionBubbleRequest
;
19 // Provides access to permissions bubbles. Allows clients to add a request
20 // callback interface to the existing permission bubble configuration.
21 // Depending on the situation and policy, that may add new UI to an existing
22 // permission bubble, create and show a new permission bubble, or provide no
23 // visible UI action at all. (In that case, the request will be immediately
24 // informed that the permission request failed.)
26 // A PermissionBubbleManager is associated with a particular WebContents.
27 // Requests attached to a particular WebContents' PBM must outlive it.
29 // The PermissionBubbleManager should be addressed on the UI thread.
30 class PermissionBubbleManager
31 : public content::WebContentsObserver
,
32 public content::WebContentsUserData
<PermissionBubbleManager
>,
33 public PermissionBubbleView::Delegate
{
38 virtual void OnBubbleAdded();
41 enum AutoResponseType
{
48 // Return the enabled state of permissions bubbles.
49 // Controlled by a flag and FieldTrial.
50 static bool Enabled();
52 ~PermissionBubbleManager() override
;
54 // Adds a new request to the permission bubble. Ownership of the request
55 // remains with the caller. The caller must arrange for the request to
56 // outlive the PermissionBubbleManager. If a bubble is visible when this
57 // call is made, the request will be queued up and shown after the current
58 // bubble closes. A request with message text identical to an outstanding
59 // request will receive a RequestFinished call immediately and not be added.
60 virtual void AddRequest(PermissionBubbleRequest
* request
);
62 // Cancels an outstanding request. This may have different effects depending
63 // on what is going on with the bubble. If the request is pending, it will be
64 // removed and never shown. If the request is showing, it will continue to be
65 // shown, but the user's action won't be reported back to the request object.
66 // In some circumstances, we can remove the request from the bubble, and may
67 // do so. The request will have RequestFinished executed on it if it is found,
68 // at which time the caller is free to delete the request.
69 virtual void CancelRequest(PermissionBubbleRequest
* request
);
74 // Will show a permission bubble if there is a pending permission request on
75 // the web contents that the PermissionBubbleManager belongs to.
76 void DisplayPendingRequests();
78 // Will reposition the bubble (may change parent if necessary).
79 void UpdateAnchorPosition();
81 // True if a permission bubble is currently visible.
82 // TODO(hcarmona): Remove this as part of the bubble API work.
83 bool IsBubbleVisible();
85 // Get the native window of the bubble.
86 // TODO(hcarmona): Remove this as part of the bubble API work.
87 gfx::NativeWindow
GetBubbleWindow();
89 // Controls whether incoming permission requests require user gestures.
90 // If |required| is false, requests will be displayed as soon as they come in.
91 // If |required| is true, requests will be silently queued until a request
92 // comes in with a user gesture.
93 void RequireUserGesture(bool required
);
95 // For observing the status of the permission bubble manager.
96 void AddObserver(Observer
* observer
);
97 void RemoveObserver(Observer
* observer
);
99 // Do NOT use this methods in production code. Use this methods in browser
100 // tests that need to accept or deny permissions when requested in
101 // JavaScript. Your test needs to set this appropriately, and then the bubble
102 // will proceed as desired as soon as Show() is called.
103 void set_auto_response_for_test(AutoResponseType response
) {
104 auto_response_for_test_
= response
;
108 // TODO(felt): Update testing so that it doesn't involve a lot of friends.
109 friend class DownloadRequestLimiterTest
;
110 friend class GeolocationBrowserTest
;
111 friend class GeolocationPermissionContextTests
;
112 friend class MockPermissionBubbleView
;
113 friend class PermissionBubbleManagerTest
;
114 friend class PermissionContextBaseTests
;
115 friend class content::WebContentsUserData
<PermissionBubbleManager
>;
116 FRIEND_TEST_ALL_PREFIXES(DownloadTest
, TestMultipleDownloadsBubble
);
118 explicit PermissionBubbleManager(content::WebContents
* web_contents
);
120 // WebContentsObserver:
121 void DidNavigateMainFrame(
122 const content::LoadCommittedDetails
& details
,
123 const content::FrameNavigateParams
& params
) override
;
124 void DocumentOnLoadCompletedInMainFrame() override
;
125 void DocumentLoadedInFrame(
126 content::RenderFrameHost
* render_frame_host
) override
;
127 void WebContentsDestroyed() override
;
129 // PermissionBubbleView::Delegate:
130 void ToggleAccept(int request_index
, bool new_value
) override
;
131 void Accept() override
;
132 void Deny() override
;
133 void Closing() override
;
135 // Posts a task which will allow the bubble to become visible if it is needed.
136 void ScheduleShowBubble();
138 // Shows the bubble if it is not already visible and there are pending
140 void TriggerShowBubble();
142 // Finalize the pending permissions request.
143 void FinalizeBubble();
145 // Cancel any pending requests. This is called if the WebContents
146 // on which permissions calls are pending is destroyed or navigated away
147 // from the requesting page.
148 void CancelPendingQueues();
150 // Returns whether or not |request| has already been added to |queue|.
151 // |same_object| must be non-null. It will be set to true if |request|
152 // is the same object as an existing request in |queue|, false otherwise.
153 bool ExistingRequest(PermissionBubbleRequest
* request
,
154 const std::vector
<PermissionBubbleRequest
*>& queue
,
157 // Returns true if |queue| contains a request which was generated by a user
158 // gesture. Returns false otherwise.
159 bool HasUserGestureRequest(
160 const std::vector
<PermissionBubbleRequest
*>& queue
);
162 void NotifyBubbleAdded();
164 void DoAutoResponseForTesting();
166 // Whether to delay displaying the bubble until a request with a user gesture.
167 // False by default, unless RequireUserGesture(bool) changes the value.
168 bool require_user_gesture_
;
170 // Factory to be used to create views when needed.
171 PermissionBubbleView::Factory view_factory_
;
173 // The UI surface to be used to display the permissions requests.
174 scoped_ptr
<PermissionBubbleView
> view_
;
176 std::vector
<PermissionBubbleRequest
*> requests_
;
177 std::vector
<PermissionBubbleRequest
*> queued_requests_
;
178 std::vector
<PermissionBubbleRequest
*> queued_frame_requests_
;
180 // URL of the main frame in the WebContents to which this manager is attached.
181 // TODO(gbillock): if there are iframes in the page, we need to deal with it.
183 bool main_frame_has_fully_loaded_
;
185 std::vector
<bool> accept_states_
;
187 base::ObserverList
<Observer
> observer_list_
;
188 AutoResponseType auto_response_for_test_
;
190 base::WeakPtrFactory
<PermissionBubbleManager
> weak_factory_
;
193 #endif // CHROME_BROWSER_UI_WEBSITE_SETTINGS_PERMISSION_BUBBLE_MANAGER_H_