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/api/capture_web_contents_function.h"
13 #include "chrome/browser/extensions/api/execute_code_function.h"
14 #include "chrome/browser/extensions/chrome_extension_function.h"
15 #include "chrome/browser/ui/zoom/zoom_controller.h"
16 #include "chrome/common/extensions/api/tabs.h"
17 #include "content/public/browser/notification_observer.h"
18 #include "content/public/browser/notification_registrar.h"
19 #include "extensions/common/extension_resource.h"
20 #include "extensions/common/user_script.h"
28 class DictionaryValue
;
36 class ListSelectionModel
;
39 namespace user_prefs
{
40 class PrefRegistrySyncable
;
43 namespace extensions
{
45 // Converts a ZoomMode to its ZoomSettings representation.
46 void ZoomModeToZoomSettings(ZoomController::ZoomMode zoom_mode
,
47 api::tabs::ZoomSettings
* zoom_settings
);
50 class WindowsGetFunction
: public ChromeSyncExtensionFunction
{
51 virtual ~WindowsGetFunction() {}
52 virtual bool RunSync() OVERRIDE
;
53 DECLARE_EXTENSION_FUNCTION("windows.get", WINDOWS_GET
)
55 class WindowsGetCurrentFunction
: public ChromeSyncExtensionFunction
{
56 virtual ~WindowsGetCurrentFunction() {}
57 virtual bool RunSync() OVERRIDE
;
58 DECLARE_EXTENSION_FUNCTION("windows.getCurrent", WINDOWS_GETCURRENT
)
60 class WindowsGetLastFocusedFunction
: public ChromeSyncExtensionFunction
{
61 virtual ~WindowsGetLastFocusedFunction() {}
62 virtual bool RunSync() OVERRIDE
;
63 DECLARE_EXTENSION_FUNCTION("windows.getLastFocused", WINDOWS_GETLASTFOCUSED
)
65 class WindowsGetAllFunction
: public ChromeSyncExtensionFunction
{
66 virtual ~WindowsGetAllFunction() {}
67 virtual bool RunSync() OVERRIDE
;
68 DECLARE_EXTENSION_FUNCTION("windows.getAll", WINDOWS_GETALL
)
70 class WindowsCreateFunction
: public ChromeSyncExtensionFunction
{
71 virtual ~WindowsCreateFunction() {}
72 virtual bool RunSync() OVERRIDE
;
73 // Returns whether the window should be created in incognito mode.
74 // |create_data| are the options passed by the extension. It may be NULL.
75 // |urls| is the list of urls to open. If we are creating an incognito window,
76 // the function will remove these urls which may not be opened in incognito
77 // mode. If window creation leads the browser into an erroneous state,
78 // |is_error| is set to true (also, error_ member variable is assigned
79 // the proper error message).
80 bool ShouldOpenIncognitoWindow(
81 const api::windows::Create::Params::CreateData
* create_data
,
82 std::vector
<GURL
>* urls
,
84 DECLARE_EXTENSION_FUNCTION("windows.create", WINDOWS_CREATE
)
86 class WindowsUpdateFunction
: public ChromeSyncExtensionFunction
{
87 virtual ~WindowsUpdateFunction() {}
88 virtual bool RunSync() OVERRIDE
;
89 DECLARE_EXTENSION_FUNCTION("windows.update", WINDOWS_UPDATE
)
91 class WindowsRemoveFunction
: public ChromeSyncExtensionFunction
{
92 virtual ~WindowsRemoveFunction() {}
93 virtual bool RunSync() OVERRIDE
;
94 DECLARE_EXTENSION_FUNCTION("windows.remove", WINDOWS_REMOVE
)
98 class TabsGetFunction
: public ChromeSyncExtensionFunction
{
99 virtual ~TabsGetFunction() {}
100 virtual bool RunSync() OVERRIDE
;
101 DECLARE_EXTENSION_FUNCTION("tabs.get", TABS_GET
)
103 class TabsGetCurrentFunction
: public ChromeSyncExtensionFunction
{
104 virtual ~TabsGetCurrentFunction() {}
105 virtual bool RunSync() OVERRIDE
;
106 DECLARE_EXTENSION_FUNCTION("tabs.getCurrent", TABS_GETCURRENT
)
108 class TabsGetSelectedFunction
: public ChromeSyncExtensionFunction
{
109 virtual ~TabsGetSelectedFunction() {}
110 virtual bool RunSync() OVERRIDE
;
111 DECLARE_EXTENSION_FUNCTION("tabs.getSelected", TABS_GETSELECTED
)
113 class TabsGetAllInWindowFunction
: public ChromeSyncExtensionFunction
{
114 virtual ~TabsGetAllInWindowFunction() {}
115 virtual bool RunSync() OVERRIDE
;
116 DECLARE_EXTENSION_FUNCTION("tabs.getAllInWindow", TABS_GETALLINWINDOW
)
118 class TabsQueryFunction
: public ChromeSyncExtensionFunction
{
119 virtual ~TabsQueryFunction() {}
120 virtual bool RunSync() OVERRIDE
;
121 DECLARE_EXTENSION_FUNCTION("tabs.query", TABS_QUERY
)
123 class TabsCreateFunction
: public ChromeSyncExtensionFunction
{
124 virtual ~TabsCreateFunction() {}
125 virtual bool RunSync() OVERRIDE
;
126 DECLARE_EXTENSION_FUNCTION("tabs.create", TABS_CREATE
)
128 class TabsDuplicateFunction
: public ChromeSyncExtensionFunction
{
129 virtual ~TabsDuplicateFunction() {}
130 virtual bool RunSync() OVERRIDE
;
131 DECLARE_EXTENSION_FUNCTION("tabs.duplicate", TABS_DUPLICATE
)
133 class TabsHighlightFunction
: public ChromeSyncExtensionFunction
{
134 virtual ~TabsHighlightFunction() {}
135 virtual bool RunSync() OVERRIDE
;
136 bool HighlightTab(TabStripModel
* tabstrip
,
137 ui::ListSelectionModel
* selection
,
140 DECLARE_EXTENSION_FUNCTION("tabs.highlight", TABS_HIGHLIGHT
)
142 class TabsUpdateFunction
: public ChromeAsyncExtensionFunction
{
144 TabsUpdateFunction();
147 virtual ~TabsUpdateFunction() {}
148 virtual bool UpdateURL(const std::string
& url
,
151 virtual void PopulateResult();
153 content::WebContents
* web_contents_
;
156 virtual bool RunAsync() OVERRIDE
;
157 void OnExecuteCodeFinished(const std::string
& error
,
159 const base::ListValue
& script_result
);
161 DECLARE_EXTENSION_FUNCTION("tabs.update", TABS_UPDATE
)
163 class TabsMoveFunction
: public ChromeSyncExtensionFunction
{
164 virtual ~TabsMoveFunction() {}
165 virtual bool RunSync() OVERRIDE
;
166 bool MoveTab(int tab_id
,
169 base::ListValue
* tab_values
,
171 DECLARE_EXTENSION_FUNCTION("tabs.move", TABS_MOVE
)
173 class TabsReloadFunction
: public ChromeSyncExtensionFunction
{
174 virtual ~TabsReloadFunction() {}
175 virtual bool RunSync() OVERRIDE
;
176 DECLARE_EXTENSION_FUNCTION("tabs.reload", TABS_RELOAD
)
178 class TabsRemoveFunction
: public ChromeSyncExtensionFunction
{
179 virtual ~TabsRemoveFunction() {}
180 virtual bool RunSync() OVERRIDE
;
181 bool RemoveTab(int tab_id
);
182 DECLARE_EXTENSION_FUNCTION("tabs.remove", TABS_REMOVE
)
184 class TabsDetectLanguageFunction
: public ChromeAsyncExtensionFunction
,
185 public content::NotificationObserver
{
187 virtual ~TabsDetectLanguageFunction() {}
188 virtual bool RunAsync() OVERRIDE
;
190 virtual void Observe(int type
,
191 const content::NotificationSource
& source
,
192 const content::NotificationDetails
& details
) OVERRIDE
;
193 void GotLanguage(const std::string
& language
);
194 content::NotificationRegistrar registrar_
;
195 DECLARE_EXTENSION_FUNCTION("tabs.detectLanguage", TABS_DETECTLANGUAGE
)
197 class TabsCaptureVisibleTabFunction
198 : public extensions::CaptureWebContentsFunction
{
200 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable
* registry
);
203 virtual ~TabsCaptureVisibleTabFunction() {}
206 // extensions::CaptureWebContentsFunction:
207 virtual bool IsScreenshotEnabled() OVERRIDE
;
208 virtual content::WebContents
* GetWebContentsForID(int id
) OVERRIDE
;
209 virtual void OnCaptureFailure(FailureReason reason
) OVERRIDE
;
211 DECLARE_EXTENSION_FUNCTION("tabs.captureVisibleTab", TABS_CAPTUREVISIBLETAB
)
214 // Implement API call tabs.executeScript and tabs.insertCSS.
215 class ExecuteCodeInTabFunction
: public ExecuteCodeFunction
{
217 ExecuteCodeInTabFunction();
220 virtual ~ExecuteCodeInTabFunction();
222 // ExtensionFunction:
223 virtual bool HasPermission() OVERRIDE
;
225 // Initialize the |execute_tab_id_| and |details_| if they haven't already
226 // been. Returns whether initialization was successful.
227 virtual bool Init() OVERRIDE
;
228 virtual bool CanExecuteScriptOnPage() OVERRIDE
;
229 virtual ScriptExecutor
* GetScriptExecutor() OVERRIDE
;
230 virtual bool IsWebView() const OVERRIDE
;
231 virtual const GURL
& GetWebViewSrc() const OVERRIDE
;
234 // Id of tab which executes code.
238 class TabsExecuteScriptFunction
: public ExecuteCodeInTabFunction
{
240 virtual bool ShouldInsertCSS() const OVERRIDE
;
243 virtual ~TabsExecuteScriptFunction() {}
245 virtual void OnExecuteCodeFinished(
246 const std::string
& error
,
248 const base::ListValue
& script_result
) OVERRIDE
;
250 DECLARE_EXTENSION_FUNCTION("tabs.executeScript", TABS_EXECUTESCRIPT
)
253 class TabsInsertCSSFunction
: public ExecuteCodeInTabFunction
{
255 virtual ~TabsInsertCSSFunction() {}
257 virtual bool ShouldInsertCSS() const OVERRIDE
;
259 DECLARE_EXTENSION_FUNCTION("tabs.insertCSS", TABS_INSERTCSS
)
262 class ZoomAPIFunction
: public ChromeAsyncExtensionFunction
{
264 virtual ~ZoomAPIFunction() {}
266 // Gets the WebContents for |tab_id| if it is specified. Otherwise get the
267 // WebContents for the active tab in the current window. Calling this function
270 // TODO(...) many other tabs API functions use similar behavior. There should
271 // be a way to share this implementation somehow.
272 content::WebContents
* GetWebContents(int tab_id
);
275 class TabsSetZoomFunction
: public ZoomAPIFunction
{
277 virtual ~TabsSetZoomFunction() {}
279 virtual bool RunAsync() OVERRIDE
;
281 DECLARE_EXTENSION_FUNCTION("tabs.setZoom", TABS_SETZOOM
)
284 class TabsGetZoomFunction
: public ZoomAPIFunction
{
286 virtual ~TabsGetZoomFunction() {}
288 virtual bool RunAsync() OVERRIDE
;
290 DECLARE_EXTENSION_FUNCTION("tabs.getZoom", TABS_GETZOOM
)
293 class TabsSetZoomSettingsFunction
: public ZoomAPIFunction
{
295 virtual ~TabsSetZoomSettingsFunction() {}
297 virtual bool RunAsync() OVERRIDE
;
299 DECLARE_EXTENSION_FUNCTION("tabs.setZoomSettings", TABS_SETZOOMSETTINGS
)
302 class TabsGetZoomSettingsFunction
: public ZoomAPIFunction
{
304 virtual ~TabsGetZoomSettingsFunction() {}
306 virtual bool RunAsync() OVERRIDE
;
308 DECLARE_EXTENSION_FUNCTION("tabs.getZoomSettings", TABS_GETZOOMSETTINGS
)
311 } // namespace extensions
313 #endif // CHROME_BROWSER_EXTENSIONS_API_TABS_TABS_API_H_