- add sources.
[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 string16& message,
45                                    int32 line_no,
46                                    const string16& source_id) OVERRIDE;
47   virtual void LoadProgressed(double progress) OVERRIDE;
48   virtual void Close() OVERRIDE;
49   virtual void EmbedderDestroyed() OVERRIDE;
50   virtual void GuestProcessGone(base::TerminationStatus status) OVERRIDE;
51   virtual bool HandleKeyboardEvent(
52       const content::NativeWebKeyboardEvent& event) OVERRIDE;
53   virtual bool IsDragAndDropEnabled() OVERRIDE;
54   virtual bool IsOverridingUserAgent() const OVERRIDE;
55   virtual void LoadAbort(bool is_top_level,
56                          const GURL& url,
57                          const std::string& error_type) OVERRIDE;
58   virtual void RendererResponsive() OVERRIDE;
59   virtual void RendererUnresponsive() OVERRIDE;
60   virtual bool RequestPermission(
61       BrowserPluginPermissionType permission_type,
62       const base::DictionaryValue& request_info,
63       const PermissionResponseCallback& callback,
64       bool allowed_by_default) OVERRIDE;
65   virtual GURL ResolveURL(const std::string& src) OVERRIDE;
66   virtual void SizeChanged(const gfx::Size& old_size, const gfx::Size& new_size)
67       OVERRIDE;
68
69   // NotificationObserver implementation.
70   virtual void Observe(int type,
71                        const content::NotificationSource& source,
72                        const content::NotificationDetails& details) OVERRIDE;
73
74   // If possible, navigate the guest to |relative_index| entries away from the
75   // current navigation entry.
76   void Go(int relative_index);
77
78   // Reload the guest.
79   void Reload();
80
81   enum PermissionResponseAction {
82     DENY,
83     ALLOW,
84     DEFAULT
85   };
86
87   enum SetPermissionResult {
88     SET_PERMISSION_INVALID,
89     SET_PERMISSION_ALLOWED,
90     SET_PERMISSION_DENIED
91   };
92
93   // Responds to the permission request |request_id| with |action| and
94   // |user_input|. Returns whether there was a pending request for the provided
95   // |request_id|.
96   SetPermissionResult SetPermission(int request_id,
97                                     PermissionResponseAction action,
98                                     const std::string& user_input);
99
100   // Overrides the user agent for this guest.
101   // This affects subsequent guest navigations.
102   void SetUserAgentOverride(const std::string& user_agent_override);
103
104   // Stop loading the guest.
105   void Stop();
106
107   // Kill the guest process.
108   void Terminate();
109
110   // Clears data in the storage partition of this guest.
111   //
112   // Partition data that are newer than |removal_since| will be removed.
113   // |removal_mask| corresponds to bitmask in StoragePartition::RemoveDataMask.
114   bool ClearData(const base::Time remove_since,
115                  uint32 removal_mask,
116                  const base::Closure& callback);
117
118   extensions::ScriptExecutor* script_executor() {
119     return script_executor_.get();
120   }
121
122  private:
123   virtual ~WebViewGuest();
124
125   // WebContentsObserver implementation.
126   virtual void DidCommitProvisionalLoadForFrame(
127       int64 frame_id,
128       const string16& frame_unique_name,
129       bool is_main_frame,
130       const GURL& url,
131       content::PageTransition transition_type,
132       content::RenderViewHost* render_view_host) OVERRIDE;
133   virtual void DidFailProvisionalLoad(
134       int64 frame_id,
135       const string16& frame_unique_name,
136       bool is_main_frame,
137       const GURL& validated_url,
138       int error_code,
139       const string16& error_description,
140       content::RenderViewHost* render_view_host) OVERRIDE;
141   virtual void DidStartProvisionalLoadForFrame(
142       int64 frame_id,
143       int64 parent_frame_id,
144       bool is_main_frame,
145       const GURL& validated_url,
146       bool is_error_page,
147       bool is_iframe_srcdoc,
148       content::RenderViewHost* render_view_host) OVERRIDE;
149   virtual void DidStopLoading(
150       content::RenderViewHost* render_view_host) OVERRIDE;
151   virtual void WebContentsDestroyed(
152       content::WebContents* web_contents) OVERRIDE;
153
154   // Called after the load handler is called in the guest's main frame.
155   void LoadHandlerCalled();
156
157   // Called when a redirect notification occurs.
158   void LoadRedirect(const GURL& old_url,
159                     const GURL& new_url,
160                     bool is_top_level);
161
162   static bool AllowChromeExtensionURLs();
163
164   void AddWebViewToExtensionRendererState();
165   static void RemoveWebViewFromExtensionRendererState(
166       content::WebContents* web_contents);
167
168   ObserverList<extensions::TabHelper::ScriptExecutionObserver>
169       script_observers_;
170   scoped_ptr<extensions::ScriptExecutor> script_executor_;
171
172   content::NotificationRegistrar notification_registrar_;
173
174   // A counter to generate a unique request id for a permission request.
175   // We only need the ids to be unique for a given WebViewGuest.
176   int next_permission_request_id_;
177
178   // A map to store the callback for a request keyed by the request's id.
179   struct PermissionResponseInfo {
180     PermissionResponseCallback callback;
181     bool allowed_by_default;
182     PermissionResponseInfo();
183     PermissionResponseInfo(const PermissionResponseCallback& callback,
184                            bool allowed_by_default);
185     ~PermissionResponseInfo();
186   };
187   typedef std::map<int, PermissionResponseInfo> RequestMap;
188   RequestMap pending_permission_requests_;
189
190   // True if the user agent is overridden.
191   bool is_overriding_user_agent_;
192
193   DISALLOW_COPY_AND_ASSIGN(WebViewGuest);
194 };
195
196 #endif  // CHROME_BROWSER_GUESTVIEW_WEBVIEW_WEBVIEW_GUEST_H_