[ServiceWorker] Implement WebServiceWorkerContextClient::openWindow().
[chromium-blink-merge.git] / extensions / browser / guest_view / web_view / web_view_guest.h
bloba7ccfffba570f867ffbf32dcdfb4d1d1e79acd17
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_
8 #include <vector>
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"
22 namespace blink {
23 struct WebFindOptions;
24 } // nanespace blink
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 {
38 public:
39 static GuestViewBase* Create(content::WebContents* owner_web_contents);
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,
51 bool* in_memory);
53 // Returns guestview::kInstanceIDNone if |contents| does not correspond to a
54 // WebViewGuest.
55 static int GetViewInstanceId(content::WebContents* contents);
57 static const char Type[];
59 // Return the stored rules registry ID of the given webview. Will generate
60 // an ID for the first query.
61 static int GetOrGenerateRulesRegistryID(
62 int embedder_process_id,
63 int web_view_instance_id);
65 // Request navigating the guest to the provided |src| URL.
66 void NavigateGuest(const std::string& src, bool force_navigation);
68 // Shows the context menu for the guest.
69 // |items| acts as a filter. This restricts the current context's default
70 // menu items to contain only the items from |items|.
71 // |items| == NULL means no filtering will be applied.
72 void ShowContextMenu(
73 int request_id,
74 const WebViewGuestDelegate::MenuItemVector* items);
76 // Sets the frame name of the guest.
77 void SetName(const std::string& name);
79 // Set the zoom factor.
80 void SetZoom(double zoom_factor);
82 // Sets the transparency of the guest.
83 void SetAllowTransparency(bool allow);
85 // Loads a data URL with a specified base URL and virtual URL.
86 bool LoadDataWithBaseURL(const std::string& data_url,
87 const std::string& base_url,
88 const std::string& virtual_url,
89 std::string* error);
91 // GuestViewBase implementation.
92 bool CanRunInDetachedState() const override;
93 void CreateWebContents(const base::DictionaryValue& create_params,
94 const WebContentsCreatedCallback& callback) override;
95 void DidAttachToEmbedder() override;
96 void DidInitialize(const base::DictionaryValue& create_params) override;
97 void DidStopLoading() override;
98 void EmbedderWillBeDestroyed() override;
99 const char* GetAPINamespace() const override;
100 int GetTaskPrefix() const override;
101 void GuestDestroyed() override;
102 void GuestReady() override;
103 void GuestSizeChangedDueToAutoSize(const gfx::Size& old_size,
104 const gfx::Size& new_size) override;
105 bool IsAutoSizeSupported() const override;
106 bool IsDragAndDropEnabled() const override;
107 void WillAttachToEmbedder() override;
108 void WillDestroy() override;
110 // WebContentsDelegate implementation.
111 bool AddMessageToConsole(content::WebContents* source,
112 int32 level,
113 const base::string16& message,
114 int32 line_no,
115 const base::string16& source_id) override;
116 void LoadProgressChanged(content::WebContents* source,
117 double progress) override;
118 void CloseContents(content::WebContents* source) override;
119 void FindReply(content::WebContents* source,
120 int request_id,
121 int number_of_matches,
122 const gfx::Rect& selection_rect,
123 int active_match_ordinal,
124 bool final_update) override;
125 bool HandleContextMenu(const content::ContextMenuParams& params) override;
126 void HandleKeyboardEvent(
127 content::WebContents* source,
128 const content::NativeWebKeyboardEvent& event) override;
129 void RendererResponsive(content::WebContents* source) override;
130 void RendererUnresponsive(content::WebContents* source) override;
131 void RequestMediaAccessPermission(
132 content::WebContents* source,
133 const content::MediaStreamRequest& request,
134 const content::MediaResponseCallback& callback) override;
135 void RequestPointerLockPermission(
136 bool user_gesture,
137 bool last_unlocked_by_target,
138 const base::Callback<void(bool)>& callback) override;
139 bool CheckMediaAccessPermission(content::WebContents* source,
140 const GURL& security_origin,
141 content::MediaStreamType type) override;
142 void CanDownload(content::RenderViewHost* render_view_host,
143 const GURL& url,
144 const std::string& request_method,
145 const base::Callback<void(bool)>& callback) override;
146 content::JavaScriptDialogManager* GetJavaScriptDialogManager(
147 content::WebContents* source) override;
148 void AddNewContents(content::WebContents* source,
149 content::WebContents* new_contents,
150 WindowOpenDisposition disposition,
151 const gfx::Rect& initial_rect,
152 bool user_gesture,
153 bool* was_blocked) override;
154 content::WebContents* OpenURLFromTab(
155 content::WebContents* source,
156 const content::OpenURLParams& params) override;
157 void WebContentsCreated(content::WebContents* source_contents,
158 int opener_render_frame_id,
159 const base::string16& frame_name,
160 const GURL& target_url,
161 content::WebContents* new_contents) override;
163 // NotificationObserver implementation.
164 void Observe(int type,
165 const content::NotificationSource& source,
166 const content::NotificationDetails& details) override;
168 // Returns the current zoom factor.
169 double zoom() const { return current_zoom_factor_; }
171 // Begin or continue a find request.
172 void StartFinding(const base::string16& search_text,
173 const blink::WebFindOptions& options,
174 scoped_refptr<WebViewInternalFindFunction> find_function);
176 // Conclude a find request to clear highlighting.
177 void StopFinding(content::StopFindAction);
179 // If possible, navigate the guest to |relative_index| entries away from the
180 // current navigation entry. Returns true on success.
181 bool Go(int relative_index);
183 // Reload the guest.
184 void Reload();
186 using PermissionResponseCallback =
187 base::Callback<void(bool /* allow */,
188 const std::string& /* user_input */)>;
189 int RequestPermission(
190 WebViewPermissionType permission_type,
191 const base::DictionaryValue& request_info,
192 const PermissionResponseCallback& callback,
193 bool allowed_by_default);
195 // Requests Geolocation Permission from the embedder.
196 void RequestGeolocationPermission(int bridge_id,
197 const GURL& requesting_frame,
198 bool user_gesture,
199 const base::Callback<void(bool)>& callback);
200 void CancelGeolocationPermissionRequest(int bridge_id);
202 // Called when file system access is requested by the guest content using the
203 // HTML5 file system API in main thread, or a worker thread.
204 // The request is plumbed through the <webview> permission request API. The
205 // request will be:
206 // - Allowed if the embedder explicitly allowed it.
207 // - Denied if the embedder explicitly denied.
208 // - Determined by the guest's content settings if the embedder does not
209 // perform an explicit action.
210 void RequestFileSystemPermission(const GURL& url,
211 bool allowed_by_default,
212 const base::Callback<void(bool)>& callback);
214 // Overrides the user agent for this guest.
215 // This affects subsequent guest navigations.
216 void SetUserAgentOverride(const std::string& user_agent_override);
218 // Stop loading the guest.
219 void Stop();
221 // Kill the guest process.
222 void Terminate();
224 // Clears data in the storage partition of this guest.
226 // Partition data that are newer than |removal_since| will be removed.
227 // |removal_mask| corresponds to bitmask in StoragePartition::RemoveDataMask.
228 bool ClearData(const base::Time remove_since,
229 uint32 removal_mask,
230 const base::Closure& callback);
232 ScriptExecutor* script_executor() { return script_executor_.get(); }
234 private:
235 friend class WebViewPermissionHelper;
237 explicit WebViewGuest(content::WebContents* owner_web_contents);
239 ~WebViewGuest() override;
241 void AttachWebViewHelpers(content::WebContents* contents);
243 void OnWebViewNewWindowResponse(int new_window_instance_id,
244 bool allow,
245 const std::string& user_input);
247 // WebContentsObserver implementation.
248 void DidCommitProvisionalLoadForFrame(
249 content::RenderFrameHost* render_frame_host,
250 const GURL& url,
251 ui::PageTransition transition_type) override;
252 void DidFailProvisionalLoad(content::RenderFrameHost* render_frame_host,
253 const GURL& validated_url,
254 int error_code,
255 const base::string16& error_description) override;
256 void DidStartProvisionalLoadForFrame(
257 content::RenderFrameHost* render_frame_host,
258 const GURL& validated_url,
259 bool is_error_page,
260 bool is_iframe_srcdoc) override;
261 void DocumentLoadedInFrame(
262 content::RenderFrameHost* render_frame_host) override;
263 bool OnMessageReceived(const IPC::Message& message,
264 content::RenderFrameHost* render_frame_host) override;
265 void RenderProcessGone(base::TerminationStatus status) override;
266 void UserAgentOverrideSet(const std::string& user_agent) override;
268 // Informs the embedder of a frame name change.
269 void ReportFrameNameChange(const std::string& name);
271 // Called after the load handler is called in the guest's main frame.
272 void LoadHandlerCalled();
274 // Called when a redirect notification occurs.
275 void LoadRedirect(const GURL& old_url,
276 const GURL& new_url,
277 bool is_top_level);
279 void PushWebViewStateToIOThread();
280 static void RemoveWebViewStateFromIOThread(
281 content::WebContents* web_contents);
283 void LoadURLWithParams(const GURL& url,
284 const content::Referrer& referrer,
285 ui::PageTransition transition_type,
286 content::WebContents* web_contents);
288 void RequestNewWindowPermission(
289 WindowOpenDisposition disposition,
290 const gfx::Rect& initial_bounds,
291 bool user_gesture,
292 content::WebContents* new_contents);
294 // Requests resolution of a potentially relative URL.
295 GURL ResolveURL(const std::string& src);
297 // Notification that a load in the guest resulted in abort. Note that |url|
298 // may be invalid.
299 void LoadAbort(bool is_top_level,
300 const GURL& url,
301 const std::string& error_type);
303 void OnFrameNameChanged(bool is_top_level, const std::string& name);
305 // Creates a new guest window owned by this WebViewGuest.
306 void CreateNewGuestWebViewWindow(const content::OpenURLParams& params);
308 void NewGuestWebViewCallback(const content::OpenURLParams& params,
309 content::WebContents* guest_web_contents);
311 bool HandleKeyboardShortcuts(const content::NativeWebKeyboardEvent& event);
313 void ApplyAttributes(const base::DictionaryValue& params);
315 // Identifies the set of rules registries belonging to this guest.
316 int rules_registry_id_;
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.
330 std::string name_;
332 // Stores whether the contents of the guest can be transparent.
333 bool guest_opaque_;
335 // Stores the src URL of the WebView.
336 GURL src_;
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 {
349 GURL url;
350 std::string name;
351 bool changed;
352 NewWindowInfo(const GURL& url, const std::string& name) :
353 url(url),
354 name(name),
355 changed(false) {}
358 using PendingWindowMap = std::map<WebViewGuest*, NewWindowInfo>;
359 PendingWindowMap pending_new_windows_;
361 // Stores the current zoom factor.
362 double current_zoom_factor_;
364 // This is used to ensure pending tasks will not fire after this object is
365 // destroyed.
366 base::WeakPtrFactory<WebViewGuest> weak_ptr_factory_;
368 DISALLOW_COPY_AND_ASSIGN(WebViewGuest);
371 } // namespace extensions
373 #endif // EXTENSIONS_BROWSER_GUEST_VIEW_WEB_VIEW_WEB_VIEW_GUEST_H_