Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / content_settings / permission_context_base.h
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.
4
5 #ifndef CHROME_BROWSER_CONTENT_SETTINGS_PERMISSION_CONTEXT_BASE_H_
6 #define CHROME_BROWSER_CONTENT_SETTINGS_PERMISSION_CONTEXT_BASE_H_
7
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"
15 #include "url/gurl.h"
16
17 class PermissionQueueController;
18 class PermissionRequestID;
19 class Profile;
20
21 namespace content {
22 class WebContents;
23 }
24
25 typedef base::Callback<void(bool)> BrowserPermissionCallback;
26
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
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.
50
51 class PermissionContextBase : public KeyedService {
52  public:
53   PermissionContextBase(Profile* profile,
54                         const ContentSettingsType permission_type);
55   virtual ~PermissionContextBase();
56
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,
63                                  bool user_gesture,
64                                  const BrowserPermissionCallback& callback);
65
66  protected:
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,
74                         bool user_gesture,
75                         const BrowserPermissionCallback& callback);
76
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,
82                          bool persist,
83                          bool allowed);
84
85   void NotifyPermissionSet(const PermissionRequestID& id,
86                            const GURL& requesting_origin,
87                            const GURL& embedder_origin,
88                            const BrowserPermissionCallback& callback,
89                            bool persist,
90                            bool allowed);
91
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,
96                                 bool allowed) {}
97
98   // Return an instance of the infobar queue controller, creating it if needed.
99   PermissionQueueController* GetQueueController();
100
101  private:
102   void UpdateContentSetting(
103       const GURL& requesting_origin,
104       const GURL& embedder_origin,
105       bool allowed);
106
107   // Called when a bubble is no longer used so it can be cleaned up.
108   void CleanUpBubble(const PermissionRequestID& id);
109
110   Profile* profile_;
111   const ContentSettingsType permission_type_;
112   base::WeakPtrFactory<PermissionContextBase> weak_factory_;
113   scoped_ptr<PermissionQueueController> permission_queue_controller_;
114   base::ScopedPtrHashMap<std::string, PermissionBubbleRequest>
115       pending_bubbles_;
116 };
117
118 #endif  // CHROME_BROWSER_CONTENT_SETTINGS_PERMISSION_CONTEXT_BASE_H_