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_PERMISSIONS_PERMISSION_CONTEXT_BASE_H_
6 #define CHROME_BROWSER_PERMISSIONS_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 using BrowserPermissionCallback
= base::Callback
<void(ContentSetting
)>;
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 // It is mandatory to override IsRestrictedToSecureOrigin.
50 // See midi_permission_context.h/cc or push_permission_context.cc/h for some
53 class PermissionContextBase
: public KeyedService
{
55 PermissionContextBase(Profile
* profile
,
56 const ContentSettingsType permission_type
);
57 ~PermissionContextBase() override
;
59 // The renderer is requesting permission to push messages.
60 // When the answer to a permission request has been determined, |callback|
61 // should be called with the result.
62 virtual void RequestPermission(content::WebContents
* web_contents
,
63 const PermissionRequestID
& id
,
64 const GURL
& requesting_frame
,
66 const BrowserPermissionCallback
& callback
);
68 // Returns whether the permission has been granted, denied...
69 virtual ContentSetting
GetPermissionStatus(
70 const GURL
& requesting_origin
,
71 const GURL
& embedding_origin
) const;
73 // Resets the permission to its default value.
74 virtual void ResetPermission(const GURL
& requesting_origin
,
75 const GURL
& embedding_origin
);
77 // Withdraw an existing permission request, no op if the permission request
78 // was already cancelled by some other means.
79 virtual void CancelPermissionRequest(content::WebContents
* web_contents
,
80 const PermissionRequestID
& id
);
83 // Decide whether the permission should be granted.
84 // Calls PermissionDecided if permission can be decided non-interactively,
85 // or NotifyPermissionSet if permission decided by presenting an infobar.
86 virtual void DecidePermission(content::WebContents
* web_contents
,
87 const PermissionRequestID
& id
,
88 const GURL
& requesting_origin
,
89 const GURL
& embedding_origin
,
91 const BrowserPermissionCallback
& callback
);
93 // Called when permission is granted without interactively asking the user.
94 void PermissionDecided(const PermissionRequestID
& id
,
95 const GURL
& requesting_origin
,
96 const GURL
& embedding_origin
,
97 const BrowserPermissionCallback
& callback
,
99 ContentSetting content_setting
);
101 virtual void NotifyPermissionSet(const PermissionRequestID
& id
,
102 const GURL
& requesting_origin
,
103 const GURL
& embedding_origin
,
104 const BrowserPermissionCallback
& callback
,
106 ContentSetting content_setting
);
108 // Implementors can override this method to update the icons on the
109 // url bar with the result of the new permission.
110 virtual void UpdateTabContext(const PermissionRequestID
& id
,
111 const GURL
& requesting_origin
,
114 // Return an instance of the infobar queue controller, creating it if needed.
115 PermissionQueueController
* GetQueueController();
117 // Returns the profile associated with this permission context.
118 Profile
* profile() const;
120 // Store the decided permission as a content setting.
121 // virtual since the permission might be stored with different restrictions
122 // (for example for desktop notifications).
123 virtual void UpdateContentSetting(const GURL
& requesting_origin
,
124 const GURL
& embedding_origin
,
125 ContentSetting content_setting
);
127 // Whether the permission should be restricted to secure origins.
128 virtual bool IsRestrictedToSecureOrigins() const = 0;
131 // Called when a bubble is no longer used so it can be cleaned up.
132 void CleanUpBubble(const PermissionRequestID
& id
);
135 const ContentSettingsType permission_type_
;
136 scoped_ptr
<PermissionQueueController
> permission_queue_controller_
;
137 base::ScopedPtrHashMap
<std::string
, scoped_ptr
<PermissionBubbleRequest
>>
140 // Must be the last member, to ensure that it will be
141 // destroyed first, which will invalidate weak pointers
142 base::WeakPtrFactory
<PermissionContextBase
> weak_factory_
;
145 #endif // CHROME_BROWSER_PERMISSIONS_PERMISSION_CONTEXT_BASE_H_