OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_SERVICES_GCM_PERMISSION_CONTEXT_BASE_H_ | 5 #ifndef CHROME_BROWSER_CONTENT_SETTINGS_PERMISSION_CONTEXT_BASE_H_ |
6 #define CHROME_BROWSER_SERVICES_GCM_PERMISSION_CONTEXT_BASE_H_ | 6 #define CHROME_BROWSER_CONTENT_SETTINGS_PERMISSION_CONTEXT_BASE_H_ |
7 | 7 |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/containers/scoped_ptr_hash_map.h" | 9 #include "base/containers/scoped_ptr_hash_map.h" |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
11 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
12 #include "chrome/browser/ui/website_settings/permission_bubble_request.h" | 12 #include "chrome/browser/ui/website_settings/permission_bubble_request.h" |
13 #include "chrome/common/content_settings_types.h" | 13 #include "chrome/common/content_settings_types.h" |
14 #include "components/keyed_service/core/keyed_service.h" | 14 #include "components/keyed_service/core/keyed_service.h" |
15 #include "url/gurl.h" | 15 #include "url/gurl.h" |
16 | 16 |
17 class PermissionQueueController; | 17 class PermissionQueueController; |
18 class PermissionRequestID; | 18 class PermissionRequestID; |
19 class Profile; | 19 class Profile; |
20 | 20 |
21 namespace content { | 21 namespace content { |
22 class WebContents; | 22 class WebContents; |
23 } | 23 } |
24 | 24 |
25 typedef base::Callback<void(bool)> BrowserPermissionCallback; | 25 typedef base::Callback<void(bool)> BrowserPermissionCallback; |
26 | 26 |
27 // TODO(miguelg): Move this out of gcm into a generic place and make | 27 // This base class contains common operations for granting permissions. |
28 // Midi permissions and others use it. | 28 // It offers the following functionality: |
29 namespace gcm { | 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 |
| 33 // this one request |
| 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 |
| 39 // new permission. |
| 40 // - Inherit from PermissionInfobarDelegate and implement |
| 41 // |GetMessageText| |
| 42 // - Edit the PermissionBubbleRequestImpl methods to add the new text for |
| 43 // the bubble. |
| 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 |
| 49 // examples. |
30 | 50 |
31 // This base class contains common operations for granting permissions. | |
32 // It is spit out of Midi and Push and will be moved to a common place | |
33 // so it can be used by both classes (and eventually others) in a separate | |
34 // patch. | |
35 // It supports both infobars and bubbles, but it handles them differently. | |
36 // For bubbles, it manages the life cycle of permission request and persists the | |
37 // permission choices when stated by the user. | |
38 // For infobars however all that logic is managed by the internal | |
39 // PermissionQueueController object. | |
40 class PermissionContextBase : public KeyedService { | 51 class PermissionContextBase : public KeyedService { |
41 public: | 52 public: |
42 PermissionContextBase(Profile* profile, | 53 PermissionContextBase(Profile* profile, |
43 const ContentSettingsType permission_type); | 54 const ContentSettingsType permission_type); |
44 virtual ~PermissionContextBase(); | 55 virtual ~PermissionContextBase(); |
45 | 56 |
46 // The renderer is requesting permission to push messages. | 57 // The renderer is requesting permission to push messages. |
47 // When the answer to a permission request has been determined, |callback| | 58 // When the answer to a permission request has been determined, |callback| |
48 // should be called with the result. | 59 // should be called with the result. |
49 virtual void RequestPermission(content::WebContents* web_contents, | 60 virtual void RequestPermission(content::WebContents* web_contents, |
50 const PermissionRequestID& id, | 61 const PermissionRequestID& id, |
51 const GURL& requesting_frame, | 62 const GURL& requesting_frame, |
52 bool user_gesture, | 63 bool user_gesture, |
53 const BrowserPermissionCallback& callback); | 64 const BrowserPermissionCallback& callback); |
54 | 65 |
55 protected: | 66 protected: |
56 // Decide whether the permission should be granted. | 67 // Decide whether the permission should be granted. |
57 // Calls PermissionDecided if permission can be decided non-interactively, | 68 // Calls PermissionDecided if permission can be decided non-interactively, |
58 // or NotifyPermissionSet if permission decided by presenting an infobar. | 69 // or NotifyPermissionSet if permission decided by presenting an infobar. |
59 void DecidePermission(content::WebContents* web_contents, | 70 void DecidePermission(content::WebContents* web_contents, |
60 const PermissionRequestID& id, | 71 const PermissionRequestID& id, |
61 const GURL& requesting_frame, | 72 const GURL& requesting_origin, |
62 const GURL& embedder, | 73 const GURL& embedder_origin, |
63 bool user_gesture, | 74 bool user_gesture, |
64 const BrowserPermissionCallback& callback); | 75 const BrowserPermissionCallback& callback); |
65 | 76 |
66 // Called when permission is granted without interactively asking the user. | 77 // Called when permission is granted without interactively asking the user. |
67 void PermissionDecided(const PermissionRequestID& id, | 78 void PermissionDecided(const PermissionRequestID& id, |
68 const GURL& requesting_frame, | 79 const GURL& requesting_origin, |
69 const GURL& embedder, | 80 const GURL& embedder_origin, |
70 const BrowserPermissionCallback& callback, | 81 const BrowserPermissionCallback& callback, |
71 bool allowed); | 82 bool allowed); |
72 | 83 |
73 void NotifyPermissionSet(const PermissionRequestID& id, | 84 void NotifyPermissionSet(const PermissionRequestID& id, |
74 const GURL& requesting_frame, | 85 const GURL& requesting_origin, |
75 const GURL& embedder, | 86 const GURL& embedder_origin, |
76 const BrowserPermissionCallback& callback, | 87 const BrowserPermissionCallback& callback, |
77 bool persist, | 88 bool persist, |
78 bool allowed); | 89 bool allowed); |
79 | 90 |
80 // Implementors can override this method to update the icons on the | 91 // Implementors can override this method to update the icons on the |
81 // url bar with the result of the new permission. | 92 // url bar with the result of the new permission. |
82 virtual void UpdateTabContext(const PermissionRequestID& id, | 93 virtual void UpdateTabContext(const PermissionRequestID& id, |
83 const GURL& requesting_frame, | 94 const GURL& requesting_origin, |
84 bool allowed) {} | 95 bool allowed) {} |
85 | 96 |
86 // Return an instance of the infobar queue controller, creating it if needed. | 97 // Return an instance of the infobar queue controller, creating it if needed. |
87 PermissionQueueController* GetQueueController(); | 98 PermissionQueueController* GetQueueController(); |
88 | 99 |
89 private: | 100 private: |
90 void UpdateContentSetting( | 101 void UpdateContentSetting( |
91 const GURL& requesting_frame, | 102 const GURL& requesting_origin, |
92 const GURL& embedder, | 103 const GURL& embedder_origin, |
93 bool allowed); | 104 bool allowed); |
94 | 105 |
95 // Called when a bubble is no longer used so it can be cleaned up. | 106 // Called when a bubble is no longer used so it can be cleaned up. |
96 void CleanUpBubble(const PermissionRequestID& id); | 107 void CleanUpBubble(const PermissionRequestID& id); |
97 | 108 |
98 Profile* profile_; | 109 Profile* profile_; |
99 const ContentSettingsType permission_type_; | 110 const ContentSettingsType permission_type_; |
100 base::WeakPtrFactory<PermissionContextBase> weak_factory_; | 111 base::WeakPtrFactory<PermissionContextBase> weak_factory_; |
101 scoped_ptr<PermissionQueueController> permission_queue_controller_; | 112 scoped_ptr<PermissionQueueController> permission_queue_controller_; |
102 base::ScopedPtrHashMap<std::string, PermissionBubbleRequest> | 113 base::ScopedPtrHashMap<std::string, PermissionBubbleRequest> |
103 pending_bubbles_; | 114 pending_bubbles_; |
104 }; | 115 }; |
105 | 116 |
106 } // namespace gcm | 117 #endif // CHROME_BROWSER_CONTENT_SETTINGS_PERMISSION_CONTEXT_BASE_H_ |
107 | |
108 #endif // CHROME_BROWSER_SERVICES_GCM_PERMISSION_CONTEXT_BASE_H_ | |
OLD | NEW |