| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #ifndef EXTENSIONS_BROWSER_GUEST_VIEW_WEB_VIEW_WEB_VIEW_GUEST_H_ | |
| 6 #define EXTENSIONS_BROWSER_GUEST_VIEW_WEB_VIEW_WEB_VIEW_GUEST_H_ | |
| 7 | |
| 8 #include <vector> | |
| 9 | |
| 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" | |
| 21 | |
| 22 namespace blink { | |
| 23 struct WebFindOptions; | |
| 24 } // nanespace blink | |
| 25 | |
| 26 namespace extensions { | |
| 27 | |
| 28 class WebViewInternalFindFunction; | |
| 29 | |
| 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::BrowserContext* browser_context, | |
| 40 int guest_instance_id); | |
| 41 | |
| 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, | |
| 52 bool* in_memory); | |
| 53 | |
| 54 // Returns guestview::kInstanceIDNone if |contents| does not correspond to a | |
| 55 // WebViewGuest. | |
| 56 static int GetViewInstanceId(content::WebContents* contents); | |
| 57 | |
| 58 static const char Type[]; | |
| 59 | |
| 60 // Request navigating the guest to the provided |src| URL. | |
| 61 void NavigateGuest(const std::string& src); | |
| 62 | |
| 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. | |
| 67 void ShowContextMenu( | |
| 68 int request_id, | |
| 69 const WebViewGuestDelegate::MenuItemVector* items); | |
| 70 | |
| 71 // Sets the frame name of the guest. | |
| 72 void SetName(const std::string& name); | |
| 73 | |
| 74 // Set the zoom factor. | |
| 75 void SetZoom(double zoom_factor); | |
| 76 | |
| 77 // GuestViewBase implementation. | |
| 78 virtual const char* GetAPINamespace() 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; | |
| 97 | |
| 98 // WebContentsDelegate implementation. | |
| 99 virtual bool AddMessageToConsole(content::WebContents* source, | |
| 100 int32 level, | |
| 101 const base::string16& message, | |
| 102 int32 line_no, | |
| 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, | |
| 108 int request_id, | |
| 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, | |
| 125 const GURL& url, | |
| 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, | |
| 132 SkColor color, | |
| 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, | |
| 141 bool user_gesture, | |
| 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; | |
| 151 | |
| 152 // BrowserPluginGuestDelegate implementation. | |
| 153 virtual content::WebContents* CreateNewGuestWindow( | |
| 154 const content::WebContents::CreateParams& create_params) OVERRIDE; | |
| 155 virtual void RequestPointerLockPermission( | |
| 156 bool user_gesture, | |
| 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; | |
| 163 | |
| 164 // Returns the current zoom factor. | |
| 165 double GetZoom(); | |
| 166 | |
| 167 // Begin or continue a find request. | |
| 168 void Find( | |
| 169 const base::string16& search_text, | |
| 170 const blink::WebFindOptions& options, | |
| 171 scoped_refptr<WebViewInternalFindFunction> find_function); | |
| 172 | |
| 173 // Conclude a find request to clear highlighting. | |
| 174 void StopFinding(content::StopFindAction); | |
| 175 | |
| 176 // If possible, navigate the guest to |relative_index| entries away from the | |
| 177 // current navigation entry. | |
| 178 void Go(int relative_index); | |
| 179 | |
| 180 // Reload the guest. | |
| 181 void Reload(); | |
| 182 | |
| 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); | |
| 191 | |
| 192 // Requests Geolocation Permission from the embedder. | |
| 193 void RequestGeolocationPermission(int bridge_id, | |
| 194 const GURL& requesting_frame, | |
| 195 bool user_gesture, | |
| 196 const base::Callback<void(bool)>& callback); | |
| 197 void CancelGeolocationPermissionRequest(int bridge_id); | |
| 198 | |
| 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 | |
| 202 // request will be: | |
| 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); | |
| 210 | |
| 211 // Overrides the user agent for this guest. | |
| 212 // This affects subsequent guest navigations. | |
| 213 void SetUserAgentOverride(const std::string& user_agent_override); | |
| 214 | |
| 215 // Stop loading the guest. | |
| 216 void Stop(); | |
| 217 | |
| 218 // Kill the guest process. | |
| 219 void Terminate(); | |
| 220 | |
| 221 // Clears data in the storage partition of this guest. | |
| 222 // | |
| 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, | |
| 226 uint32 removal_mask, | |
| 227 const base::Closure& callback); | |
| 228 | |
| 229 ScriptExecutor* script_executor() { return script_executor_.get(); } | |
| 230 | |
| 231 private: | |
| 232 friend class WebViewPermissionHelper; | |
| 233 WebViewGuest(content::BrowserContext* browser_context, | |
| 234 int guest_instance_id); | |
| 235 | |
| 236 virtual ~WebViewGuest(); | |
| 237 | |
| 238 void AttachWebViewHelpers(content::WebContents* contents); | |
| 239 | |
| 240 void OnWebViewNewWindowResponse(int new_window_instance_id, | |
| 241 bool allow, | |
| 242 const std::string& user_input); | |
| 243 | |
| 244 // WebContentsObserver implementation. | |
| 245 virtual void DidCommitProvisionalLoadForFrame( | |
| 246 content::RenderFrameHost* render_frame_host, | |
| 247 const GURL& url, | |
| 248 content::PageTransition transition_type) OVERRIDE; | |
| 249 virtual void DidFailProvisionalLoad( | |
| 250 content::RenderFrameHost* render_frame_host, | |
| 251 const GURL& validated_url, | |
| 252 int error_code, | |
| 253 const base::string16& error_description) OVERRIDE; | |
| 254 virtual void DidStartProvisionalLoadForFrame( | |
| 255 content::RenderFrameHost* render_frame_host, | |
| 256 const GURL& validated_url, | |
| 257 bool is_error_page, | |
| 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; | |
| 266 | |
| 267 // Informs the embedder of a frame name change. | |
| 268 void ReportFrameNameChange(const std::string& name); | |
| 269 | |
| 270 // Called after the load handler is called in the guest's main frame. | |
| 271 void LoadHandlerCalled(); | |
| 272 | |
| 273 // Called when a redirect notification occurs. | |
| 274 void LoadRedirect(const GURL& old_url, | |
| 275 const GURL& new_url, | |
| 276 bool is_top_level); | |
| 277 | |
| 278 void PushWebViewStateToIOThread(); | |
| 279 static void RemoveWebViewStateFromIOThread( | |
| 280 content::WebContents* web_contents); | |
| 281 | |
| 282 void LoadURLWithParams(const GURL& url, | |
| 283 const content::Referrer& referrer, | |
| 284 content::PageTransition transition_type, | |
| 285 content::WebContents* web_contents); | |
| 286 | |
| 287 void RequestNewWindowPermission( | |
| 288 WindowOpenDisposition disposition, | |
| 289 const gfx::Rect& initial_bounds, | |
| 290 bool user_gesture, | |
| 291 content::WebContents* new_contents); | |
| 292 | |
| 293 // Destroy unattached new windows that have been opened by this | |
| 294 // WebViewGuest. | |
| 295 void DestroyUnattachedWindows(); | |
| 296 | |
| 297 // Requests resolution of a potentially relative URL. | |
| 298 GURL ResolveURL(const std::string& src); | |
| 299 | |
| 300 // Notification that a load in the guest resulted in abort. Note that |url| | |
| 301 // may be invalid. | |
| 302 void LoadAbort(bool is_top_level, | |
| 303 const GURL& url, | |
| 304 const std::string& error_type); | |
| 305 | |
| 306 void OnFrameNameChanged(bool is_top_level, const std::string& name); | |
| 307 | |
| 308 // Creates a new guest window owned by this WebViewGuest. | |
| 309 void CreateNewGuestWebViewWindow(const content::OpenURLParams& params); | |
| 310 | |
| 311 void NewGuestWebViewCallback(const content::OpenURLParams& params, | |
| 312 content::WebContents* guest_web_contents); | |
| 313 | |
| 314 bool HandleKeyboardShortcuts(const content::NativeWebKeyboardEvent& event); | |
| 315 | |
| 316 void SetUpAutoSize(); | |
| 317 | |
| 318 ObserverList<ScriptExecutionObserver> script_observers_; | |
| 319 scoped_ptr<ScriptExecutor> script_executor_; | |
| 320 | |
| 321 content::NotificationRegistrar notification_registrar_; | |
| 322 | |
| 323 // True if the user agent is overridden. | |
| 324 bool is_overriding_user_agent_; | |
| 325 | |
| 326 // Stores the window name of the main frame of the guest. | |
| 327 std::string name_; | |
| 328 | |
| 329 // Handles find requests and replies for the webview find API. | |
| 330 WebViewFindHelper find_helper_; | |
| 331 | |
| 332 // Handles the JavaScript dialog requests. | |
| 333 JavaScriptDialogHelper javascript_dialog_helper_; | |
| 334 | |
| 335 // Handels permission requests. | |
| 336 scoped_ptr<WebViewPermissionHelper> web_view_permission_helper_; | |
| 337 | |
| 338 scoped_ptr<WebViewGuestDelegate> web_view_guest_delegate_; | |
| 339 | |
| 340 friend void WebViewFindHelper::DispatchFindUpdateEvent(bool canceled, | |
| 341 bool final_update); | |
| 342 | |
| 343 // Tracks the name, and target URL of the new window. Once the first | |
| 344 // navigation commits, we no longer track this information. | |
| 345 struct NewWindowInfo { | |
| 346 GURL url; | |
| 347 std::string name; | |
| 348 bool changed; | |
| 349 NewWindowInfo(const GURL& url, const std::string& name) : | |
| 350 url(url), | |
| 351 name(name), | |
| 352 changed(false) {} | |
| 353 }; | |
| 354 | |
| 355 typedef std::map<WebViewGuest*, NewWindowInfo> PendingWindowMap; | |
| 356 PendingWindowMap pending_new_windows_; | |
| 357 | |
| 358 DISALLOW_COPY_AND_ASSIGN(WebViewGuest); | |
| 359 }; | |
| 360 | |
| 361 } // namespace extensions | |
| 362 | |
| 363 #endif // EXTENSIONS_BROWSER_GUEST_VIEW_WEB_VIEW_WEB_VIEW_GUEST_H_ | |
| OLD | NEW |