1 // Copyright (c) 2013 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 CHROME_BROWSER_EXTENSIONS_API_WEBVIEW_WEBVIEW_API_H_ 6 #define CHROME_BROWSER_EXTENSIONS_API_WEBVIEW_WEBVIEW_API_H_ 7 8 #include "chrome/browser/extensions/api/capture_web_contents_function.h" 9 #include "chrome/browser/extensions/api/execute_code_function.h" 10 #include "chrome/browser/guestview/webview/webview_guest.h" 11 12 // WARNING: Webview could be loaded in an unblessed context, thus any new 13 // APIs must extend WebviewExtensionFunction/WebviewExecuteCodeFunction which 14 // do a process ID check to prevent abuse by normal renderer processes. 15 // TODO(guohui): refactor WebviewExecuteCodeFunction to also extend 16 // WebviewExtensionFunction. 17 namespace extensions { 18 19 // An abstract base class for async webview APIs. It does a process ID check 20 // in RunImpl, and then calls RunImplSafe which must be overriden by all 21 // subclasses. 22 class WebviewExtensionFunction : public AsyncExtensionFunction { 23 public: WebviewExtensionFunction()24 WebviewExtensionFunction() {} 25 26 protected: ~WebviewExtensionFunction()27 virtual ~WebviewExtensionFunction() {} 28 29 // ExtensionFunction implementation. 30 virtual bool RunImpl() OVERRIDE FINAL; 31 32 private: 33 virtual bool RunImplSafe(WebViewGuest* guest) = 0; 34 }; 35 36 class WebviewClearDataFunction : public WebviewExtensionFunction { 37 public: 38 DECLARE_EXTENSION_FUNCTION("webview.clearData", WEBVIEW_CLEARDATA); 39 40 WebviewClearDataFunction(); 41 42 protected: 43 virtual ~WebviewClearDataFunction(); 44 45 private: 46 // WebviewExtensionFunction implementation. 47 virtual bool RunImplSafe(WebViewGuest* guest) OVERRIDE; 48 49 uint32 GetRemovalMask(); 50 void ClearDataDone(); 51 52 // Removal start time. 53 base::Time remove_since_; 54 // Removal mask, corresponds to StoragePartition::RemoveDataMask enum. 55 uint32 remove_mask_; 56 // Tracks any data related or parse errors. 57 bool bad_message_; 58 59 DISALLOW_COPY_AND_ASSIGN(WebviewClearDataFunction); 60 }; 61 62 class WebviewExecuteCodeFunction : public extensions::ExecuteCodeFunction { 63 public: 64 WebviewExecuteCodeFunction(); 65 66 protected: 67 virtual ~WebviewExecuteCodeFunction(); 68 69 // Initialize |details_| if it hasn't already been. 70 virtual bool Init() OVERRIDE; 71 virtual bool ShouldInsertCSS() const OVERRIDE; 72 virtual bool CanExecuteScriptOnPage() OVERRIDE; 73 // Guarded by a process ID check. 74 virtual extensions::ScriptExecutor* GetScriptExecutor() OVERRIDE FINAL; 75 virtual bool IsWebView() const OVERRIDE; 76 77 private: 78 // Contains extension resource built from path of file which is 79 // specified in JSON arguments. 80 extensions::ExtensionResource resource_; 81 82 int guest_instance_id_; 83 84 DISALLOW_COPY_AND_ASSIGN(WebviewExecuteCodeFunction); 85 }; 86 87 class WebviewExecuteScriptFunction : public WebviewExecuteCodeFunction { 88 public: 89 WebviewExecuteScriptFunction(); 90 91 protected: ~WebviewExecuteScriptFunction()92 virtual ~WebviewExecuteScriptFunction() {} 93 94 virtual void OnExecuteCodeFinished(const std::string& error, 95 int32 on_page_id, 96 const GURL& on_url, 97 const base::ListValue& result) OVERRIDE; 98 99 DECLARE_EXTENSION_FUNCTION("webview.executeScript", WEBVIEW_EXECUTESCRIPT) 100 101 private: 102 DISALLOW_COPY_AND_ASSIGN(WebviewExecuteScriptFunction); 103 }; 104 105 class WebviewInsertCSSFunction : public WebviewExecuteCodeFunction { 106 public: 107 WebviewInsertCSSFunction(); 108 109 protected: ~WebviewInsertCSSFunction()110 virtual ~WebviewInsertCSSFunction() {} 111 112 virtual bool ShouldInsertCSS() const OVERRIDE; 113 114 DECLARE_EXTENSION_FUNCTION("webview.insertCSS", WEBVIEW_INSERTCSS) 115 116 private: 117 DISALLOW_COPY_AND_ASSIGN(WebviewInsertCSSFunction); 118 }; 119 120 class WebviewCaptureVisibleRegionFunction 121 : public extensions::CaptureWebContentsFunction { 122 DECLARE_EXTENSION_FUNCTION("webview.captureVisibleRegion", 123 WEBVIEW_CAPTUREVISIBLEREGION); 124 125 WebviewCaptureVisibleRegionFunction(); 126 127 protected: 128 virtual ~WebviewCaptureVisibleRegionFunction(); 129 130 private: 131 // extensions::CaptureWebContentsFunction implementation. 132 virtual bool IsScreenshotEnabled() OVERRIDE; 133 virtual content::WebContents* GetWebContentsForID(int id) OVERRIDE; 134 virtual void OnCaptureFailure(FailureReason reason) OVERRIDE; 135 136 DISALLOW_COPY_AND_ASSIGN(WebviewCaptureVisibleRegionFunction); 137 }; 138 139 class WebviewGoFunction : public WebviewExtensionFunction { 140 public: 141 DECLARE_EXTENSION_FUNCTION("webview.go", WEBVIEW_GO); 142 143 WebviewGoFunction(); 144 145 protected: 146 virtual ~WebviewGoFunction(); 147 148 private: 149 // WebviewExtensionFunction implementation. 150 virtual bool RunImplSafe(WebViewGuest* guest) OVERRIDE; 151 152 DISALLOW_COPY_AND_ASSIGN(WebviewGoFunction); 153 }; 154 155 class WebviewReloadFunction : public WebviewExtensionFunction { 156 public: 157 DECLARE_EXTENSION_FUNCTION("webview.reload", WEBVIEW_RELOAD); 158 159 WebviewReloadFunction(); 160 161 protected: 162 virtual ~WebviewReloadFunction(); 163 164 private: 165 // WebviewExtensionFunction implementation. 166 virtual bool RunImplSafe(WebViewGuest* guest) OVERRIDE; 167 168 DISALLOW_COPY_AND_ASSIGN(WebviewReloadFunction); 169 }; 170 171 class WebviewSetPermissionFunction : public WebviewExtensionFunction { 172 public: 173 DECLARE_EXTENSION_FUNCTION("webview.setPermission", WEBVIEW_SETPERMISSION); 174 175 WebviewSetPermissionFunction(); 176 177 protected: 178 virtual ~WebviewSetPermissionFunction(); 179 180 private: 181 // WebviewExtensionFunction implementation. 182 virtual bool RunImplSafe(WebViewGuest* guest) OVERRIDE; 183 184 DISALLOW_COPY_AND_ASSIGN(WebviewSetPermissionFunction); 185 }; 186 187 class WebviewOverrideUserAgentFunction: public WebviewExtensionFunction { 188 public: 189 DECLARE_EXTENSION_FUNCTION("webview.overrideUserAgent", 190 WEBVIEW_OVERRIDEUSERAGENT); 191 192 WebviewOverrideUserAgentFunction(); 193 194 protected: 195 virtual ~WebviewOverrideUserAgentFunction(); 196 197 private: 198 // WebviewExtensionFunction implementation. 199 virtual bool RunImplSafe(WebViewGuest* guest) OVERRIDE; 200 201 DISALLOW_COPY_AND_ASSIGN(WebviewOverrideUserAgentFunction); 202 }; 203 204 class WebviewStopFunction : public WebviewExtensionFunction { 205 public: 206 DECLARE_EXTENSION_FUNCTION("webview.stop", WEBVIEW_STOP); 207 208 WebviewStopFunction(); 209 210 protected: 211 virtual ~WebviewStopFunction(); 212 213 private: 214 // WebviewExtensionFunction implementation. 215 virtual bool RunImplSafe(WebViewGuest* guest) OVERRIDE; 216 217 DISALLOW_COPY_AND_ASSIGN(WebviewStopFunction); 218 }; 219 220 class WebviewTerminateFunction : public WebviewExtensionFunction { 221 public: 222 DECLARE_EXTENSION_FUNCTION("webview.terminate", WEBVIEW_TERMINATE); 223 224 WebviewTerminateFunction(); 225 226 protected: 227 virtual ~WebviewTerminateFunction(); 228 229 private: 230 // WebviewExtensionFunction implementation. 231 virtual bool RunImplSafe(WebViewGuest* guest) OVERRIDE; 232 233 DISALLOW_COPY_AND_ASSIGN(WebviewTerminateFunction); 234 }; 235 236 } // namespace extensions 237 238 #endif // CHROME_BROWSER_EXTENSIONS_API_WEBVIEW_WEBVIEW_API_H_ 239