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 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 // 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 // Resets the permission to its default value.
73 virtual void ResetPermission(const GURL
& requesting_origin
,
74 const GURL
& embedding_origin
);
76 // Withdraw an existing permission request, no op if the permission request
77 // was already cancelled by some other means.
78 virtual void CancelPermissionRequest(content::WebContents
* web_contents
,
79 const PermissionRequestID
& id
);
82 // Decide whether the permission should be granted.
83 // Calls PermissionDecided if permission can be decided non-interactively,
84 // or NotifyPermissionSet if permission decided by presenting an infobar.
85 virtual void DecidePermission(content::WebContents
* web_contents
,
86 const PermissionRequestID
& id
,
87 const GURL
& requesting_origin
,
88 const GURL
& embedding_origin
,
90 const BrowserPermissionCallback
& callback
);
92 // Called when permission is granted without interactively asking the user.
93 void PermissionDecided(const PermissionRequestID
& id
,
94 const GURL
& requesting_origin
,
95 const GURL
& embedding_origin
,
96 const BrowserPermissionCallback
& callback
,
98 ContentSetting content_setting
);
100 virtual void NotifyPermissionSet(const PermissionRequestID
& id
,
101 const GURL
& requesting_origin
,
102 const GURL
& embedding_origin
,
103 const BrowserPermissionCallback
& callback
,
105 ContentSetting content_setting
);
107 // Implementors can override this method to update the icons on the
108 // url bar with the result of the new permission.
109 virtual void UpdateTabContext(const PermissionRequestID
& id
,
110 const GURL
& requesting_origin
,
113 // Return an instance of the infobar queue controller, creating it if needed.
114 PermissionQueueController
* GetQueueController();
116 // Returns the profile associated with this permission context.
117 Profile
* profile() const;
119 // Store the decided permission as a content setting.
120 // virtual since the permission might be stored with different restrictions
121 // (for example for desktop notifications).
122 virtual void UpdateContentSetting(const GURL
& requesting_origin
,
123 const GURL
& embedding_origin
,
124 ContentSetting content_setting
);
127 // Called when a bubble is no longer used so it can be cleaned up.
128 void CleanUpBubble(const PermissionRequestID
& id
);
131 const ContentSettingsType permission_type_
;
132 scoped_ptr
<PermissionQueueController
> permission_queue_controller_
;
133 base::ScopedPtrHashMap
<std::string
, PermissionBubbleRequest
>
136 // Must be the last member, to ensure that it will be
137 // destroyed first, which will invalidate weak pointers
138 base::WeakPtrFactory
<PermissionContextBase
> weak_factory_
;
141 #endif // CHROME_BROWSER_CONTENT_SETTINGS_PERMISSION_CONTEXT_BASE_H_