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_guest_delegate.h"
17 #include "extensions/browser/guest_view/web_view/web_view_permission_helper.h"
18 #include "extensions/browser/guest_view/web_view/web_view_permission_types.h"
19 #include "extensions/browser/script_executor.h"
22 struct WebFindOptions
;
25 namespace extensions
{
27 class WebViewInternalFindFunction
;
29 // A WebViewGuest provides the browser-side implementation of the <webview> API
30 // and manages the dispatch of <webview> extension events. WebViewGuest is
31 // created on attachment. That is, when a guest WebContents is associated with
32 // a particular embedder WebContents. This happens on either initial navigation
33 // or through the use of the New Window API, when a new window is attached to
34 // a particular <webview>.
35 class WebViewGuest
: public GuestView
<WebViewGuest
>,
36 public content::NotificationObserver
{
38 static GuestViewBase
* Create(content::BrowserContext
* browser_context
,
39 int guest_instance_id
);
41 // For WebViewGuest, we create special guest processes, which host the
42 // tag content separately from the main application that embeds the tag.
43 // A <webview> can specify both the partition name and whether the storage
44 // for that partition should be persisted. Each tag gets a SiteInstance with
45 // a specially formatted URL, based on the application it is hosted by and
46 // the partition requested by it. The format for that URL is:
47 // chrome-guest://partition_domain/persist?partition_name
48 static bool GetGuestPartitionConfigForSite(const GURL
& site
,
49 std::string
* partition_domain
,
50 std::string
* partition_name
,
53 // Returns guestview::kInstanceIDNone if |contents| does not correspond to a
55 static int GetViewInstanceId(content::WebContents
* contents
);
57 static const char Type
[];
59 // Request navigating the guest to the provided |src| URL.
60 void NavigateGuest(const std::string
& src
);
62 // Shows the context menu for the guest.
63 // |items| acts as a filter. This restricts the current context's default
64 // menu items to contain only the items from |items|.
65 // |items| == NULL means no filtering will be applied.
68 const WebViewGuestDelegate::MenuItemVector
* items
);
70 // Sets the frame name of the guest.
71 void SetName(const std::string
& name
);
73 // Set the zoom factor.
74 void SetZoom(double zoom_factor
);
76 // GuestViewBase implementation.
77 virtual const char* GetAPINamespace() const OVERRIDE
;
78 virtual int GetTaskPrefix() const OVERRIDE
;
79 virtual void CreateWebContents(
80 const std::string
& embedder_extension_id
,
81 int embedder_render_process_id
,
82 const base::DictionaryValue
& create_params
,
83 const WebContentsCreatedCallback
& callback
) OVERRIDE
;
84 virtual void DidAttachToEmbedder() OVERRIDE
;
85 virtual void DidInitialize() OVERRIDE
;
86 virtual void DidStopLoading() OVERRIDE
;
87 virtual void EmbedderDestroyed() OVERRIDE
;
88 virtual void GuestDestroyed() OVERRIDE
;
89 virtual void GuestReady() OVERRIDE
;
90 virtual void GuestSizeChangedDueToAutoSize(
91 const gfx::Size
& old_size
,
92 const gfx::Size
& new_size
) OVERRIDE
;
93 virtual bool IsAutoSizeSupported() const OVERRIDE
;
94 virtual bool IsDragAndDropEnabled() const OVERRIDE
;
95 virtual void WillAttachToEmbedder() OVERRIDE
;
96 virtual void WillDestroy() OVERRIDE
;
98 // WebContentsDelegate implementation.
99 virtual bool AddMessageToConsole(content::WebContents
* source
,
101 const base::string16
& message
,
103 const base::string16
& source_id
) OVERRIDE
;
104 virtual void LoadProgressChanged(content::WebContents
* source
,
105 double progress
) OVERRIDE
;
106 virtual void CloseContents(content::WebContents
* source
) OVERRIDE
;
107 virtual void FindReply(content::WebContents
* source
,
109 int number_of_matches
,
110 const gfx::Rect
& selection_rect
,
111 int active_match_ordinal
,
112 bool final_update
) OVERRIDE
;
113 virtual bool HandleContextMenu(
114 const content::ContextMenuParams
& params
) OVERRIDE
;
115 virtual void HandleKeyboardEvent(
116 content::WebContents
* source
,
117 const content::NativeWebKeyboardEvent
& event
) OVERRIDE
;
118 virtual void RendererResponsive(content::WebContents
* source
) OVERRIDE
;
119 virtual void RendererUnresponsive(content::WebContents
* source
) OVERRIDE
;
120 virtual void RequestMediaAccessPermission(
121 content::WebContents
* source
,
122 const content::MediaStreamRequest
& request
,
123 const content::MediaResponseCallback
& callback
) OVERRIDE
;
124 virtual void CanDownload(content::RenderViewHost
* render_view_host
,
126 const std::string
& request_method
,
127 const base::Callback
<void(bool)>& callback
) OVERRIDE
;
128 virtual content::JavaScriptDialogManager
*
129 GetJavaScriptDialogManager() OVERRIDE
;
130 virtual content::ColorChooser
* OpenColorChooser(
131 content::WebContents
* web_contents
,
133 const std::vector
<content::ColorSuggestion
>& suggestions
) OVERRIDE
;
134 virtual void RunFileChooser(
135 content::WebContents
* web_contents
,
136 const content::FileChooserParams
& params
) OVERRIDE
;
137 virtual void AddNewContents(content::WebContents
* source
,
138 content::WebContents
* new_contents
,
139 WindowOpenDisposition disposition
,
140 const gfx::Rect
& initial_pos
,
142 bool* was_blocked
) OVERRIDE
;
143 virtual content::WebContents
* OpenURLFromTab(
144 content::WebContents
* source
,
145 const content::OpenURLParams
& params
) OVERRIDE
;
146 virtual void WebContentsCreated(content::WebContents
* source_contents
,
147 int opener_render_frame_id
,
148 const base::string16
& frame_name
,
149 const GURL
& target_url
,
150 content::WebContents
* new_contents
) OVERRIDE
;
152 // BrowserPluginGuestDelegate implementation.
153 virtual content::WebContents
* CreateNewGuestWindow(
154 const content::WebContents::CreateParams
& create_params
) OVERRIDE
;
155 virtual void RequestPointerLockPermission(
157 bool last_unlocked_by_target
,
158 const base::Callback
<void(bool)>& callback
) OVERRIDE
;
159 // NotificationObserver implementation.
160 virtual void Observe(int type
,
161 const content::NotificationSource
& source
,
162 const content::NotificationDetails
& details
) OVERRIDE
;
164 // Returns the current zoom factor.
167 // Begin or continue a find request.
169 const base::string16
& search_text
,
170 const blink::WebFindOptions
& options
,
171 WebViewInternalFindFunction
* find_function
);
173 // Conclude a find request to clear highlighting.
174 void StopFinding(content::StopFindAction
);
176 // If possible, navigate the guest to |relative_index| entries away from the
177 // current navigation entry.
178 void Go(int relative_index
);
183 typedef base::Callback
<void(bool /* allow */,
184 const std::string
& /* user_input */)>
185 PermissionResponseCallback
;
186 int RequestPermission(
187 WebViewPermissionType permission_type
,
188 const base::DictionaryValue
& request_info
,
189 const PermissionResponseCallback
& callback
,
190 bool allowed_by_default
);
192 // Requests Geolocation Permission from the embedder.
193 void RequestGeolocationPermission(int bridge_id
,
194 const GURL
& requesting_frame
,
196 const base::Callback
<void(bool)>& callback
);
197 void CancelGeolocationPermissionRequest(int bridge_id
);
199 // Called when file system access is requested by the guest content using the
200 // HTML5 file system API in main thread, or a worker thread.
201 // The request is plumbed through the <webview> permission request API. The
203 // - Allowed if the embedder explicitly allowed it.
204 // - Denied if the embedder explicitly denied.
205 // - Determined by the guest's content settings if the embedder does not
206 // perform an explicit action.
207 void RequestFileSystemPermission(const GURL
& url
,
208 bool allowed_by_default
,
209 const base::Callback
<void(bool)>& callback
);
211 // Overrides the user agent for this guest.
212 // This affects subsequent guest navigations.
213 void SetUserAgentOverride(const std::string
& user_agent_override
);
215 // Stop loading the guest.
218 // Kill the guest process.
221 // Clears data in the storage partition of this guest.
223 // Partition data that are newer than |removal_since| will be removed.
224 // |removal_mask| corresponds to bitmask in StoragePartition::RemoveDataMask.
225 bool ClearData(const base::Time remove_since
,
227 const base::Closure
& callback
);
229 ScriptExecutor
* script_executor() { return script_executor_
.get(); }
232 friend class WebViewPermissionHelper
;
233 WebViewGuest(content::BrowserContext
* browser_context
,
234 int guest_instance_id
);
236 virtual ~WebViewGuest();
238 void AttachWebViewHelpers(content::WebContents
* contents
);
240 void OnWebViewNewWindowResponse(int new_window_instance_id
,
242 const std::string
& user_input
);
244 // WebContentsObserver implementation.
245 virtual void DidCommitProvisionalLoadForFrame(
246 content::RenderFrameHost
* render_frame_host
,
248 content::PageTransition transition_type
) OVERRIDE
;
249 virtual void DidFailProvisionalLoad(
250 content::RenderFrameHost
* render_frame_host
,
251 const GURL
& validated_url
,
253 const base::string16
& error_description
) OVERRIDE
;
254 virtual void DidStartProvisionalLoadForFrame(
255 content::RenderFrameHost
* render_frame_host
,
256 const GURL
& validated_url
,
258 bool is_iframe_srcdoc
) OVERRIDE
;
259 virtual void DocumentLoadedInFrame(
260 content::RenderFrameHost
* render_frame_host
) OVERRIDE
;
261 virtual bool OnMessageReceived(
262 const IPC::Message
& message
,
263 content::RenderFrameHost
* render_frame_host
) OVERRIDE
;
264 virtual void RenderProcessGone(base::TerminationStatus status
) OVERRIDE
;
265 virtual void UserAgentOverrideSet(const std::string
& user_agent
) OVERRIDE
;
267 // Informs the embedder of a frame name change.
268 void ReportFrameNameChange(const std::string
& name
);
270 // Called after the load handler is called in the guest's main frame.
271 void LoadHandlerCalled();
273 // Called when a redirect notification occurs.
274 void LoadRedirect(const GURL
& old_url
,
278 void PushWebViewStateToIOThread();
279 static void RemoveWebViewStateFromIOThread(
280 content::WebContents
* web_contents
);
282 void LoadURLWithParams(const GURL
& url
,
283 const content::Referrer
& referrer
,
284 content::PageTransition transition_type
,
285 content::WebContents
* web_contents
);
287 void RequestNewWindowPermission(
288 WindowOpenDisposition disposition
,
289 const gfx::Rect
& initial_bounds
,
291 content::WebContents
* new_contents
);
293 // Destroy unattached new windows that have been opened by this
295 void DestroyUnattachedWindows();
297 // Requests resolution of a potentially relative URL.
298 GURL
ResolveURL(const std::string
& src
);
300 // Notification that a load in the guest resulted in abort. Note that |url|
302 void LoadAbort(bool is_top_level
,
304 const std::string
& error_type
);
306 void OnFrameNameChanged(bool is_top_level
, const std::string
& name
);
308 // Creates a new guest window owned by this WebViewGuest.
309 void CreateNewGuestWebViewWindow(const content::OpenURLParams
& params
);
311 void NewGuestWebViewCallback(const content::OpenURLParams
& params
,
312 content::WebContents
* guest_web_contents
);
314 bool HandleKeyboardShortcuts(const content::NativeWebKeyboardEvent
& event
);
316 void SetUpAutoSize();
318 ObserverList
<ScriptExecutionObserver
> script_observers_
;
319 scoped_ptr
<ScriptExecutor
> script_executor_
;
321 content::NotificationRegistrar notification_registrar_
;
323 // True if the user agent is overridden.
324 bool is_overriding_user_agent_
;
326 // Stores the window name of the main frame of the guest.
329 // Handles the JavaScript dialog requests.
330 JavaScriptDialogHelper javascript_dialog_helper_
;
332 // Handels permission requests.
333 scoped_ptr
<WebViewPermissionHelper
> web_view_permission_helper_
;
335 scoped_ptr
<WebViewGuestDelegate
> web_view_guest_delegate_
;
337 // Tracks the name, and target URL of the new window. Once the first
338 // navigation commits, we no longer track this information.
339 struct NewWindowInfo
{
343 NewWindowInfo(const GURL
& url
, const std::string
& name
) :
349 typedef std::map
<WebViewGuest
*, NewWindowInfo
> PendingWindowMap
;
350 PendingWindowMap pending_new_windows_
;
352 DISALLOW_COPY_AND_ASSIGN(WebViewGuest
);
355 } // namespace extensions
357 #endif // EXTENSIONS_BROWSER_GUEST_VIEW_WEB_VIEW_WEB_VIEW_GUEST_H_