OLD | NEW |
| (Empty) |
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 CHROME_BROWSER_EXTENSIONS_API_TABS_TABS_API_H_ | |
6 #define CHROME_BROWSER_EXTENSIONS_API_TABS_TABS_API_H_ | |
7 | |
8 #include <string> | |
9 #include <vector> | |
10 | |
11 #include "base/compiler_specific.h" | |
12 #include "chrome/browser/extensions/extension_function.h" | |
13 #include "chrome/common/extensions/extension_resource.h" | |
14 #include "chrome/common/extensions/user_script.h" | |
15 #include "content/public/browser/notification_observer.h" | |
16 #include "content/public/browser/notification_registrar.h" | |
17 #include "googleurl/src/gurl.h" | |
18 | |
19 class BackingStore; | |
20 class GURL; | |
21 class PrefServiceSyncable; | |
22 class SkBitmap; | |
23 | |
24 namespace base { | |
25 class DictionaryValue; | |
26 } | |
27 | |
28 namespace content { | |
29 class WebContents; | |
30 } | |
31 | |
32 namespace extensions { | |
33 namespace api { | |
34 namespace tabs { | |
35 struct InjectDetails; | |
36 } // namespace tabs | |
37 } // namespace api | |
38 } // namespace extensions | |
39 | |
40 namespace skia { | |
41 class PlatformBitmap; | |
42 } | |
43 | |
44 // Windows | |
45 class WindowsGetFunction : public SyncExtensionFunction { | |
46 virtual ~WindowsGetFunction() {} | |
47 virtual bool RunImpl() OVERRIDE; | |
48 DECLARE_EXTENSION_FUNCTION_NAME("windows.get") | |
49 }; | |
50 class WindowsGetCurrentFunction : public SyncExtensionFunction { | |
51 virtual ~WindowsGetCurrentFunction() {} | |
52 virtual bool RunImpl() OVERRIDE; | |
53 DECLARE_EXTENSION_FUNCTION_NAME("windows.getCurrent") | |
54 }; | |
55 class WindowsGetLastFocusedFunction : public SyncExtensionFunction { | |
56 virtual ~WindowsGetLastFocusedFunction() {} | |
57 virtual bool RunImpl() OVERRIDE; | |
58 DECLARE_EXTENSION_FUNCTION_NAME("windows.getLastFocused") | |
59 }; | |
60 class WindowsGetAllFunction : public SyncExtensionFunction { | |
61 virtual ~WindowsGetAllFunction() {} | |
62 virtual bool RunImpl() OVERRIDE; | |
63 DECLARE_EXTENSION_FUNCTION_NAME("windows.getAll") | |
64 }; | |
65 class WindowsCreateFunction : public SyncExtensionFunction { | |
66 virtual ~WindowsCreateFunction() {} | |
67 virtual bool RunImpl() OVERRIDE; | |
68 // Returns whether the window should be created in incognito mode. | |
69 // |urls| is the list of urls to open. If we are creating an incognito window, | |
70 // the function will remove these urls which may not be opened in incognito | |
71 // mode. If window creation leads the browser into an erroneous state, | |
72 // |is_error| is set to true (also, error_ member variable is assigned | |
73 // the proper error message). | |
74 bool ShouldOpenIncognitoWindow(const base::DictionaryValue* args, | |
75 std::vector<GURL>* urls, | |
76 bool* is_error); | |
77 DECLARE_EXTENSION_FUNCTION_NAME("windows.create") | |
78 }; | |
79 class WindowsUpdateFunction : public SyncExtensionFunction { | |
80 virtual ~WindowsUpdateFunction() {} | |
81 virtual bool RunImpl() OVERRIDE; | |
82 DECLARE_EXTENSION_FUNCTION_NAME("windows.update") | |
83 }; | |
84 class WindowsRemoveFunction : public SyncExtensionFunction { | |
85 virtual ~WindowsRemoveFunction() {} | |
86 virtual bool RunImpl() OVERRIDE; | |
87 DECLARE_EXTENSION_FUNCTION_NAME("windows.remove") | |
88 }; | |
89 | |
90 // Tabs | |
91 class TabsGetFunction : public SyncExtensionFunction { | |
92 virtual ~TabsGetFunction() {} | |
93 virtual bool RunImpl() OVERRIDE; | |
94 DECLARE_EXTENSION_FUNCTION_NAME("tabs.get") | |
95 }; | |
96 class TabsGetCurrentFunction : public SyncExtensionFunction { | |
97 virtual ~TabsGetCurrentFunction() {} | |
98 virtual bool RunImpl() OVERRIDE; | |
99 DECLARE_EXTENSION_FUNCTION_NAME("tabs.getCurrent") | |
100 }; | |
101 class TabsGetSelectedFunction : public SyncExtensionFunction { | |
102 virtual ~TabsGetSelectedFunction() {} | |
103 virtual bool RunImpl() OVERRIDE; | |
104 DECLARE_EXTENSION_FUNCTION_NAME("tabs.getSelected") | |
105 }; | |
106 class TabsGetAllInWindowFunction : public SyncExtensionFunction { | |
107 virtual ~TabsGetAllInWindowFunction() {} | |
108 virtual bool RunImpl() OVERRIDE; | |
109 DECLARE_EXTENSION_FUNCTION_NAME("tabs.getAllInWindow") | |
110 }; | |
111 class TabsQueryFunction : public SyncExtensionFunction { | |
112 virtual ~TabsQueryFunction() {} | |
113 virtual bool RunImpl() OVERRIDE; | |
114 DECLARE_EXTENSION_FUNCTION_NAME("tabs.query") | |
115 }; | |
116 class TabsCreateFunction : public SyncExtensionFunction { | |
117 virtual ~TabsCreateFunction() {} | |
118 virtual bool RunImpl() OVERRIDE; | |
119 DECLARE_EXTENSION_FUNCTION_NAME("tabs.create") | |
120 }; | |
121 class TabsDuplicateFunction : public SyncExtensionFunction { | |
122 virtual ~TabsDuplicateFunction() {} | |
123 virtual bool RunImpl() OVERRIDE; | |
124 DECLARE_EXTENSION_FUNCTION_NAME("tabs.duplicate") | |
125 }; | |
126 class TabsHighlightFunction : public SyncExtensionFunction { | |
127 virtual ~TabsHighlightFunction() {} | |
128 virtual bool RunImpl() OVERRIDE; | |
129 DECLARE_EXTENSION_FUNCTION_NAME("tabs.highlight") | |
130 }; | |
131 class TabsUpdateFunction : public AsyncExtensionFunction { | |
132 public: | |
133 TabsUpdateFunction(); | |
134 | |
135 protected: | |
136 virtual ~TabsUpdateFunction() {} | |
137 virtual bool UpdateURLIfPresent(base::DictionaryValue* update_props, | |
138 int tab_id, | |
139 bool* is_async); | |
140 virtual void PopulateResult(); | |
141 | |
142 content::WebContents* web_contents_; | |
143 | |
144 private: | |
145 virtual bool RunImpl() OVERRIDE; | |
146 void OnExecuteCodeFinished(const std::string& error, | |
147 int32 on_page_id, | |
148 const GURL& on_url, | |
149 const ListValue& script_result); | |
150 | |
151 DECLARE_EXTENSION_FUNCTION_NAME("tabs.update") | |
152 }; | |
153 class TabsMoveFunction : public SyncExtensionFunction { | |
154 virtual ~TabsMoveFunction() {} | |
155 virtual bool RunImpl() OVERRIDE; | |
156 DECLARE_EXTENSION_FUNCTION_NAME("tabs.move") | |
157 }; | |
158 class TabsReloadFunction : public SyncExtensionFunction { | |
159 virtual ~TabsReloadFunction() {} | |
160 virtual bool RunImpl() OVERRIDE; | |
161 DECLARE_EXTENSION_FUNCTION_NAME("tabs.reload") | |
162 }; | |
163 class TabsRemoveFunction : public SyncExtensionFunction { | |
164 virtual ~TabsRemoveFunction() {} | |
165 virtual bool RunImpl() OVERRIDE; | |
166 DECLARE_EXTENSION_FUNCTION_NAME("tabs.remove") | |
167 }; | |
168 class TabsDetectLanguageFunction : public AsyncExtensionFunction, | |
169 public content::NotificationObserver { | |
170 private: | |
171 virtual ~TabsDetectLanguageFunction() {} | |
172 virtual bool RunImpl() OVERRIDE; | |
173 | |
174 virtual void Observe(int type, | |
175 const content::NotificationSource& source, | |
176 const content::NotificationDetails& details) OVERRIDE; | |
177 void GotLanguage(const std::string& language); | |
178 content::NotificationRegistrar registrar_; | |
179 DECLARE_EXTENSION_FUNCTION_NAME("tabs.detectLanguage") | |
180 }; | |
181 class TabsCaptureVisibleTabFunction : public AsyncExtensionFunction, | |
182 public content::NotificationObserver { | |
183 public: | |
184 static void RegisterUserPrefs(PrefServiceSyncable* service); | |
185 | |
186 protected: | |
187 enum ImageFormat { | |
188 FORMAT_JPEG, | |
189 FORMAT_PNG | |
190 }; | |
191 | |
192 // The default quality setting used when encoding jpegs. | |
193 static const int kDefaultQuality; | |
194 | |
195 virtual ~TabsCaptureVisibleTabFunction() {} | |
196 virtual bool RunImpl() OVERRIDE; | |
197 virtual bool GetTabToCapture(content::WebContents** web_contents); | |
198 virtual void Observe(int type, | |
199 const content::NotificationSource& source, | |
200 const content::NotificationDetails& details) OVERRIDE; | |
201 void SendResultFromBitmap(const SkBitmap& screen_capture); | |
202 | |
203 private: | |
204 void CopyFromBackingStoreComplete(skia::PlatformBitmap* bitmap, | |
205 bool succeeded); | |
206 | |
207 content::NotificationRegistrar registrar_; | |
208 | |
209 // The format (JPEG vs PNG) of the resulting image. Set in RunImpl(). | |
210 ImageFormat image_format_; | |
211 | |
212 // Quality setting to use when encoding jpegs. Set in RunImpl(). | |
213 int image_quality_; | |
214 | |
215 DECLARE_EXTENSION_FUNCTION_NAME("tabs.captureVisibleTab") | |
216 }; | |
217 | |
218 // Implement API call tabs.executeScript and tabs.insertCSS. | |
219 class ExecuteCodeInTabFunction : public AsyncExtensionFunction { | |
220 public: | |
221 ExecuteCodeInTabFunction(); | |
222 | |
223 protected: | |
224 virtual ~ExecuteCodeInTabFunction(); | |
225 | |
226 // ExtensionFunction: | |
227 virtual bool HasPermission() OVERRIDE; | |
228 virtual bool RunImpl() OVERRIDE; | |
229 | |
230 // Message handler. | |
231 virtual void OnExecuteCodeFinished(const std::string& error, | |
232 int32 on_page_id, | |
233 const GURL& on_url, | |
234 const ListValue& script_result); | |
235 | |
236 private: | |
237 // Initialize the |execute_tab_id_| and |details_| if they haven't already | |
238 // been. Returns whether initialization was successful. | |
239 bool Init(); | |
240 | |
241 // Called when contents from the file whose path is specified in JSON | |
242 // arguments has been loaded. | |
243 void DidLoadFile(bool success, const std::string& data); | |
244 | |
245 // Runs on FILE thread. Loads message bundles for the extension and | |
246 // localizes the CSS data. Calls back DidLoadAndLocalizeFile on the UI thread. | |
247 void LocalizeCSS( | |
248 const std::string& data, | |
249 const std::string& extension_id, | |
250 const FilePath& extension_path, | |
251 const std::string& extension_default_locale); | |
252 | |
253 // Called when contents from the loaded file have been localized. | |
254 void DidLoadAndLocalizeFile(bool success, const std::string& data); | |
255 | |
256 // Run in UI thread. Code string contains the code to be executed. Returns | |
257 // true on success. If true is returned, this does an AddRef. | |
258 bool Execute(const std::string& code_string); | |
259 | |
260 // Id of tab which executes code. | |
261 int execute_tab_id_; | |
262 | |
263 // The injection details. | |
264 scoped_ptr<extensions::api::tabs::InjectDetails> details_; | |
265 | |
266 // Contains extension resource built from path of file which is | |
267 // specified in JSON arguments. | |
268 ExtensionResource resource_; | |
269 }; | |
270 | |
271 class TabsExecuteScriptFunction : public ExecuteCodeInTabFunction { | |
272 private: | |
273 virtual ~TabsExecuteScriptFunction() {} | |
274 | |
275 virtual void OnExecuteCodeFinished(const std::string& error, | |
276 int32 on_page_id, | |
277 const GURL& on_url, | |
278 const ListValue& script_result) OVERRIDE; | |
279 | |
280 DECLARE_EXTENSION_FUNCTION_NAME("tabs.executeScript") | |
281 }; | |
282 | |
283 class TabsInsertCSSFunction : public ExecuteCodeInTabFunction { | |
284 private: | |
285 virtual ~TabsInsertCSSFunction() {} | |
286 | |
287 DECLARE_EXTENSION_FUNCTION_NAME("tabs.insertCSS") | |
288 }; | |
289 | |
290 #endif // CHROME_BROWSER_EXTENSIONS_API_TABS_TABS_API_H_ | |
OLD | NEW |