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.
5 #ifndef EXTENSIONS_BROWSER_GUEST_VIEW_WEB_VIEW_WEB_VIEW_GUEST_H_
6 #define EXTENSIONS_BROWSER_GUEST_VIEW_WEB_VIEW_WEB_VIEW_GUEST_H_
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"
23 struct WebFindOptions;
26 namespace extensions {
28 class WebViewInternalFindFunction;
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 {
39 static GuestViewBase* Create(content::BrowserContext* browser_context,
40 int guest_instance_id);
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,
54 // Returns guestview::kInstanceIDNone if |contents| does not correspond to a
56 static int GetViewInstanceId(content::WebContents* contents);
58 static const char Type[];
60 // Request navigating the guest to the provided |src| URL.
61 void NavigateGuest(const std::string& src);
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.
69 const WebViewGuestDelegate::MenuItemVector* items);
71 // Sets the frame name of the guest.
72 void SetName(const std::string& name);
74 // Set the zoom factor.
75 void SetZoom(double zoom_factor);
77 // Sets the transparency of the guest.
78 void SetAllowTransparency(bool allow);
80 // GuestViewBase implementation.
81 virtual const char* GetAPINamespace() const OVERRIDE;
82 virtual int GetTaskPrefix() const OVERRIDE;
83 virtual void CreateWebContents(
84 const std::string& embedder_extension_id,
85 int embedder_render_process_id,
86 const GURL& embedder_site_url,
87 const base::DictionaryValue& create_params,
88 const WebContentsCreatedCallback& callback) OVERRIDE;
89 virtual void DidAttachToEmbedder() OVERRIDE;
90 virtual void DidInitialize() OVERRIDE;
91 virtual void DidStopLoading() OVERRIDE;
92 virtual void EmbedderDestroyed() OVERRIDE;
93 virtual void GuestDestroyed() OVERRIDE;
94 virtual void GuestReady() OVERRIDE;
95 virtual void GuestSizeChangedDueToAutoSize(
96 const gfx::Size& old_size,
97 const gfx::Size& new_size) OVERRIDE;
98 virtual bool IsAutoSizeSupported() const OVERRIDE;
99 virtual bool IsDragAndDropEnabled() const OVERRIDE;
100 virtual void WillAttachToEmbedder() OVERRIDE;
101 virtual void WillDestroy() OVERRIDE;
103 // WebContentsDelegate implementation.
104 virtual bool AddMessageToConsole(content::WebContents* source,
106 const base::string16& message,
108 const base::string16& source_id) OVERRIDE;
109 virtual void LoadProgressChanged(content::WebContents* source,
110 double progress) OVERRIDE;
111 virtual void CloseContents(content::WebContents* source) OVERRIDE;
112 virtual void FindReply(content::WebContents* source,
114 int number_of_matches,
115 const gfx::Rect& selection_rect,
116 int active_match_ordinal,
117 bool final_update) OVERRIDE;
118 virtual bool HandleContextMenu(
119 const content::ContextMenuParams& params) OVERRIDE;
120 virtual void HandleKeyboardEvent(
121 content::WebContents* source,
122 const content::NativeWebKeyboardEvent& event) OVERRIDE;
123 virtual void RendererResponsive(content::WebContents* source) OVERRIDE;
124 virtual void RendererUnresponsive(content::WebContents* source) OVERRIDE;
125 virtual void RequestMediaAccessPermission(
126 content::WebContents* source,
127 const content::MediaStreamRequest& request,
128 const content::MediaResponseCallback& callback) OVERRIDE;
129 virtual bool CheckMediaAccessPermission(
130 content::WebContents* source,
131 const GURL& security_origin,
132 content::MediaStreamType type) OVERRIDE;
133 virtual void CanDownload(content::RenderViewHost* render_view_host,
135 const std::string& request_method,
136 const base::Callback<void(bool)>& callback) OVERRIDE;
137 virtual content::JavaScriptDialogManager*
138 GetJavaScriptDialogManager() OVERRIDE;
139 virtual content::ColorChooser* OpenColorChooser(
140 content::WebContents* web_contents,
142 const std::vector<content::ColorSuggestion>& suggestions) OVERRIDE;
143 virtual void AddNewContents(content::WebContents* source,
144 content::WebContents* new_contents,
145 WindowOpenDisposition disposition,
146 const gfx::Rect& initial_pos,
148 bool* was_blocked) OVERRIDE;
149 virtual content::WebContents* OpenURLFromTab(
150 content::WebContents* source,
151 const content::OpenURLParams& params) OVERRIDE;
152 virtual void WebContentsCreated(content::WebContents* source_contents,
153 int opener_render_frame_id,
154 const base::string16& frame_name,
155 const GURL& target_url,
156 content::WebContents* new_contents) OVERRIDE;
158 // BrowserPluginGuestDelegate implementation.
159 virtual content::WebContents* CreateNewGuestWindow(
160 const content::WebContents::CreateParams& create_params) OVERRIDE;
161 virtual void RequestPointerLockPermission(
163 bool last_unlocked_by_target,
164 const base::Callback<void(bool)>& callback) OVERRIDE;
165 // NotificationObserver implementation.
166 virtual void Observe(int type,
167 const content::NotificationSource& source,
168 const content::NotificationDetails& details) OVERRIDE;
170 // Returns the current zoom factor.
173 // Begin or continue a find request.
174 void Find(const base::string16& search_text,
175 const blink::WebFindOptions& options,
176 scoped_refptr<WebViewInternalFindFunction> find_function);
178 // Conclude a find request to clear highlighting.
179 void StopFinding(content::StopFindAction);
181 // If possible, navigate the guest to |relative_index| entries away from the
182 // current navigation entry.
183 void Go(int relative_index);
188 typedef base::Callback<void(bool /* allow */,
189 const std::string& /* user_input */)>
190 PermissionResponseCallback;
191 int RequestPermission(
192 WebViewPermissionType permission_type,
193 const base::DictionaryValue& request_info,
194 const PermissionResponseCallback& callback,
195 bool allowed_by_default);
197 // Requests Geolocation Permission from the embedder.
198 void RequestGeolocationPermission(int bridge_id,
199 const GURL& requesting_frame,
201 const base::Callback<void(bool)>& callback);
202 void CancelGeolocationPermissionRequest(int bridge_id);
204 // Called when file system access is requested by the guest content using the
205 // HTML5 file system API in main thread, or a worker thread.
206 // The request is plumbed through the <webview> permission request API. The
208 // - Allowed if the embedder explicitly allowed it.
209 // - Denied if the embedder explicitly denied.
210 // - Determined by the guest's content settings if the embedder does not
211 // perform an explicit action.
212 void RequestFileSystemPermission(const GURL& url,
213 bool allowed_by_default,
214 const base::Callback<void(bool)>& callback);
216 // Overrides the user agent for this guest.
217 // This affects subsequent guest navigations.
218 void SetUserAgentOverride(const std::string& user_agent_override);
220 // Stop loading the guest.
223 // Kill the guest process.
226 // Clears data in the storage partition of this guest.
228 // Partition data that are newer than |removal_since| will be removed.
229 // |removal_mask| corresponds to bitmask in StoragePartition::RemoveDataMask.
230 bool ClearData(const base::Time remove_since,
232 const base::Closure& callback);
234 ScriptExecutor* script_executor() { return script_executor_.get(); }
237 friend class WebViewPermissionHelper;
238 WebViewGuest(content::BrowserContext* browser_context,
239 int guest_instance_id);
241 virtual ~WebViewGuest();
243 void AttachWebViewHelpers(content::WebContents* contents);
245 void OnWebViewNewWindowResponse(int new_window_instance_id,
247 const std::string& user_input);
249 // WebContentsObserver implementation.
250 virtual void DidCommitProvisionalLoadForFrame(
251 content::RenderFrameHost* render_frame_host,
253 ui::PageTransition transition_type) OVERRIDE;
254 virtual void DidFailProvisionalLoad(
255 content::RenderFrameHost* render_frame_host,
256 const GURL& validated_url,
258 const base::string16& error_description) OVERRIDE;
259 virtual void DidStartProvisionalLoadForFrame(
260 content::RenderFrameHost* render_frame_host,
261 const GURL& validated_url,
263 bool is_iframe_srcdoc) OVERRIDE;
264 virtual void DocumentLoadedInFrame(
265 content::RenderFrameHost* render_frame_host) OVERRIDE;
266 virtual bool OnMessageReceived(
267 const IPC::Message& message,
268 content::RenderFrameHost* render_frame_host) OVERRIDE;
269 virtual void RenderProcessGone(base::TerminationStatus status) OVERRIDE;
270 virtual void UserAgentOverrideSet(const std::string& user_agent) OVERRIDE;
272 // Informs the embedder of a frame name change.
273 void ReportFrameNameChange(const std::string& name);
275 // Called after the load handler is called in the guest's main frame.
276 void LoadHandlerCalled();
278 // Called when a redirect notification occurs.
279 void LoadRedirect(const GURL& old_url,
283 void PushWebViewStateToIOThread();
284 static void RemoveWebViewStateFromIOThread(
285 content::WebContents* web_contents);
287 void LoadURLWithParams(const GURL& url,
288 const content::Referrer& referrer,
289 ui::PageTransition transition_type,
290 content::WebContents* web_contents);
292 void RequestNewWindowPermission(
293 WindowOpenDisposition disposition,
294 const gfx::Rect& initial_bounds,
296 content::WebContents* new_contents);
298 // Destroy unattached new windows that have been opened by this
300 void DestroyUnattachedWindows();
302 // Requests resolution of a potentially relative URL.
303 GURL ResolveURL(const std::string& src);
305 // Notification that a load in the guest resulted in abort. Note that |url|
307 void LoadAbort(bool is_top_level,
309 const std::string& error_type);
311 void OnFrameNameChanged(bool is_top_level, const std::string& name);
313 // Creates a new guest window owned by this WebViewGuest.
314 void CreateNewGuestWebViewWindow(const content::OpenURLParams& params);
316 void NewGuestWebViewCallback(const content::OpenURLParams& params,
317 content::WebContents* guest_web_contents);
319 bool HandleKeyboardShortcuts(const content::NativeWebKeyboardEvent& event);
321 void SetUpAutoSize();
323 // Handles find requests and replies for the webview find API.
324 WebViewFindHelper find_helper_;
326 ObserverList<ScriptExecutionObserver> script_observers_;
327 scoped_ptr<ScriptExecutor> script_executor_;
329 content::NotificationRegistrar notification_registrar_;
331 // True if the user agent is overridden.
332 bool is_overriding_user_agent_;
334 // Stores the window name of the main frame of the guest.
337 // Stores whether the contents of the guest can be transparent.
340 // Handles the JavaScript dialog requests.
341 JavaScriptDialogHelper javascript_dialog_helper_;
343 // Handels permission requests.
344 scoped_ptr<WebViewPermissionHelper> web_view_permission_helper_;
346 scoped_ptr<WebViewGuestDelegate> web_view_guest_delegate_;
348 // Tracks the name, and target URL of the new window. Once the first
349 // navigation commits, we no longer track this information.
350 struct NewWindowInfo {
354 NewWindowInfo(const GURL& url, const std::string& name) :
360 typedef std::map<WebViewGuest*, NewWindowInfo> PendingWindowMap;
361 PendingWindowMap pending_new_windows_;
363 DISALLOW_COPY_AND_ASSIGN(WebViewGuest);
366 } // namespace extensions
368 #endif // EXTENSIONS_BROWSER_GUEST_VIEW_WEB_VIEW_WEB_VIEW_GUEST_H_