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/browser/ui/zoom/zoom_controller.h"
15 #include "chrome/common/extensions/api/tabs.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(ZoomController::ZoomMode zoom_mode,
48 api::tabs::ZoomSettings* zoom_settings);
51 class WindowsGetFunction : public ChromeSyncExtensionFunction {
52 virtual ~WindowsGetFunction() {}
53 virtual bool RunSync() OVERRIDE;
54 DECLARE_EXTENSION_FUNCTION("windows.get", WINDOWS_GET)
56 class WindowsGetCurrentFunction : public ChromeSyncExtensionFunction {
57 virtual ~WindowsGetCurrentFunction() {}
58 virtual bool RunSync() OVERRIDE;
59 DECLARE_EXTENSION_FUNCTION("windows.getCurrent", WINDOWS_GETCURRENT)
61 class WindowsGetLastFocusedFunction : public ChromeSyncExtensionFunction {
62 virtual ~WindowsGetLastFocusedFunction() {}
63 virtual bool RunSync() OVERRIDE;
64 DECLARE_EXTENSION_FUNCTION("windows.getLastFocused", WINDOWS_GETLASTFOCUSED)
66 class WindowsGetAllFunction : public ChromeSyncExtensionFunction {
67 virtual ~WindowsGetAllFunction() {}
68 virtual bool RunSync() OVERRIDE;
69 DECLARE_EXTENSION_FUNCTION("windows.getAll", WINDOWS_GETALL)
71 class WindowsCreateFunction : public ChromeSyncExtensionFunction {
72 virtual ~WindowsCreateFunction() {}
73 virtual 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 virtual ~WindowsUpdateFunction() {}
89 virtual bool RunSync() OVERRIDE;
90 DECLARE_EXTENSION_FUNCTION("windows.update", WINDOWS_UPDATE)
92 class WindowsRemoveFunction : public ChromeSyncExtensionFunction {
93 virtual ~WindowsRemoveFunction() {}
94 virtual bool RunSync() OVERRIDE;
95 DECLARE_EXTENSION_FUNCTION("windows.remove", WINDOWS_REMOVE)
99 class TabsGetFunction : public ChromeSyncExtensionFunction {
100 virtual ~TabsGetFunction() {}
101 virtual bool RunSync() OVERRIDE;
102 DECLARE_EXTENSION_FUNCTION("tabs.get", TABS_GET)
104 class TabsGetCurrentFunction : public ChromeSyncExtensionFunction {
105 virtual ~TabsGetCurrentFunction() {}
106 virtual bool RunSync() OVERRIDE;
107 DECLARE_EXTENSION_FUNCTION("tabs.getCurrent", TABS_GETCURRENT)
109 class TabsGetSelectedFunction : public ChromeSyncExtensionFunction {
110 virtual ~TabsGetSelectedFunction() {}
111 virtual bool RunSync() OVERRIDE;
112 DECLARE_EXTENSION_FUNCTION("tabs.getSelected", TABS_GETSELECTED)
114 class TabsGetAllInWindowFunction : public ChromeSyncExtensionFunction {
115 virtual ~TabsGetAllInWindowFunction() {}
116 virtual bool RunSync() OVERRIDE;
117 DECLARE_EXTENSION_FUNCTION("tabs.getAllInWindow", TABS_GETALLINWINDOW)
119 class TabsQueryFunction : public ChromeSyncExtensionFunction {
120 virtual ~TabsQueryFunction() {}
121 virtual bool RunSync() OVERRIDE;
122 DECLARE_EXTENSION_FUNCTION("tabs.query", TABS_QUERY)
124 class TabsCreateFunction : public ChromeSyncExtensionFunction {
125 virtual ~TabsCreateFunction() {}
126 virtual bool RunSync() OVERRIDE;
127 DECLARE_EXTENSION_FUNCTION("tabs.create", TABS_CREATE)
129 class TabsDuplicateFunction : public ChromeSyncExtensionFunction {
130 virtual ~TabsDuplicateFunction() {}
131 virtual bool RunSync() OVERRIDE;
132 DECLARE_EXTENSION_FUNCTION("tabs.duplicate", TABS_DUPLICATE)
134 class TabsHighlightFunction : public ChromeSyncExtensionFunction {
135 virtual ~TabsHighlightFunction() {}
136 virtual 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 virtual ~TabsUpdateFunction() {}
149 virtual bool UpdateURL(const std::string& url,
152 virtual void PopulateResult();
154 content::WebContents* web_contents_;
157 virtual 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 virtual ~TabsMoveFunction() {}
166 virtual 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 virtual ~TabsReloadFunction() {}
176 virtual bool RunSync() OVERRIDE;
177 DECLARE_EXTENSION_FUNCTION("tabs.reload", TABS_RELOAD)
179 class TabsRemoveFunction : public ChromeSyncExtensionFunction {
180 virtual ~TabsRemoveFunction() {}
181 virtual 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 virtual ~TabsDetectLanguageFunction() {}
189 virtual bool RunAsync() OVERRIDE;
191 virtual 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 virtual ~TabsCaptureVisibleTabFunction() {}
208 ChromeExtensionFunctionDetails chrome_details_;
210 // extensions::CaptureWebContentsFunction:
211 virtual bool IsScreenshotEnabled() OVERRIDE;
212 virtual content::WebContents* GetWebContentsForID(int id) OVERRIDE;
213 virtual 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 virtual ~ExecuteCodeInTabFunction();
226 // ExtensionFunction:
227 virtual bool HasPermission() OVERRIDE;
229 // Initialize the |execute_tab_id_| and |details_| if they haven't already
230 // been. Returns whether initialization was successful.
231 virtual bool Init() OVERRIDE;
232 virtual bool CanExecuteScriptOnPage() OVERRIDE;
233 virtual ScriptExecutor* GetScriptExecutor() OVERRIDE;
234 virtual bool IsWebView() const OVERRIDE;
235 virtual const GURL& GetWebViewSrc() const OVERRIDE;
238 const ChromeExtensionFunctionDetails chrome_details_;
240 // Id of tab which executes code.
244 class TabsExecuteScriptFunction : public ExecuteCodeInTabFunction {
246 virtual bool ShouldInsertCSS() const OVERRIDE;
249 virtual ~TabsExecuteScriptFunction() {}
251 virtual void OnExecuteCodeFinished(
252 const std::string& error,
254 const base::ListValue& script_result) OVERRIDE;
256 DECLARE_EXTENSION_FUNCTION("tabs.executeScript", TABS_EXECUTESCRIPT)
259 class TabsInsertCSSFunction : public ExecuteCodeInTabFunction {
261 virtual ~TabsInsertCSSFunction() {}
263 virtual bool ShouldInsertCSS() const OVERRIDE;
265 DECLARE_EXTENSION_FUNCTION("tabs.insertCSS", TABS_INSERTCSS)
268 class ZoomAPIFunction : public ChromeAsyncExtensionFunction {
270 virtual ~ZoomAPIFunction() {}
272 // Gets the WebContents for |tab_id| if it is specified. Otherwise get the
273 // WebContents for the active tab in the current window. Calling this function
276 // TODO(...) many other tabs API functions use similar behavior. There should
277 // be a way to share this implementation somehow.
278 content::WebContents* GetWebContents(int tab_id);
281 class TabsSetZoomFunction : public ZoomAPIFunction {
283 virtual ~TabsSetZoomFunction() {}
285 virtual bool RunAsync() OVERRIDE;
287 DECLARE_EXTENSION_FUNCTION("tabs.setZoom", TABS_SETZOOM)
290 class TabsGetZoomFunction : public ZoomAPIFunction {
292 virtual ~TabsGetZoomFunction() {}
294 virtual bool RunAsync() OVERRIDE;
296 DECLARE_EXTENSION_FUNCTION("tabs.getZoom", TABS_GETZOOM)
299 class TabsSetZoomSettingsFunction : public ZoomAPIFunction {
301 virtual ~TabsSetZoomSettingsFunction() {}
303 virtual bool RunAsync() OVERRIDE;
305 DECLARE_EXTENSION_FUNCTION("tabs.setZoomSettings", TABS_SETZOOMSETTINGS)
308 class TabsGetZoomSettingsFunction : public ZoomAPIFunction {
310 virtual ~TabsGetZoomSettingsFunction() {}
312 virtual bool RunAsync() OVERRIDE;
314 DECLARE_EXTENSION_FUNCTION("tabs.getZoomSettings", TABS_GETZOOMSETTINGS)
317 } // namespace extensions
319 #endif // CHROME_BROWSER_EXTENSIONS_API_TABS_TABS_API_H_