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/memory/weak_ptr.h"
11 #include "chrome/browser/ui/website_settings/permission_bubble_view.h"
12 #include "content/public/browser/web_contents_observer.h"
13 #include "content/public/browser/web_contents_user_data.h"
15 class PermissionBubbleRequest;
17 // Provides access to permissions bubbles. Allows clients to add a request
18 // callback interface to the existing permission bubble configuration.
19 // Depending on the situation and policy, that may add new UI to an existing
20 // permission bubble, create and show a new permission bubble, or provide no
21 // visible UI action at all. (In that case, the request will be immediately
22 // informed that the permission request failed.)
24 // A PermissionBubbleManager is associated with a particular WebContents.
25 // Requests attached to a particular WebContents' PBM must outlive it.
27 // The PermissionBubbleManager should be addressed on the UI thread.
28 class PermissionBubbleManager
29 : public content::WebContentsObserver,
30 public content::WebContentsUserData<PermissionBubbleManager>,
31 public PermissionBubbleView::Delegate {
33 // Return the flag-driven enabled state of permissions bubbles.
34 static bool Enabled();
36 virtual ~PermissionBubbleManager();
38 // Adds a new request to the permission bubble. Ownership of the request
39 // remains with the caller. The caller must arrange for the request to
40 // outlive the PermissionBubbleManager. If a bubble is visible when this
41 // call is made, the request will be queued up and shown after the current
42 // bubble closes. A request with message text identical to an outstanding
43 // request will receive a RequestFinished call immediately and not be added.
44 virtual void AddRequest(PermissionBubbleRequest* request);
46 // Cancels an outstanding request. This may have different effects depending
47 // on what is going on with the bubble. If the request is pending, it will be
48 // removed and never shown. If the request is showing, it will continue to be
49 // shown, but the user's action won't be reported back to the request object.
50 // In some circumstances, we can remove the request from the bubble, and may
51 // do so. The request will have RequestFinished executed on it if it is found,
52 // at which time the caller is free to delete the request.
53 virtual void CancelRequest(PermissionBubbleRequest* request);
55 // Sets the active view for the permission bubble. If this is NULL, it
56 // means any existing permission bubble can no longer be shown. Does not
57 // take ownership of the view.
58 virtual void SetView(PermissionBubbleView* view) OVERRIDE;
61 friend class PermissionBubbleManagerTest;
62 friend class DownloadRequestLimiterTest;
63 friend class content::WebContentsUserData<PermissionBubbleManager>;
65 explicit PermissionBubbleManager(content::WebContents* web_contents);
67 // WebContentsObserver:
69 // TODO(leng): Finalize policy for permission requests with iFrames.
70 // DocumentLoadedInFrame() might be needed as well.
71 virtual void DocumentOnLoadCompletedInMainFrame() OVERRIDE;
73 // If a page on which permissions requests are pending is navigated,
74 // they will be finalized as if canceled by the user.
75 virtual void NavigationEntryCommitted(
76 const content::LoadCommittedDetails& details) OVERRIDE;
77 virtual void WebContentsDestroyed() OVERRIDE;
79 // PermissionBubbleView::Delegate:
80 virtual void ToggleAccept(int request_index, bool new_value) OVERRIDE;
81 virtual void SetCustomizationMode() OVERRIDE;
82 virtual void Accept() OVERRIDE;
83 virtual void Deny() OVERRIDE;
84 virtual void Closing() OVERRIDE;
88 // Finalize the pending permissions request.
89 void FinalizeBubble();
91 // Cancel any pending requests. This is called if the WebContents
92 // on which permissions calls are pending is destroyed or navigated away
93 // from the requesting page.
94 void CancelPendingQueue();
96 // Whether or not we are showing the bubble in this tab.
99 // Set to the UI surface to be used to display the permissions requests.
100 PermissionBubbleView* view_;
102 std::vector<PermissionBubbleRequest*> requests_;
103 std::vector<PermissionBubbleRequest*> queued_requests_;
105 // URL of the main frame in the WebContents to which this manager is attached.
106 // TODO(gbillock): if there are iframes in the page, we need to deal with it.
108 bool request_url_has_loaded_;
110 std::vector<bool> accept_states_;
111 bool customization_mode_;
113 base::WeakPtrFactory<PermissionBubbleManager> weak_factory_;
116 #endif // CHROME_BROWSER_UI_WEBSITE_SETTINGS_PERMISSION_BUBBLE_MANAGER_H_