Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / extensions / browser / guest_view / web_view / web_view_guest.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 EXTENSIONS_BROWSER_GUEST_VIEW_WEB_VIEW_WEB_VIEW_GUEST_H_
6 #define EXTENSIONS_BROWSER_GUEST_VIEW_WEB_VIEW_WEB_VIEW_GUEST_H_
7
8 #include <vector>
9
10 #include "base/observer_list.h"
11 #include "content/public/browser/javascript_dialog_manager.h"
12 #include "content/public/browser/notification_observer.h"
13 #include "content/public/browser/notification_registrar.h"
14 #include "extensions/browser/guest_view/guest_view.h"
15 #include "extensions/browser/guest_view/web_view/javascript_dialog_helper.h"
16 #include "extensions/browser/guest_view/web_view/web_view_find_helper.h"
17 #include "extensions/browser/guest_view/web_view/web_view_guest_delegate.h"
18 #include "extensions/browser/guest_view/web_view/web_view_permission_helper.h"
19 #include "extensions/browser/guest_view/web_view/web_view_permission_types.h"
20 #include "extensions/browser/script_executor.h"
21
22 namespace blink {
23 struct WebFindOptions;
24 }  // nanespace blink
25
26 namespace extensions {
27
28 class WebViewInternalFindFunction;
29
30 // A WebViewGuest provides the browser-side implementation of the <webview> API
31 // and manages the dispatch of <webview> extension events. WebViewGuest is
32 // created on attachment. That is, when a guest WebContents is associated with
33 // a particular embedder WebContents. This happens on either initial navigation
34 // or through the use of the New Window API, when a new window is attached to
35 // a particular <webview>.
36 class WebViewGuest : public GuestView<WebViewGuest>,
37                      public content::NotificationObserver {
38  public:
39   static GuestViewBase* Create(content::BrowserContext* browser_context,
40                                int guest_instance_id);
41
42   // For WebViewGuest, we create special guest processes, which host the
43   // tag content separately from the main application that embeds the tag.
44   // A <webview> can specify both the partition name and whether the storage
45   // for that partition should be persisted. Each tag gets a SiteInstance with
46   // a specially formatted URL, based on the application it is hosted by and
47   // the partition requested by it. The format for that URL is:
48   // chrome-guest://partition_domain/persist?partition_name
49   static bool GetGuestPartitionConfigForSite(const GURL& site,
50                                              std::string* partition_domain,
51                                              std::string* partition_name,
52                                              bool* in_memory);
53
54   // Returns guestview::kInstanceIDNone if |contents| does not correspond to a
55   // WebViewGuest.
56   static int GetViewInstanceId(content::WebContents* contents);
57
58   static const char Type[];
59
60   // Request navigating the guest to the provided |src| URL.
61   void NavigateGuest(const std::string& src, bool force_navigation);
62
63   // Shows the context menu for the guest.
64   // |items| acts as a filter. This restricts the current context's default
65   // menu items to contain only the items from |items|.
66   // |items| == NULL means no filtering will be applied.
67   void ShowContextMenu(
68       int request_id,
69       const WebViewGuestDelegate::MenuItemVector* items);
70
71   // Sets the frame name of the guest.
72   void SetName(const std::string& name);
73
74   // Set the zoom factor.
75   void SetZoom(double zoom_factor);
76
77   // Sets the transparency of the guest.
78   void SetAllowTransparency(bool allow);
79
80   // Loads a data URL with a specified base URL and virtual URL.
81   bool LoadDataWithBaseURL(const std::string& data_url,
82                            const std::string& base_url,
83                            const std::string& virtual_url,
84                            std::string* error);
85
86   // GuestViewBase implementation.
87   const char* GetAPINamespace() const override;
88   int GetTaskPrefix() const override;
89   void CreateWebContents(const std::string& embedder_extension_id,
90                          int embedder_render_process_id,
91                          const GURL& embedder_site_url,
92                          const base::DictionaryValue& create_params,
93                          const WebContentsCreatedCallback& callback) override;
94   void DidAttachToEmbedder() override;
95   void DidInitialize() override;
96   void DidStopLoading() override;
97   void EmbedderWillBeDestroyed() override;
98   void GuestDestroyed() override;
99   void GuestReady() override;
100   void GuestSizeChangedDueToAutoSize(const gfx::Size& old_size,
101                                      const gfx::Size& new_size) override;
102   bool IsAutoSizeSupported() const override;
103   bool IsDragAndDropEnabled() const override;
104   void WillAttachToEmbedder() override;
105   void WillDestroy() override;
106
107   // WebContentsDelegate implementation.
108   bool AddMessageToConsole(content::WebContents* source,
109                            int32 level,
110                            const base::string16& message,
111                            int32 line_no,
112                            const base::string16& source_id) override;
113   void LoadProgressChanged(content::WebContents* source,
114                            double progress) override;
115   void CloseContents(content::WebContents* source) override;
116   void FindReply(content::WebContents* source,
117                  int request_id,
118                  int number_of_matches,
119                  const gfx::Rect& selection_rect,
120                  int active_match_ordinal,
121                  bool final_update) override;
122   bool HandleContextMenu(const content::ContextMenuParams& params) override;
123   void HandleKeyboardEvent(
124       content::WebContents* source,
125       const content::NativeWebKeyboardEvent& event) override;
126   void RendererResponsive(content::WebContents* source) override;
127   void RendererUnresponsive(content::WebContents* source) override;
128   void RequestMediaAccessPermission(
129       content::WebContents* source,
130       const content::MediaStreamRequest& request,
131       const content::MediaResponseCallback& callback) override;
132   bool CheckMediaAccessPermission(content::WebContents* source,
133                                   const GURL& security_origin,
134                                   content::MediaStreamType type) override;
135   void CanDownload(content::RenderViewHost* render_view_host,
136                    const GURL& url,
137                    const std::string& request_method,
138                    const base::Callback<void(bool)>& callback) override;
139   content::JavaScriptDialogManager* GetJavaScriptDialogManager() override;
140   content::ColorChooser* OpenColorChooser(
141       content::WebContents* web_contents,
142       SkColor color,
143       const std::vector<content::ColorSuggestion>& suggestions) override;
144   void AddNewContents(content::WebContents* source,
145                       content::WebContents* new_contents,
146                       WindowOpenDisposition disposition,
147                       const gfx::Rect& initial_pos,
148                       bool user_gesture,
149                       bool* was_blocked) override;
150   content::WebContents* OpenURLFromTab(
151       content::WebContents* source,
152       const content::OpenURLParams& params) override;
153   void WebContentsCreated(content::WebContents* source_contents,
154                           int opener_render_frame_id,
155                           const base::string16& frame_name,
156                           const GURL& target_url,
157                           content::WebContents* new_contents) override;
158
159   // BrowserPluginGuestDelegate implementation.
160   content::WebContents* CreateNewGuestWindow(
161       const content::WebContents::CreateParams& create_params) override;
162   void RequestPointerLockPermission(
163       bool user_gesture,
164       bool last_unlocked_by_target,
165       const base::Callback<void(bool)>& callback) override;
166   // NotificationObserver implementation.
167   void Observe(int type,
168                const content::NotificationSource& source,
169                const content::NotificationDetails& details) override;
170
171   // Returns the current zoom factor.
172   double GetZoom();
173
174   // Begin or continue a find request.
175   void StartFinding(const base::string16& search_text,
176                     const blink::WebFindOptions& options,
177                     scoped_refptr<WebViewInternalFindFunction> find_function);
178
179   // Conclude a find request to clear highlighting.
180   void StopFinding(content::StopFindAction);
181
182   // If possible, navigate the guest to |relative_index| entries away from the
183   // current navigation entry. Returns true on success.
184   bool Go(int relative_index);
185
186   // Reload the guest.
187   void Reload();
188
189   typedef base::Callback<void(bool /* allow */,
190                               const std::string& /* user_input */)>
191       PermissionResponseCallback;
192   int RequestPermission(
193       WebViewPermissionType permission_type,
194       const base::DictionaryValue& request_info,
195       const PermissionResponseCallback& callback,
196       bool allowed_by_default);
197
198   // Requests Geolocation Permission from the embedder.
199   void RequestGeolocationPermission(int bridge_id,
200                                     const GURL& requesting_frame,
201                                     bool user_gesture,
202                                     const base::Callback<void(bool)>& callback);
203   void CancelGeolocationPermissionRequest(int bridge_id);
204
205   // Called when file system access is requested by the guest content using the
206   // HTML5 file system API in main thread, or a worker thread.
207   // The request is plumbed through the <webview> permission request API. The
208   // request will be:
209   // - Allowed if the embedder explicitly allowed it.
210   // - Denied if the embedder explicitly denied.
211   // - Determined by the guest's content settings if the embedder does not
212   // perform an explicit action.
213   void RequestFileSystemPermission(const GURL& url,
214                                    bool allowed_by_default,
215                                    const base::Callback<void(bool)>& callback);
216
217   // Overrides the user agent for this guest.
218   // This affects subsequent guest navigations.
219   void SetUserAgentOverride(const std::string& user_agent_override);
220
221   // Stop loading the guest.
222   void Stop();
223
224   // Kill the guest process.
225   void Terminate();
226
227   // Clears data in the storage partition of this guest.
228   //
229   // Partition data that are newer than |removal_since| will be removed.
230   // |removal_mask| corresponds to bitmask in StoragePartition::RemoveDataMask.
231   bool ClearData(const base::Time remove_since,
232                  uint32 removal_mask,
233                  const base::Closure& callback);
234
235   ScriptExecutor* script_executor() { return script_executor_.get(); }
236
237  private:
238   friend class WebViewPermissionHelper;
239   WebViewGuest(content::BrowserContext* browser_context,
240                int guest_instance_id);
241
242   ~WebViewGuest() override;
243
244   void AttachWebViewHelpers(content::WebContents* contents);
245
246   void OnWebViewNewWindowResponse(int new_window_instance_id,
247                                   bool allow,
248                                   const std::string& user_input);
249
250   // WebContentsObserver implementation.
251   void DidCommitProvisionalLoadForFrame(
252       content::RenderFrameHost* render_frame_host,
253       const GURL& url,
254       ui::PageTransition transition_type) override;
255   void DidFailProvisionalLoad(content::RenderFrameHost* render_frame_host,
256                               const GURL& validated_url,
257                               int error_code,
258                               const base::string16& error_description) override;
259   void DidStartProvisionalLoadForFrame(
260       content::RenderFrameHost* render_frame_host,
261       const GURL& validated_url,
262       bool is_error_page,
263       bool is_iframe_srcdoc) override;
264   void DocumentLoadedInFrame(
265       content::RenderFrameHost* render_frame_host) override;
266   bool OnMessageReceived(const IPC::Message& message,
267                          content::RenderFrameHost* render_frame_host) override;
268   void RenderProcessGone(base::TerminationStatus status) override;
269   void UserAgentOverrideSet(const std::string& user_agent) override;
270
271   // Informs the embedder of a frame name change.
272   void ReportFrameNameChange(const std::string& name);
273
274   // Called after the load handler is called in the guest's main frame.
275   void LoadHandlerCalled();
276
277   // Called when a redirect notification occurs.
278   void LoadRedirect(const GURL& old_url,
279                     const GURL& new_url,
280                     bool is_top_level);
281
282   void PushWebViewStateToIOThread();
283   static void RemoveWebViewStateFromIOThread(
284       content::WebContents* web_contents);
285
286   void LoadURLWithParams(const GURL& url,
287                          const content::Referrer& referrer,
288                          ui::PageTransition transition_type,
289                          content::WebContents* web_contents);
290
291   void RequestNewWindowPermission(
292       WindowOpenDisposition disposition,
293       const gfx::Rect& initial_bounds,
294       bool user_gesture,
295       content::WebContents* new_contents);
296
297   // Requests resolution of a potentially relative URL.
298   GURL ResolveURL(const std::string& src);
299
300   // Notification that a load in the guest resulted in abort. Note that |url|
301   // may be invalid.
302   void LoadAbort(bool is_top_level,
303                  const GURL& url,
304                  const std::string& error_type);
305
306   void OnFrameNameChanged(bool is_top_level, const std::string& name);
307
308   // Creates a new guest window owned by this WebViewGuest.
309   void CreateNewGuestWebViewWindow(const content::OpenURLParams& params);
310
311   void NewGuestWebViewCallback(const content::OpenURLParams& params,
312                                content::WebContents* guest_web_contents);
313
314   bool HandleKeyboardShortcuts(const content::NativeWebKeyboardEvent& event);
315
316   void SetUpAutoSize();
317
318   // Handles find requests and replies for the webview find API.
319   WebViewFindHelper find_helper_;
320
321   ObserverList<ScriptExecutionObserver> script_observers_;
322   scoped_ptr<ScriptExecutor> script_executor_;
323
324   content::NotificationRegistrar notification_registrar_;
325
326   // True if the user agent is overridden.
327   bool is_overriding_user_agent_;
328
329   // Stores the window name of the main frame of the guest.
330   std::string name_;
331
332   // Stores whether the contents of the guest can be transparent.
333   bool guest_opaque_;
334
335   // Stores the src URL of the WebView.
336   GURL src_;
337
338   // Handles the JavaScript dialog requests.
339   JavaScriptDialogHelper javascript_dialog_helper_;
340
341   // Handles permission requests.
342   scoped_ptr<WebViewPermissionHelper> web_view_permission_helper_;
343
344   scoped_ptr<WebViewGuestDelegate> web_view_guest_delegate_;
345
346   // Tracks the name, and target URL of the new window. Once the first
347   // navigation commits, we no longer track this information.
348   struct NewWindowInfo {
349     GURL url;
350     std::string name;
351     bool changed;
352     NewWindowInfo(const GURL& url, const std::string& name) :
353         url(url),
354         name(name),
355         changed(false) {}
356   };
357
358   typedef std::map<WebViewGuest*, NewWindowInfo> PendingWindowMap;
359   PendingWindowMap pending_new_windows_;
360
361   // This is used to ensure pending tasks will not fire after this object is
362   // destroyed.
363   base::WeakPtrFactory<WebViewGuest> weak_ptr_factory_;
364
365   DISALLOW_COPY_AND_ASSIGN(WebViewGuest);
366 };
367
368 }  // namespace extensions
369
370 #endif  // EXTENSIONS_BROWSER_GUEST_VIEW_WEB_VIEW_WEB_VIEW_GUEST_H_