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/execute_code_function.h"
13 #include "chrome/browser/extensions/chrome_extension_function.h"
14 #include "chrome/common/extensions/api/tabs.h"
15 #include "content/public/browser/notification_observer.h"
16 #include "content/public/browser/notification_registrar.h"
17 #include "extensions/common/extension_resource.h"
18 #include "extensions/common/user_script.h"
27 class DictionaryValue;
35 class ListSelectionModel;
38 namespace user_prefs {
39 class PrefRegistrySyncable;
42 namespace extensions {
45 class WindowsGetFunction : public ChromeSyncExtensionFunction {
46 virtual ~WindowsGetFunction() {}
47 virtual bool RunImpl() OVERRIDE;
48 DECLARE_EXTENSION_FUNCTION("windows.get", WINDOWS_GET)
50 class WindowsGetCurrentFunction : public ChromeSyncExtensionFunction {
51 virtual ~WindowsGetCurrentFunction() {}
52 virtual bool RunImpl() OVERRIDE;
53 DECLARE_EXTENSION_FUNCTION("windows.getCurrent", WINDOWS_GETCURRENT)
55 class WindowsGetLastFocusedFunction : public ChromeSyncExtensionFunction {
56 virtual ~WindowsGetLastFocusedFunction() {}
57 virtual bool RunImpl() OVERRIDE;
58 DECLARE_EXTENSION_FUNCTION("windows.getLastFocused", WINDOWS_GETLASTFOCUSED)
60 class WindowsGetAllFunction : public ChromeSyncExtensionFunction {
61 virtual ~WindowsGetAllFunction() {}
62 virtual bool RunImpl() OVERRIDE;
63 DECLARE_EXTENSION_FUNCTION("windows.getAll", WINDOWS_GETALL)
65 class WindowsCreateFunction : public ChromeSyncExtensionFunction {
66 virtual ~WindowsCreateFunction() {}
67 virtual bool RunImpl() OVERRIDE;
68 // Returns whether the window should be created in incognito mode.
69 // |create_data| are the options passed by the extension. It may be NULL.
70 // |urls| is the list of urls to open. If we are creating an incognito window,
71 // the function will remove these urls which may not be opened in incognito
72 // mode. If window creation leads the browser into an erroneous state,
73 // |is_error| is set to true (also, error_ member variable is assigned
74 // the proper error message).
75 bool ShouldOpenIncognitoWindow(
76 const api::windows::Create::Params::CreateData* create_data,
77 std::vector<GURL>* urls,
79 DECLARE_EXTENSION_FUNCTION("windows.create", WINDOWS_CREATE)
81 class WindowsUpdateFunction : public ChromeSyncExtensionFunction {
82 virtual ~WindowsUpdateFunction() {}
83 virtual bool RunImpl() OVERRIDE;
84 DECLARE_EXTENSION_FUNCTION("windows.update", WINDOWS_UPDATE)
86 class WindowsRemoveFunction : public ChromeSyncExtensionFunction {
87 virtual ~WindowsRemoveFunction() {}
88 virtual bool RunImpl() OVERRIDE;
89 DECLARE_EXTENSION_FUNCTION("windows.remove", WINDOWS_REMOVE)
93 class TabsGetFunction : public ChromeSyncExtensionFunction {
94 virtual ~TabsGetFunction() {}
95 virtual bool RunImpl() OVERRIDE;
96 DECLARE_EXTENSION_FUNCTION("tabs.get", TABS_GET)
98 class TabsGetCurrentFunction : public ChromeSyncExtensionFunction {
99 virtual ~TabsGetCurrentFunction() {}
100 virtual bool RunImpl() OVERRIDE;
101 DECLARE_EXTENSION_FUNCTION("tabs.getCurrent", TABS_GETCURRENT)
103 class TabsGetSelectedFunction : public ChromeSyncExtensionFunction {
104 virtual ~TabsGetSelectedFunction() {}
105 virtual bool RunImpl() OVERRIDE;
106 DECLARE_EXTENSION_FUNCTION("tabs.getSelected", TABS_GETSELECTED)
108 class TabsGetAllInWindowFunction : public ChromeSyncExtensionFunction {
109 virtual ~TabsGetAllInWindowFunction() {}
110 virtual bool RunImpl() OVERRIDE;
111 DECLARE_EXTENSION_FUNCTION("tabs.getAllInWindow", TABS_GETALLINWINDOW)
113 class TabsQueryFunction : public ChromeSyncExtensionFunction {
114 virtual ~TabsQueryFunction() {}
115 virtual bool RunImpl() OVERRIDE;
116 DECLARE_EXTENSION_FUNCTION("tabs.query", TABS_QUERY)
118 class TabsCreateFunction : public ChromeSyncExtensionFunction {
119 virtual ~TabsCreateFunction() {}
120 virtual bool RunImpl() OVERRIDE;
121 DECLARE_EXTENSION_FUNCTION("tabs.create", TABS_CREATE)
123 class TabsDuplicateFunction : public ChromeSyncExtensionFunction {
124 virtual ~TabsDuplicateFunction() {}
125 virtual bool RunImpl() OVERRIDE;
126 DECLARE_EXTENSION_FUNCTION("tabs.duplicate", TABS_DUPLICATE)
128 class TabsHighlightFunction : public ChromeSyncExtensionFunction {
129 virtual ~TabsHighlightFunction() {}
130 virtual bool RunImpl() OVERRIDE;
131 bool HighlightTab(TabStripModel* tabstrip,
132 ui::ListSelectionModel* selection,
135 DECLARE_EXTENSION_FUNCTION("tabs.highlight", TABS_HIGHLIGHT)
137 class TabsUpdateFunction : public ChromeAsyncExtensionFunction {
139 TabsUpdateFunction();
142 virtual ~TabsUpdateFunction() {}
143 virtual bool UpdateURL(const std::string& url,
146 virtual void PopulateResult();
148 content::WebContents* web_contents_;
151 virtual bool RunImpl() OVERRIDE;
152 void OnExecuteCodeFinished(const std::string& error,
155 const base::ListValue& script_result);
157 DECLARE_EXTENSION_FUNCTION("tabs.update", TABS_UPDATE)
159 class TabsMoveFunction : public ChromeSyncExtensionFunction {
160 virtual ~TabsMoveFunction() {}
161 virtual bool RunImpl() OVERRIDE;
162 bool MoveTab(int tab_id,
165 base::ListValue* tab_values,
167 DECLARE_EXTENSION_FUNCTION("tabs.move", TABS_MOVE)
169 class TabsReloadFunction : public ChromeSyncExtensionFunction {
170 virtual ~TabsReloadFunction() {}
171 virtual bool RunImpl() OVERRIDE;
172 DECLARE_EXTENSION_FUNCTION("tabs.reload", TABS_RELOAD)
174 class TabsRemoveFunction : public ChromeSyncExtensionFunction {
175 virtual ~TabsRemoveFunction() {}
176 virtual bool RunImpl() OVERRIDE;
177 bool RemoveTab(int tab_id);
178 DECLARE_EXTENSION_FUNCTION("tabs.remove", TABS_REMOVE)
180 class TabsDetectLanguageFunction : public ChromeAsyncExtensionFunction,
181 public content::NotificationObserver {
183 virtual ~TabsDetectLanguageFunction() {}
184 virtual bool RunImpl() OVERRIDE;
186 virtual void Observe(int type,
187 const content::NotificationSource& source,
188 const content::NotificationDetails& details) OVERRIDE;
189 void GotLanguage(const std::string& language);
190 content::NotificationRegistrar registrar_;
191 DECLARE_EXTENSION_FUNCTION("tabs.detectLanguage", TABS_DETECTLANGUAGE)
193 class TabsCaptureVisibleTabFunction : public ChromeAsyncExtensionFunction {
195 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
198 typedef api::tabs::CaptureVisibleTab::Params::Options::Format ImageFormat;
200 // The default quality setting used when encoding jpegs.
201 static const int kDefaultQuality;
203 virtual ~TabsCaptureVisibleTabFunction() {}
204 virtual bool RunImpl() OVERRIDE;
205 virtual bool GetTabToCapture(content::WebContents** web_contents);
206 void SendResultFromBitmap(const SkBitmap& screen_capture);
209 // Callback for the RWH::CopyFromBackingStore call.
210 void CopyFromBackingStoreComplete(bool succeeded,
211 const SkBitmap& bitmap);
213 // Callback for the RWH::GetSnapshotFromRenderer call. This path is used if
214 // CopyFromBackingStore fails for some reason.
215 void GetSnapshotFromRendererComplete(bool succeeded,
216 const SkBitmap& bitmap);
217 void SendInternalError();
219 // The format (JPEG vs PNG) of the resulting image. Set in RunImpl().
220 ImageFormat image_format_;
222 // Quality setting to use when encoding jpegs. Set in RunImpl().
225 DECLARE_EXTENSION_FUNCTION("tabs.captureVisibleTab", TABS_CAPTUREVISIBLETAB)
228 // Implement API call tabs.executeScript and tabs.insertCSS.
229 class ExecuteCodeInTabFunction : public ExecuteCodeFunction {
231 ExecuteCodeInTabFunction();
234 virtual ~ExecuteCodeInTabFunction();
236 // ExtensionFunction:
237 virtual bool HasPermission() OVERRIDE;
239 // Initialize the |execute_tab_id_| and |details_| if they haven't already
240 // been. Returns whether initialization was successful.
241 virtual bool Init() OVERRIDE;
242 virtual bool CanExecuteScriptOnPage() OVERRIDE;
243 virtual ScriptExecutor* GetScriptExecutor() OVERRIDE;
244 virtual bool IsWebView() const OVERRIDE;
247 // Id of tab which executes code.
251 class TabsExecuteScriptFunction : public ExecuteCodeInTabFunction {
253 virtual bool ShouldInsertCSS() const OVERRIDE;
256 virtual ~TabsExecuteScriptFunction() {}
258 virtual void OnExecuteCodeFinished(
259 const std::string& error,
262 const base::ListValue& script_result) OVERRIDE;
264 DECLARE_EXTENSION_FUNCTION("tabs.executeScript", TABS_EXECUTESCRIPT)
267 class TabsInsertCSSFunction : public ExecuteCodeInTabFunction {
269 virtual ~TabsInsertCSSFunction() {}
271 virtual bool ShouldInsertCSS() const OVERRIDE;
273 DECLARE_EXTENSION_FUNCTION("tabs.insertCSS", TABS_INSERTCSS)
276 } // namespace extensions
278 #endif // CHROME_BROWSER_EXTENSIONS_API_TABS_TABS_API_H_