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, bool force_navigation);
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 // 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,
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;
107 // WebContentsDelegate implementation.
108 bool AddMessageToConsole(content::WebContents* source,
110 const base::string16& message,
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,
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,
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,
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,
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;
159 // BrowserPluginGuestDelegate implementation.
160 content::WebContents* CreateNewGuestWindow(
161 const content::WebContents::CreateParams& create_params) override;
162 void RequestPointerLockPermission(
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;
171 // Returns the current zoom factor.
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);
179 // Conclude a find request to clear highlighting.
180 void StopFinding(content::StopFindAction);
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);
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);
198 // Requests Geolocation Permission from the embedder.
199 void RequestGeolocationPermission(int bridge_id,
200 const GURL& requesting_frame,
202 const base::Callback<void(bool)>& callback);
203 void CancelGeolocationPermissionRequest(int bridge_id);
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
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);
217 // Overrides the user agent for this guest.
218 // This affects subsequent guest navigations.
219 void SetUserAgentOverride(const std::string& user_agent_override);
221 // Stop loading the guest.
224 // Kill the guest process.
227 // Clears data in the storage partition of this guest.
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,
233 const base::Closure& callback);
235 ScriptExecutor* script_executor() { return script_executor_.get(); }
238 friend class WebViewPermissionHelper;
239 WebViewGuest(content::BrowserContext* browser_context,
240 int guest_instance_id);
242 ~WebViewGuest() override;
244 void AttachWebViewHelpers(content::WebContents* contents);
246 void OnWebViewNewWindowResponse(int new_window_instance_id,
248 const std::string& user_input);
250 // WebContentsObserver implementation.
251 void DidCommitProvisionalLoadForFrame(
252 content::RenderFrameHost* render_frame_host,
254 ui::PageTransition transition_type) override;
255 void DidFailProvisionalLoad(content::RenderFrameHost* render_frame_host,
256 const GURL& validated_url,
258 const base::string16& error_description) override;
259 void DidStartProvisionalLoadForFrame(
260 content::RenderFrameHost* render_frame_host,
261 const GURL& validated_url,
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;
271 // Informs the embedder of a frame name change.
272 void ReportFrameNameChange(const std::string& name);
274 // Called after the load handler is called in the guest's main frame.
275 void LoadHandlerCalled();
277 // Called when a redirect notification occurs.
278 void LoadRedirect(const GURL& old_url,
282 void PushWebViewStateToIOThread();
283 static void RemoveWebViewStateFromIOThread(
284 content::WebContents* web_contents);
286 void LoadURLWithParams(const GURL& url,
287 const content::Referrer& referrer,
288 ui::PageTransition transition_type,
289 content::WebContents* web_contents);
291 void RequestNewWindowPermission(
292 WindowOpenDisposition disposition,
293 const gfx::Rect& initial_bounds,
295 content::WebContents* new_contents);
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 // Handles find requests and replies for the webview find API.
319 WebViewFindHelper find_helper_;
321 ObserverList<ScriptExecutionObserver> script_observers_;
322 scoped_ptr<ScriptExecutor> script_executor_;
324 content::NotificationRegistrar notification_registrar_;
326 // True if the user agent is overridden.
327 bool is_overriding_user_agent_;
329 // Stores the window name of the main frame of the guest.
332 // Stores whether the contents of the guest can be transparent.
335 // Stores the src URL of the WebView.
338 // Handles the JavaScript dialog requests.
339 JavaScriptDialogHelper javascript_dialog_helper_;
341 // Handles permission requests.
342 scoped_ptr<WebViewPermissionHelper> web_view_permission_helper_;
344 scoped_ptr<WebViewGuestDelegate> web_view_guest_delegate_;
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 {
352 NewWindowInfo(const GURL& url, const std::string& name) :
358 typedef std::map<WebViewGuest*, NewWindowInfo> PendingWindowMap;
359 PendingWindowMap pending_new_windows_;
361 // This is used to ensure pending tasks will not fire after this object is
363 base::WeakPtrFactory<WebViewGuest> weak_ptr_factory_;
365 DISALLOW_COPY_AND_ASSIGN(WebViewGuest);
368 } // namespace extensions
370 #endif // EXTENSIONS_BROWSER_GUEST_VIEW_WEB_VIEW_WEB_VIEW_GUEST_H_