Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / guest_view / web_view / web_view_permission_helper.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_GUEST_VIEW_WEB_VIEW_WEB_VIEW_PERMISSION_HELPER_H_
6 #define CHROME_BROWSER_GUEST_VIEW_WEB_VIEW_WEB_VIEW_PERMISSION_HELPER_H_
7
8 #include "base/memory/weak_ptr.h"
9 #include "base/metrics/user_metrics_action.h"
10 #include "chrome/browser/guest_view/web_view/web_view_permission_types.h"
11 #include "content/public/browser/web_contents.h"
12 #include "content/public/browser/web_contents_observer.h"
13 #include "content/public/common/media_stream_request.h"
14 #include "extensions/browser/guest_view/guest_view_constants.h"
15
16 using base::UserMetricsAction;
17
18 namespace extensions {
19
20 class WebViewGuest;
21 class WebViewPermissionHelperDelegate;
22
23 // WebViewPermissionHelper manages <webview> permission requests. This helper
24 // class is owned by WebViewGuest. Its purpose is to request permission for
25 // various operations from the <webview> embedder, and reply back via callbacks
26 // to the callers on a response from the embedder.
27 class WebViewPermissionHelper
28       : public content::WebContentsObserver {
29  public:
30   explicit WebViewPermissionHelper(WebViewGuest* guest);
31   virtual ~WebViewPermissionHelper();
32   typedef base::Callback<
33       void(bool /* allow */, const std::string& /* user_input */)>
34       PermissionResponseCallback;
35
36   // A map to store the callback for a request keyed by the request's id.
37   struct PermissionResponseInfo {
38     PermissionResponseCallback callback;
39     WebViewPermissionType permission_type;
40     bool allowed_by_default;
41     PermissionResponseInfo();
42     PermissionResponseInfo(const PermissionResponseCallback& callback,
43                            WebViewPermissionType permission_type,
44                            bool allowed_by_default);
45     ~PermissionResponseInfo();
46   };
47
48   typedef std::map<int, PermissionResponseInfo> RequestMap;
49
50   int RequestPermission(WebViewPermissionType permission_type,
51                         const base::DictionaryValue& request_info,
52                         const PermissionResponseCallback& callback,
53                         bool allowed_by_default);
54
55   static WebViewPermissionHelper* FromWebContents(
56       content::WebContents* web_contents);
57   static WebViewPermissionHelper* FromFrameID(int render_process_id,
58                                               int render_frame_id);
59   void RequestMediaAccessPermission(
60       content::WebContents* source,
61       const content::MediaStreamRequest& request,
62       const content::MediaResponseCallback& callback);
63   void CanDownload(content::RenderViewHost* render_view_host,
64                    const GURL& url,
65                    const std::string& request_method,
66                    const base::Callback<void(bool)>& callback);
67   void RequestPointerLockPermission(bool user_gesture,
68                                     bool last_unlocked_by_target,
69                                     const base::Callback<void(bool)>& callback);
70
71   // Requests Geolocation Permission from the embedder.
72   void RequestGeolocationPermission(int bridge_id,
73                                     const GURL& requesting_frame,
74                                     bool user_gesture,
75                                     const base::Callback<void(bool)>& callback);
76   void CancelGeolocationPermissionRequest(int bridge_id);
77
78   void RequestFileSystemPermission(const GURL& url,
79                                    bool allowed_by_default,
80                                    const base::Callback<void(bool)>& callback);
81
82   // Called when file system access is requested by the guest content using the
83   // asynchronous HTML5 file system API. The request is plumbed through the
84   // <webview> permission request API. The request will be:
85   // - Allowed if the embedder explicitly allowed it.
86   // - Denied if the embedder explicitly denied.
87   // - Determined by the guest's content settings if the embedder does not
88   // perform an explicit action.
89   // If access was blocked due to the page's content settings,
90   // |blocked_by_policy| should be true, and this function should invoke
91   // OnContentBlocked.
92   void FileSystemAccessedAsync(int render_process_id,
93                                int render_frame_id,
94                                int request_id,
95                                const GURL& url,
96                                bool blocked_by_policy);
97
98   // Called when file system access is requested by the guest content using the
99   // synchronous HTML5 file system API in a worker thread or shared worker. The
100   // request is plumbed through the <webview> permission request API. The
101   // request will be:
102   // - Allowed if the embedder explicitly allowed it.
103   // - Denied if the embedder explicitly denied.
104   // - Determined by the guest's content settings if the embedder does not
105   // perform an explicit action.
106   // If access was blocked due to the page's content settings,
107   // |blocked_by_policy| should be true, and this function should invoke
108   // OnContentBlocked.
109   void FileSystemAccessedSync(int render_process_id,
110                               int render_frame_id,
111                               const GURL& url,
112                               bool blocked_by_policy,
113                               IPC::Message* reply_msg);
114
115   enum PermissionResponseAction { DENY, ALLOW, DEFAULT };
116
117   enum SetPermissionResult {
118     SET_PERMISSION_INVALID,
119     SET_PERMISSION_ALLOWED,
120     SET_PERMISSION_DENIED
121   };
122
123   // Responds to the permission request |request_id| with |action| and
124   // |user_input|. Returns whether there was a pending request for the provided
125   // |request_id|.
126   SetPermissionResult SetPermission(int request_id,
127                                     PermissionResponseAction action,
128                                     const std::string& user_input);
129
130   void CancelPendingPermissionRequest(int request_id);
131
132   WebViewGuest* web_view_guest() { return web_view_guest_; }
133
134  private:
135 #if defined(ENABLE_PLUGINS)
136   // content::WebContentsObserver implementation.
137   virtual bool OnMessageReceived(
138       const IPC::Message& message,
139       content::RenderFrameHost* render_frame_host) OVERRIDE;
140   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
141 #endif  // defined(ENABLE_PLUGINS)
142
143   // A counter to generate a unique request id for a permission request.
144   // We only need the ids to be unique for a given WebViewGuest.
145   int next_permission_request_id_;
146
147   WebViewPermissionHelper::RequestMap pending_permission_requests_;
148
149   scoped_ptr<extensions::WebViewPermissionHelperDelegate>
150       web_view_permission_helper_delegate_;
151
152   WebViewGuest* web_view_guest_;
153
154   base::WeakPtrFactory<WebViewPermissionHelper> weak_factory_;
155
156   DISALLOW_COPY_AND_ASSIGN(WebViewPermissionHelper);
157 };
158
159 }  // namespace extensions
160
161 #endif  // CHROME_BROWSER_GUEST_VIEW_WEB_VIEW_WEB_VIEW_PERMISSION_HELPER_H_