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_types.h"
14 #include "components/keyed_service/core/keyed_service.h"
17 class PermissionQueueController;
18 class PermissionRequestID;
25 typedef base::Callback<void(bool)> BrowserPermissionCallback;
27 // This base class contains common operations for granting permissions.
28 // It offers the following functionality:
29 // - Creates a bubble or infobar when a permission is needed
30 // - If accepted/denied the permission is saved in content settings for
31 // future uses (for the domain that requested it).
32 // - If dismissed the permission is not saved but it's considered denied for
34 // - In any case the BrowserPermissionCallback is executed once a decision
35 // about the permission is made by the user.
36 // The bare minimum you need to create a new permission request is
37 // - Define your new permission in the ContentSettingsType enum.
38 // - Create a class that inherits from PermissionContextBase and passes the
40 // - Inherit from PermissionInfobarDelegate and implement
42 // - Edit the PermissionBubbleRequestImpl methods to add the new text for
44 // - Hit several asserts for the missing plumbing and fix them :)
45 // After this you can override several other methods to customize behavior,
46 // in particular it is advised to override UpdateTabContext in order to manage
47 // the permission from the omnibox.
48 // See midi_permission_context.h/cc or push_permission_context.cc/h for some
51 class PermissionContextBase : public KeyedService {
53 PermissionContextBase(Profile* profile,
54 const ContentSettingsType permission_type);
55 virtual ~PermissionContextBase();
57 // The renderer is requesting permission to push messages.
58 // When the answer to a permission request has been determined, |callback|
59 // should be called with the result.
60 virtual void RequestPermission(content::WebContents* web_contents,
61 const PermissionRequestID& id,
62 const GURL& requesting_frame,
64 const BrowserPermissionCallback& callback);
67 // Decide whether the permission should be granted.
68 // Calls PermissionDecided if permission can be decided non-interactively,
69 // or NotifyPermissionSet if permission decided by presenting an infobar.
70 void DecidePermission(content::WebContents* web_contents,
71 const PermissionRequestID& id,
72 const GURL& requesting_origin,
73 const GURL& embedder_origin,
75 const BrowserPermissionCallback& callback);
77 // Called when permission is granted without interactively asking the user.
78 void PermissionDecided(const PermissionRequestID& id,
79 const GURL& requesting_origin,
80 const GURL& embedder_origin,
81 const BrowserPermissionCallback& callback,
85 void NotifyPermissionSet(const PermissionRequestID& id,
86 const GURL& requesting_origin,
87 const GURL& embedder_origin,
88 const BrowserPermissionCallback& callback,
92 // Implementors can override this method to update the icons on the
93 // url bar with the result of the new permission.
94 virtual void UpdateTabContext(const PermissionRequestID& id,
95 const GURL& requesting_origin,
98 // Return an instance of the infobar queue controller, creating it if needed.
99 PermissionQueueController* GetQueueController();
102 void UpdateContentSetting(
103 const GURL& requesting_origin,
104 const GURL& embedder_origin,
107 // Called when a bubble is no longer used so it can be cleaned up.
108 void CleanUpBubble(const PermissionRequestID& id);
111 const ContentSettingsType permission_type_;
112 base::WeakPtrFactory<PermissionContextBase> weak_factory_;
113 scoped_ptr<PermissionQueueController> permission_queue_controller_;
114 base::ScopedPtrHashMap<std::string, PermissionBubbleRequest>
118 #endif // CHROME_BROWSER_CONTENT_SETTINGS_PERMISSION_CONTEXT_BASE_H_