1 // Copyright (c) 2012 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 CEF_LIBCEF_BROWSER_EXTENSIONS_API_TABS_TABS_API_H_ 6 #define CEF_LIBCEF_BROWSER_EXTENSIONS_API_TABS_TABS_API_H_ 7 8 #include "libcef/browser/extensions/extension_function_details.h" 9 10 #include "chrome/common/extensions/api/tabs.h" 11 #include "extensions/browser/api/execute_code_function.h" 12 #include "extensions/browser/extension_function.h" 13 14 // The contents of this file are extracted from 15 // chrome/browser/extensions/api/tabs/tabs_api.h. 16 17 namespace content { 18 class WebContents; 19 } 20 21 namespace extensions { 22 namespace cef { 23 24 class TabsGetFunction : public ExtensionFunction { ~TabsGetFunction()25 ~TabsGetFunction() override {} 26 27 ResponseAction Run() override; 28 29 DECLARE_EXTENSION_FUNCTION("tabs.get", TABS_GET) 30 }; 31 32 class TabsCreateFunction : public ExtensionFunction { 33 public: 34 TabsCreateFunction(); ~TabsCreateFunction()35 ~TabsCreateFunction() override {} 36 37 ResponseAction Run() override; 38 39 DECLARE_EXTENSION_FUNCTION("tabs.create", TABS_CREATE) 40 41 private: 42 const CefExtensionFunctionDetails cef_details_; 43 }; 44 45 class BaseAPIFunction : public ExtensionFunction { 46 public: 47 BaseAPIFunction(); 48 49 protected: ~BaseAPIFunction()50 ~BaseAPIFunction() override {} 51 52 // Gets the WebContents for |tab_id| if it is specified. Otherwise get the 53 // WebContents for the active tab in the current window. Calling this function 54 // may set |error_|. 55 content::WebContents* GetWebContents(int tab_id); 56 57 std::string error_; 58 const CefExtensionFunctionDetails cef_details_; 59 }; 60 61 class TabsUpdateFunction : public BaseAPIFunction { 62 private: ~TabsUpdateFunction()63 ~TabsUpdateFunction() override {} 64 65 ResponseAction Run() override; 66 67 bool UpdateURL(const std::string& url, int tab_id, std::string* error); 68 ResponseValue GetResult(); 69 70 DECLARE_EXTENSION_FUNCTION("tabs.update", TABS_UPDATE) 71 72 int tab_id_ = -1; 73 content::WebContents* web_contents_ = nullptr; 74 }; 75 76 // Implement API calls tabs.executeScript, tabs.insertCSS, and tabs.removeCSS. 77 class ExecuteCodeInTabFunction : public ExecuteCodeFunction { 78 public: 79 ExecuteCodeInTabFunction(); 80 81 protected: 82 ~ExecuteCodeInTabFunction() override; 83 84 // Initializes |execute_tab_id_| and |details_|. 85 InitResult Init() override; 86 bool ShouldInsertCSS() const override; 87 bool ShouldRemoveCSS() const override; 88 bool CanExecuteScriptOnPage(std::string* error) override; 89 ScriptExecutor* GetScriptExecutor(std::string* error) override; 90 bool IsWebView() const override; 91 const GURL& GetWebViewSrc() const override; 92 bool LoadFile(const std::string& file, std::string* error) override; 93 94 private: 95 const CefExtensionFunctionDetails cef_details_; 96 97 void LoadFileComplete(const std::string& file, 98 std::unique_ptr<std::string> data); 99 100 // Id of tab which executes code. 101 int execute_tab_id_; 102 }; 103 104 class TabsExecuteScriptFunction : public ExecuteCodeInTabFunction { 105 private: ~TabsExecuteScriptFunction()106 ~TabsExecuteScriptFunction() override {} 107 108 DECLARE_EXTENSION_FUNCTION("tabs.executeScript", TABS_EXECUTESCRIPT) 109 }; 110 111 class TabsInsertCSSFunction : public ExecuteCodeInTabFunction { 112 private: ~TabsInsertCSSFunction()113 ~TabsInsertCSSFunction() override {} 114 115 bool ShouldInsertCSS() const override; 116 117 DECLARE_EXTENSION_FUNCTION("tabs.insertCSS", TABS_INSERTCSS) 118 }; 119 120 class TabsRemoveCSSFunction : public ExecuteCodeInTabFunction { 121 private: ~TabsRemoveCSSFunction()122 ~TabsRemoveCSSFunction() override {} 123 124 bool ShouldRemoveCSS() const override; 125 126 DECLARE_EXTENSION_FUNCTION("tabs.removeCSS", TABS_INSERTCSS) 127 }; 128 129 // Based on ChromeAsyncExtensionFunction. 130 class ZoomAPIFunction : public ExtensionFunction { 131 public: 132 ZoomAPIFunction(); 133 134 protected: ~ZoomAPIFunction()135 ~ZoomAPIFunction() override {} 136 137 // Gets the WebContents for |tab_id| if it is specified. Otherwise get the 138 // WebContents for the active tab in the current window. Calling this function 139 // may set |error_|. 140 content::WebContents* GetWebContents(int tab_id); 141 142 std::string error_; 143 144 private: 145 const CefExtensionFunctionDetails cef_details_; 146 }; 147 148 class TabsSetZoomFunction : public BaseAPIFunction { 149 private: ~TabsSetZoomFunction()150 ~TabsSetZoomFunction() override {} 151 152 ResponseAction Run() override; 153 154 DECLARE_EXTENSION_FUNCTION("tabs.setZoom", TABS_SETZOOM) 155 }; 156 157 class TabsGetZoomFunction : public BaseAPIFunction { 158 private: ~TabsGetZoomFunction()159 ~TabsGetZoomFunction() override {} 160 161 ResponseAction Run() override; 162 163 DECLARE_EXTENSION_FUNCTION("tabs.getZoom", TABS_GETZOOM) 164 }; 165 166 class TabsSetZoomSettingsFunction : public BaseAPIFunction { 167 private: ~TabsSetZoomSettingsFunction()168 ~TabsSetZoomSettingsFunction() override {} 169 170 ResponseAction Run() override; 171 172 DECLARE_EXTENSION_FUNCTION("tabs.setZoomSettings", TABS_SETZOOMSETTINGS) 173 }; 174 175 class TabsGetZoomSettingsFunction : public BaseAPIFunction { 176 private: ~TabsGetZoomSettingsFunction()177 ~TabsGetZoomSettingsFunction() override {} 178 179 ResponseAction Run() override; 180 181 DECLARE_EXTENSION_FUNCTION("tabs.getZoomSettings", TABS_GETZOOMSETTINGS) 182 }; 183 184 } // namespace cef 185 } // namespace extensions 186 187 #endif // CEF_LIBCEF_BROWSER_EXTENSIONS_API_TABS_TABS_API_H_ 188