• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 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_CONTENT_SETTINGS_PERMISSION_QUEUE_CONTROLLER_H_
6 #define CHROME_BROWSER_CONTENT_SETTINGS_PERMISSION_QUEUE_CONTROLLER_H_
7 
8 #include "base/bind.h"
9 #include "chrome/common/content_settings_types.h"
10 #include "content/public/browser/notification_observer.h"
11 #include "content/public/browser/notification_registrar.h"
12 
13 class GURL;
14 class PermissionRequestID;
15 class InfoBarService;
16 class Profile;
17 
18 // This class controls an infobar queue per profile, and it's used by
19 // GeolocationPermissionContext, and so on.
20 // An alternate approach would be to have this queue per tab, and use
21 // notifications to broadcast when permission is set / listen to notification to
22 // cancel pending requests. This may be specially useful if there are other
23 // things listening for such notifications.
24 // For the time being this class is self-contained and it doesn't seem pulling
25 // the notification infrastructure would simplify.
26 class PermissionQueueController : public content::NotificationObserver {
27  public:
28   typedef base::Callback<void(bool /* allowed */)> PermissionDecidedCallback;
29 
30   PermissionQueueController(Profile* profile, ContentSettingsType type);
31   virtual ~PermissionQueueController();
32 
33   // The InfoBar will be displayed immediately if the tab is not already
34   // displaying one, otherwise it'll be queued.
35   void CreateInfoBarRequest(const PermissionRequestID& id,
36                             const GURL& requesting_frame,
37                             const GURL& embedder,
38                             const std::string& accept_button_label,
39                             PermissionDecidedCallback callback);
40 
41   // Cancels a specific infobar request.
42   void CancelInfoBarRequest(const PermissionRequestID& id);
43 
44   // Called by the InfoBarDelegate to notify permission has been set.
45   // It'll notify and dismiss any other pending InfoBar request for the same
46   // |requesting_frame| and embedder.
47   void OnPermissionSet(const PermissionRequestID& id,
48                        const GURL& requesting_frame,
49                        const GURL& embedder,
50                        bool update_content_setting,
51                        bool allowed);
52 
53  protected:
54   // content::NotificationObserver:
55   virtual void Observe(int type,
56                        const content::NotificationSource& source,
57                        const content::NotificationDetails& details) OVERRIDE;
58 
59  private:
60   class PendingInfobarRequest;
61   class RequestEquals;
62 
63   typedef std::vector<PendingInfobarRequest> PendingInfobarRequests;
64 
65   // Returns true if a geolocation infobar is already visible for the tab
66   // corresponding to |id|.
67   bool AlreadyShowingInfoBarForTab(const PermissionRequestID& id) const;
68 
69   // Shows the next pending infobar for the tab corresponding to |id|, if any.
70   // Note that this may not be the pending request whose ID is |id| if other
71   // requests are higher in the queue.  If we can't show infobars because there
72   // is no InfoBarService for this tab, removes all queued requests for this
73   // tab.
74   void ShowQueuedInfoBarForTab(const PermissionRequestID& id);
75 
76   void ClearPendingInfobarRequestsForTab(const PermissionRequestID& id);
77 
78   void RegisterForInfoBarNotifications(InfoBarService* infobar_service);
79   void UnregisterForInfoBarNotifications(InfoBarService* infobar_service);
80 
81   void UpdateContentSetting(
82       const GURL& requesting_frame, const GURL& embedder, bool allowed);
83 
84   content::NotificationRegistrar registrar_;
85 
86   Profile* const profile_;
87   ContentSettingsType type_;
88   PendingInfobarRequests pending_infobar_requests_;
89   bool in_shutdown_;
90 
91   DISALLOW_COPY_AND_ASSIGN(PermissionQueueController);
92 };
93 
94 #endif  // CHROME_BROWSER_CONTENT_SETTINGS_PERMISSION_QUEUE_CONTROLLER_H_
95