Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / guestview / webview / webview_guest.h
1 // Copyright 2013 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_GUESTVIEW_WEBVIEW_WEBVIEW_GUEST_H_
6 #define CHROME_BROWSER_GUESTVIEW_WEBVIEW_WEBVIEW_GUEST_H_
7
8 #include "base/observer_list.h"
9 #include "chrome/browser/extensions/tab_helper.h"
10 #include "chrome/browser/guestview/guestview.h"
11 #include "content/public/browser/notification_registrar.h"
12 #include "content/public/browser/web_contents_observer.h"
13
14 namespace extensions {
15 class ScriptExecutor;
16 }  // namespace extensions
17
18 // A WebViewGuest is a WebContentsObserver on the guest WebContents of a
19 // <webview> tag. It provides the browser-side implementation of the <webview>
20 // API and manages the lifetime of <webview> extension events. WebViewGuest is
21 // created on attachment. That is, when a guest WebContents is associated with
22 // a particular embedder WebContents. This happens on either initial navigation
23 // or through the use of the New Window API, when a new window is attached to
24 // a particular <webview>.
25 class WebViewGuest : public GuestView,
26                      public content::NotificationObserver,
27                      public content::WebContentsObserver {
28  public:
29   WebViewGuest(content::WebContents* guest_web_contents,
30                const std::string& extension_id);
31
32   static WebViewGuest* From(int embedder_process_id, int instance_id);
33   static WebViewGuest* FromWebContents(content::WebContents* contents);
34
35   // GuestView implementation.
36   virtual void Attach(content::WebContents* embedder_web_contents,
37                       const base::DictionaryValue& args) OVERRIDE;
38   virtual GuestView::Type GetViewType() const OVERRIDE;
39   virtual WebViewGuest* AsWebView() OVERRIDE;
40   virtual AdViewGuest* AsAdView() OVERRIDE;
41
42   // GuestDelegate implementation.
43   virtual void AddMessageToConsole(int32 level,
44                                    const base::string16& message,
45                                    int32 line_no,
46                                    const base::string16& source_id) OVERRIDE;
47   virtual void LoadProgressed(double progress) OVERRIDE;
48   virtual void Close() OVERRIDE;
49   virtual void DidAttach() OVERRIDE;
50   virtual void EmbedderDestroyed() OVERRIDE;
51   virtual void GuestProcessGone(base::TerminationStatus status) OVERRIDE;
52   virtual bool HandleKeyboardEvent(
53       const content::NativeWebKeyboardEvent& event) OVERRIDE;
54   virtual bool IsDragAndDropEnabled() OVERRIDE;
55   virtual bool IsOverridingUserAgent() const OVERRIDE;
56   virtual void LoadAbort(bool is_top_level,
57                          const GURL& url,
58                          const std::string& error_type) OVERRIDE;
59   virtual void RendererResponsive() OVERRIDE;
60   virtual void RendererUnresponsive() OVERRIDE;
61   virtual bool RequestPermission(
62       BrowserPluginPermissionType permission_type,
63       const base::DictionaryValue& request_info,
64       const PermissionResponseCallback& callback,
65       bool allowed_by_default) OVERRIDE;
66   virtual GURL ResolveURL(const std::string& src) OVERRIDE;
67   virtual void SizeChanged(const gfx::Size& old_size, const gfx::Size& new_size)
68       OVERRIDE;
69
70   // NotificationObserver implementation.
71   virtual void Observe(int type,
72                        const content::NotificationSource& source,
73                        const content::NotificationDetails& details) OVERRIDE;
74
75   // Set the zoom factor.
76   virtual void SetZoom(double zoom_factor) OVERRIDE;
77
78   // Returns the current zoom factor.
79   double GetZoom();
80
81   // If possible, navigate the guest to |relative_index| entries away from the
82   // current navigation entry.
83   void Go(int relative_index);
84
85   // Reload the guest.
86   void Reload();
87
88   enum PermissionResponseAction {
89     DENY,
90     ALLOW,
91     DEFAULT
92   };
93
94   enum SetPermissionResult {
95     SET_PERMISSION_INVALID,
96     SET_PERMISSION_ALLOWED,
97     SET_PERMISSION_DENIED
98   };
99
100   // Responds to the permission request |request_id| with |action| and
101   // |user_input|. Returns whether there was a pending request for the provided
102   // |request_id|.
103   SetPermissionResult SetPermission(int request_id,
104                                     PermissionResponseAction action,
105                                     const std::string& user_input);
106
107   // Overrides the user agent for this guest.
108   // This affects subsequent guest navigations.
109   void SetUserAgentOverride(const std::string& user_agent_override);
110
111   // Stop loading the guest.
112   void Stop();
113
114   // Kill the guest process.
115   void Terminate();
116
117   // Clears data in the storage partition of this guest.
118   //
119   // Partition data that are newer than |removal_since| will be removed.
120   // |removal_mask| corresponds to bitmask in StoragePartition::RemoveDataMask.
121   bool ClearData(const base::Time remove_since,
122                  uint32 removal_mask,
123                  const base::Closure& callback);
124
125   extensions::ScriptExecutor* script_executor() {
126     return script_executor_.get();
127   }
128
129  private:
130   virtual ~WebViewGuest();
131
132   // A map to store the callback for a request keyed by the request's id.
133   struct PermissionResponseInfo {
134     PermissionResponseCallback callback;
135     BrowserPluginPermissionType permission_type;
136     bool allowed_by_default;
137     PermissionResponseInfo();
138     PermissionResponseInfo(const PermissionResponseCallback& callback,
139                            BrowserPluginPermissionType permission_type,
140                            bool allowed_by_default);
141     ~PermissionResponseInfo();
142   };
143
144   static void RecordUserInitiatedUMA(const PermissionResponseInfo& info,
145                                      bool allow);
146   // WebContentsObserver implementation.
147   virtual void DidCommitProvisionalLoadForFrame(
148       int64 frame_id,
149       const base::string16& frame_unique_name,
150       bool is_main_frame,
151       const GURL& url,
152       content::PageTransition transition_type,
153       content::RenderViewHost* render_view_host) OVERRIDE;
154   virtual void DidFailProvisionalLoad(
155       int64 frame_id,
156       const base::string16& frame_unique_name,
157       bool is_main_frame,
158       const GURL& validated_url,
159       int error_code,
160       const base::string16& error_description,
161       content::RenderViewHost* render_view_host) OVERRIDE;
162   virtual void DidStartProvisionalLoadForFrame(
163       int64 frame_id,
164       int64 parent_frame_id,
165       bool is_main_frame,
166       const GURL& validated_url,
167       bool is_error_page,
168       bool is_iframe_srcdoc,
169       content::RenderViewHost* render_view_host) OVERRIDE;
170   virtual void DocumentLoadedInFrame(
171       int64 frame_id,
172       content::RenderViewHost* render_view_host) OVERRIDE;
173   virtual void DidStopLoading(
174       content::RenderViewHost* render_view_host) OVERRIDE;
175   virtual void WebContentsDestroyed(
176       content::WebContents* web_contents) OVERRIDE;
177   virtual void UserAgentOverrideSet(const std::string& user_agent) OVERRIDE;
178
179   // Called after the load handler is called in the guest's main frame.
180   void LoadHandlerCalled();
181
182   // Called when a redirect notification occurs.
183   void LoadRedirect(const GURL& old_url,
184                     const GURL& new_url,
185                     bool is_top_level);
186
187   void AddWebViewToExtensionRendererState();
188   static void RemoveWebViewFromExtensionRendererState(
189       content::WebContents* web_contents);
190
191   void InjectChromeVoxIfNeeded(content::RenderViewHost* render_view_host);
192
193   ObserverList<extensions::TabHelper::ScriptExecutionObserver>
194       script_observers_;
195   scoped_ptr<extensions::ScriptExecutor> script_executor_;
196
197   content::NotificationRegistrar notification_registrar_;
198
199   // A counter to generate a unique request id for a permission request.
200   // We only need the ids to be unique for a given WebViewGuest.
201   int next_permission_request_id_;
202
203   typedef std::map<int, PermissionResponseInfo> RequestMap;
204   RequestMap pending_permission_requests_;
205
206   // True if the user agent is overridden.
207   bool is_overriding_user_agent_;
208
209   // Indicates that the page needs to be reloaded once it has been attached to
210   // an embedder.
211   bool pending_reload_on_attachment_;
212
213   // Main frame ID of last committed page.
214   int64 main_frame_id_;
215
216   // Set to |true| if ChromeVox was already injected in main frame.
217   bool chromevox_injected_;
218
219   // Stores the current zoom factor.
220   double current_zoom_factor_;
221
222   DISALLOW_COPY_AND_ASSIGN(WebViewGuest);
223 };
224
225 #endif  // CHROME_BROWSER_GUESTVIEW_WEBVIEW_WEBVIEW_GUEST_H_