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.
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_TABS_TABS_API_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_TABS_TABS_API_H_
11 #include "base/compiler_specific.h"
12 #include "chrome/browser/extensions/chrome_extension_function.h"
13 #include "chrome/browser/extensions/chrome_extension_function_details.h"
14 #include "chrome/common/extensions/api/tabs.h"
15 #include "components/ui/zoom/zoom_controller.h"
16 #include "content/public/browser/notification_observer.h"
17 #include "content/public/browser/notification_registrar.h"
18 #include "extensions/browser/api/capture_web_contents_function.h"
19 #include "extensions/browser/api/execute_code_function.h"
20 #include "extensions/common/extension_resource.h"
21 #include "extensions/common/user_script.h"
29 class DictionaryValue
;
37 class ListSelectionModel
;
40 namespace user_prefs
{
41 class PrefRegistrySyncable
;
44 namespace extensions
{
46 // Converts a ZoomMode to its ZoomSettings representation.
47 void ZoomModeToZoomSettings(ui_zoom::ZoomController::ZoomMode zoom_mode
,
48 api::tabs::ZoomSettings
* zoom_settings
);
51 class WindowsGetFunction
: public ChromeSyncExtensionFunction
{
52 ~WindowsGetFunction() override
{}
53 bool RunSync() override
;
54 DECLARE_EXTENSION_FUNCTION("windows.get", WINDOWS_GET
)
56 class WindowsGetCurrentFunction
: public ChromeSyncExtensionFunction
{
57 ~WindowsGetCurrentFunction() override
{}
58 bool RunSync() override
;
59 DECLARE_EXTENSION_FUNCTION("windows.getCurrent", WINDOWS_GETCURRENT
)
61 class WindowsGetLastFocusedFunction
: public ChromeSyncExtensionFunction
{
62 ~WindowsGetLastFocusedFunction() override
{}
63 bool RunSync() override
;
64 DECLARE_EXTENSION_FUNCTION("windows.getLastFocused", WINDOWS_GETLASTFOCUSED
)
66 class WindowsGetAllFunction
: public ChromeSyncExtensionFunction
{
67 ~WindowsGetAllFunction() override
{}
68 bool RunSync() override
;
69 DECLARE_EXTENSION_FUNCTION("windows.getAll", WINDOWS_GETALL
)
71 class WindowsCreateFunction
: public ChromeSyncExtensionFunction
{
72 ~WindowsCreateFunction() override
{}
73 bool RunSync() override
;
74 // Returns whether the window should be created in incognito mode.
75 // |create_data| are the options passed by the extension. It may be NULL.
76 // |urls| is the list of urls to open. If we are creating an incognito window,
77 // the function will remove these urls which may not be opened in incognito
78 // mode. If window creation leads the browser into an erroneous state,
79 // |is_error| is set to true (also, error_ member variable is assigned
80 // the proper error message).
81 bool ShouldOpenIncognitoWindow(
82 const api::windows::Create::Params::CreateData
* create_data
,
83 std::vector
<GURL
>* urls
,
85 DECLARE_EXTENSION_FUNCTION("windows.create", WINDOWS_CREATE
)
87 class WindowsUpdateFunction
: public ChromeSyncExtensionFunction
{
88 ~WindowsUpdateFunction() override
{}
89 bool RunSync() override
;
90 DECLARE_EXTENSION_FUNCTION("windows.update", WINDOWS_UPDATE
)
92 class WindowsRemoveFunction
: public ChromeSyncExtensionFunction
{
93 ~WindowsRemoveFunction() override
{}
94 bool RunSync() override
;
95 DECLARE_EXTENSION_FUNCTION("windows.remove", WINDOWS_REMOVE
)
99 class TabsGetFunction
: public ChromeSyncExtensionFunction
{
100 ~TabsGetFunction() override
{}
101 bool RunSync() override
;
102 DECLARE_EXTENSION_FUNCTION("tabs.get", TABS_GET
)
104 class TabsGetCurrentFunction
: public ChromeSyncExtensionFunction
{
105 ~TabsGetCurrentFunction() override
{}
106 bool RunSync() override
;
107 DECLARE_EXTENSION_FUNCTION("tabs.getCurrent", TABS_GETCURRENT
)
109 class TabsGetSelectedFunction
: public ChromeSyncExtensionFunction
{
110 ~TabsGetSelectedFunction() override
{}
111 bool RunSync() override
;
112 DECLARE_EXTENSION_FUNCTION("tabs.getSelected", TABS_GETSELECTED
)
114 class TabsGetAllInWindowFunction
: public ChromeSyncExtensionFunction
{
115 ~TabsGetAllInWindowFunction() override
{}
116 bool RunSync() override
;
117 DECLARE_EXTENSION_FUNCTION("tabs.getAllInWindow", TABS_GETALLINWINDOW
)
119 class TabsQueryFunction
: public ChromeSyncExtensionFunction
{
120 ~TabsQueryFunction() override
{}
121 bool RunSync() override
;
122 DECLARE_EXTENSION_FUNCTION("tabs.query", TABS_QUERY
)
124 class TabsCreateFunction
: public ChromeSyncExtensionFunction
{
125 ~TabsCreateFunction() override
{}
126 bool RunSync() override
;
127 DECLARE_EXTENSION_FUNCTION("tabs.create", TABS_CREATE
)
129 class TabsDuplicateFunction
: public ChromeSyncExtensionFunction
{
130 ~TabsDuplicateFunction() override
{}
131 bool RunSync() override
;
132 DECLARE_EXTENSION_FUNCTION("tabs.duplicate", TABS_DUPLICATE
)
134 class TabsHighlightFunction
: public ChromeSyncExtensionFunction
{
135 ~TabsHighlightFunction() override
{}
136 bool RunSync() override
;
137 bool HighlightTab(TabStripModel
* tabstrip
,
138 ui::ListSelectionModel
* selection
,
141 DECLARE_EXTENSION_FUNCTION("tabs.highlight", TABS_HIGHLIGHT
)
143 class TabsUpdateFunction
: public ChromeAsyncExtensionFunction
{
145 TabsUpdateFunction();
148 ~TabsUpdateFunction() override
{}
149 virtual bool UpdateURL(const std::string
& url
,
152 virtual void PopulateResult();
154 content::WebContents
* web_contents_
;
157 bool RunAsync() override
;
158 void OnExecuteCodeFinished(const std::string
& error
,
160 const base::ListValue
& script_result
);
162 DECLARE_EXTENSION_FUNCTION("tabs.update", TABS_UPDATE
)
164 class TabsMoveFunction
: public ChromeSyncExtensionFunction
{
165 ~TabsMoveFunction() override
{}
166 bool RunSync() override
;
167 bool MoveTab(int tab_id
,
170 base::ListValue
* tab_values
,
172 DECLARE_EXTENSION_FUNCTION("tabs.move", TABS_MOVE
)
174 class TabsReloadFunction
: public ChromeSyncExtensionFunction
{
175 ~TabsReloadFunction() override
{}
176 bool RunSync() override
;
177 DECLARE_EXTENSION_FUNCTION("tabs.reload", TABS_RELOAD
)
179 class TabsRemoveFunction
: public ChromeSyncExtensionFunction
{
180 ~TabsRemoveFunction() override
{}
181 bool RunSync() override
;
182 bool RemoveTab(int tab_id
);
183 DECLARE_EXTENSION_FUNCTION("tabs.remove", TABS_REMOVE
)
185 class TabsDetectLanguageFunction
: public ChromeAsyncExtensionFunction
,
186 public content::NotificationObserver
{
188 ~TabsDetectLanguageFunction() override
{}
189 bool RunAsync() override
;
191 void Observe(int type
,
192 const content::NotificationSource
& source
,
193 const content::NotificationDetails
& details
) override
;
194 void GotLanguage(const std::string
& language
);
195 content::NotificationRegistrar registrar_
;
196 DECLARE_EXTENSION_FUNCTION("tabs.detectLanguage", TABS_DETECTLANGUAGE
)
198 class TabsCaptureVisibleTabFunction
199 : public extensions::CaptureWebContentsFunction
{
201 TabsCaptureVisibleTabFunction();
202 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable
* registry
);
205 ~TabsCaptureVisibleTabFunction() override
{}
208 ChromeExtensionFunctionDetails chrome_details_
;
210 // extensions::CaptureWebContentsFunction:
211 bool IsScreenshotEnabled() override
;
212 content::WebContents
* GetWebContentsForID(int id
) override
;
213 void OnCaptureFailure(FailureReason reason
) override
;
215 DECLARE_EXTENSION_FUNCTION("tabs.captureVisibleTab", TABS_CAPTUREVISIBLETAB
)
218 // Implement API call tabs.executeScript and tabs.insertCSS.
219 class ExecuteCodeInTabFunction
: public ExecuteCodeFunction
{
221 ExecuteCodeInTabFunction();
224 ~ExecuteCodeInTabFunction() override
;
226 // ExtensionFunction:
227 bool HasPermission() override
;
229 // Initialize the |execute_tab_id_| and |details_| if they haven't already
230 // been. Returns whether initialization was successful.
231 bool Init() override
;
232 bool CanExecuteScriptOnPage() override
;
233 ScriptExecutor
* GetScriptExecutor() override
;
234 bool IsWebView() const override
;
235 const GURL
& GetWebViewSrc() const override
;
238 const ChromeExtensionFunctionDetails chrome_details_
;
240 // Id of tab which executes code.
244 class TabsExecuteScriptFunction
: public ExecuteCodeInTabFunction
{
246 bool ShouldInsertCSS() const override
;
249 ~TabsExecuteScriptFunction() override
{}
251 void OnExecuteCodeFinished(const std::string
& error
,
253 const base::ListValue
& script_result
) override
;
255 DECLARE_EXTENSION_FUNCTION("tabs.executeScript", TABS_EXECUTESCRIPT
)
258 class TabsInsertCSSFunction
: public ExecuteCodeInTabFunction
{
260 ~TabsInsertCSSFunction() override
{}
262 bool ShouldInsertCSS() const override
;
264 DECLARE_EXTENSION_FUNCTION("tabs.insertCSS", TABS_INSERTCSS
)
267 class ZoomAPIFunction
: public ChromeAsyncExtensionFunction
{
269 ~ZoomAPIFunction() override
{}
271 // Gets the WebContents for |tab_id| if it is specified. Otherwise get the
272 // WebContents for the active tab in the current window. Calling this function
275 // TODO(...) many other tabs API functions use similar behavior. There should
276 // be a way to share this implementation somehow.
277 content::WebContents
* GetWebContents(int tab_id
);
280 class TabsSetZoomFunction
: public ZoomAPIFunction
{
282 ~TabsSetZoomFunction() override
{}
284 bool RunAsync() override
;
286 DECLARE_EXTENSION_FUNCTION("tabs.setZoom", TABS_SETZOOM
)
289 class TabsGetZoomFunction
: public ZoomAPIFunction
{
291 ~TabsGetZoomFunction() override
{}
293 bool RunAsync() override
;
295 DECLARE_EXTENSION_FUNCTION("tabs.getZoom", TABS_GETZOOM
)
298 class TabsSetZoomSettingsFunction
: public ZoomAPIFunction
{
300 ~TabsSetZoomSettingsFunction() override
{}
302 bool RunAsync() override
;
304 DECLARE_EXTENSION_FUNCTION("tabs.setZoomSettings", TABS_SETZOOMSETTINGS
)
307 class TabsGetZoomSettingsFunction
: public ZoomAPIFunction
{
309 ~TabsGetZoomSettingsFunction() override
{}
311 bool RunAsync() override
;
313 DECLARE_EXTENSION_FUNCTION("tabs.getZoomSettings", TABS_GETZOOMSETTINGS
)
316 } // namespace extensions
318 #endif // CHROME_BROWSER_EXTENSIONS_API_TABS_TABS_API_H_