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_CONTENT_SETTINGS_PERMISSION_CONTEXT_BASE_H_
6 #define CHROME_BROWSER_CONTENT_SETTINGS_PERMISSION_CONTEXT_BASE_H_
8 #include "base/callback.h"
9 #include "base/containers/scoped_ptr_hash_map.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/weak_ptr.h"
12 #include "chrome/browser/ui/website_settings/permission_bubble_request.h"
13 #include "components/content_settings/core/common/content_settings.h"
14 #include "components/content_settings/core/common/content_settings_types.h"
15 #include "components/keyed_service/core/keyed_service.h"
18 class PermissionQueueController
;
19 class PermissionRequestID
;
26 typedef base::Callback
<void(bool)> BrowserPermissionCallback
;
28 // This base class contains common operations for granting permissions.
29 // It offers the following functionality:
30 // - Creates a bubble or infobar when a permission is needed
31 // - If accepted/denied the permission is saved in content settings for
32 // future uses (for the domain that requested it).
33 // - If dismissed the permission is not saved but it's considered denied for
35 // - In any case the BrowserPermissionCallback is executed once a decision
36 // about the permission is made by the user.
37 // The bare minimum you need to create a new permission request is
38 // - Define your new permission in the ContentSettingsType enum.
39 // - Create a class that inherits from PermissionContextBase and passes the
41 // - Inherit from PermissionInfobarDelegate and implement
43 // - Edit the PermissionBubbleRequestImpl methods to add the new text for
45 // - Hit several asserts for the missing plumbing and fix them :)
46 // After this you can override several other methods to customize behavior,
47 // in particular it is advised to override UpdateTabContext in order to manage
48 // the permission from the omnibox.
49 // See midi_permission_context.h/cc or push_permission_context.cc/h for some
52 class PermissionContextBase
: public KeyedService
{
54 PermissionContextBase(Profile
* profile
,
55 const ContentSettingsType permission_type
);
56 ~PermissionContextBase() override
;
58 // The renderer is requesting permission to push messages.
59 // When the answer to a permission request has been determined, |callback|
60 // should be called with the result.
61 virtual void RequestPermission(content::WebContents
* web_contents
,
62 const PermissionRequestID
& id
,
63 const GURL
& requesting_frame
,
65 const BrowserPermissionCallback
& callback
);
67 // Returns whether the permission has been granted, denied...
68 virtual ContentSetting
GetPermissionStatus(
69 const GURL
& requesting_origin
,
70 const GURL
& embedding_origin
) const;
72 // Withdraw an existing permission request, no op if the permission request
73 // was already cancelled by some other means.
74 virtual void CancelPermissionRequest(content::WebContents
* web_contents
,
75 const PermissionRequestID
& id
);
78 // Decide whether the permission should be granted.
79 // Calls PermissionDecided if permission can be decided non-interactively,
80 // or NotifyPermissionSet if permission decided by presenting an infobar.
81 virtual void DecidePermission(content::WebContents
* web_contents
,
82 const PermissionRequestID
& id
,
83 const GURL
& requesting_origin
,
84 const GURL
& embedding_origin
,
86 const BrowserPermissionCallback
& callback
);
88 // Called when permission is granted without interactively asking the user.
89 void PermissionDecided(const PermissionRequestID
& id
,
90 const GURL
& requesting_origin
,
91 const GURL
& embedding_origin
,
92 const BrowserPermissionCallback
& callback
,
96 virtual void NotifyPermissionSet(const PermissionRequestID
& id
,
97 const GURL
& requesting_origin
,
98 const GURL
& embedding_origin
,
99 const BrowserPermissionCallback
& callback
,
103 // Implementors can override this method to update the icons on the
104 // url bar with the result of the new permission.
105 virtual void UpdateTabContext(const PermissionRequestID
& id
,
106 const GURL
& requesting_origin
,
109 // Return an instance of the infobar queue controller, creating it if needed.
110 PermissionQueueController
* GetQueueController();
112 // Returns the profile associated with this permission context.
113 Profile
* profile() const;
115 // Store the decided permission as a content setting.
116 // virtual since the permission might be stored with different restrictions
117 // (for example for desktop notifications).
118 virtual void UpdateContentSetting(const GURL
& requesting_origin
,
119 const GURL
& embedding_origin
,
123 // Called when a bubble is no longer used so it can be cleaned up.
124 void CleanUpBubble(const PermissionRequestID
& id
);
127 const ContentSettingsType permission_type_
;
128 scoped_ptr
<PermissionQueueController
> permission_queue_controller_
;
129 base::ScopedPtrHashMap
<std::string
, PermissionBubbleRequest
>
132 // Must be the last member, to ensure that it will be
133 // destroyed first, which will invalidate weak pointers
134 base::WeakPtrFactory
<PermissionContextBase
> weak_factory_
;
137 #endif // CHROME_BROWSER_CONTENT_SETTINGS_PERMISSION_CONTEXT_BASE_H_