- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / tabs / tabs_api.h
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/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"
19 #include "url/gurl.h"
20
21 class BackingStore;
22 class GURL;
23 class SkBitmap;
24 class TabStripModel;
25
26 namespace base {
27 class DictionaryValue;
28 }
29
30 namespace content {
31 class WebContents;
32 }
33
34 namespace ui {
35 class ListSelectionModel;
36 }
37
38 namespace user_prefs {
39 class PrefRegistrySyncable;
40 }
41
42 namespace extensions {
43
44 // Windows
45 class WindowsGetFunction : public ChromeSyncExtensionFunction {
46   virtual ~WindowsGetFunction() {}
47   virtual bool RunImpl() OVERRIDE;
48   DECLARE_EXTENSION_FUNCTION("windows.get", WINDOWS_GET)
49 };
50 class WindowsGetCurrentFunction : public ChromeSyncExtensionFunction {
51   virtual ~WindowsGetCurrentFunction() {}
52   virtual bool RunImpl() OVERRIDE;
53   DECLARE_EXTENSION_FUNCTION("windows.getCurrent", WINDOWS_GETCURRENT)
54 };
55 class WindowsGetLastFocusedFunction : public ChromeSyncExtensionFunction {
56   virtual ~WindowsGetLastFocusedFunction() {}
57   virtual bool RunImpl() OVERRIDE;
58   DECLARE_EXTENSION_FUNCTION("windows.getLastFocused", WINDOWS_GETLASTFOCUSED)
59 };
60 class WindowsGetAllFunction : public ChromeSyncExtensionFunction {
61   virtual ~WindowsGetAllFunction() {}
62   virtual bool RunImpl() OVERRIDE;
63   DECLARE_EXTENSION_FUNCTION("windows.getAll", WINDOWS_GETALL)
64 };
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,
78       bool* is_error);
79   DECLARE_EXTENSION_FUNCTION("windows.create", WINDOWS_CREATE)
80 };
81 class WindowsUpdateFunction : public ChromeSyncExtensionFunction {
82   virtual ~WindowsUpdateFunction() {}
83   virtual bool RunImpl() OVERRIDE;
84   DECLARE_EXTENSION_FUNCTION("windows.update", WINDOWS_UPDATE)
85 };
86 class WindowsRemoveFunction : public ChromeSyncExtensionFunction {
87   virtual ~WindowsRemoveFunction() {}
88   virtual bool RunImpl() OVERRIDE;
89   DECLARE_EXTENSION_FUNCTION("windows.remove", WINDOWS_REMOVE)
90 };
91
92 // Tabs
93 class TabsGetFunction : public ChromeSyncExtensionFunction {
94   virtual ~TabsGetFunction() {}
95   virtual bool RunImpl() OVERRIDE;
96   DECLARE_EXTENSION_FUNCTION("tabs.get", TABS_GET)
97 };
98 class TabsGetCurrentFunction : public ChromeSyncExtensionFunction {
99   virtual ~TabsGetCurrentFunction() {}
100   virtual bool RunImpl() OVERRIDE;
101   DECLARE_EXTENSION_FUNCTION("tabs.getCurrent", TABS_GETCURRENT)
102 };
103 class TabsGetSelectedFunction : public ChromeSyncExtensionFunction {
104   virtual ~TabsGetSelectedFunction() {}
105   virtual bool RunImpl() OVERRIDE;
106   DECLARE_EXTENSION_FUNCTION("tabs.getSelected", TABS_GETSELECTED)
107 };
108 class TabsGetAllInWindowFunction : public ChromeSyncExtensionFunction {
109   virtual ~TabsGetAllInWindowFunction() {}
110   virtual bool RunImpl() OVERRIDE;
111   DECLARE_EXTENSION_FUNCTION("tabs.getAllInWindow", TABS_GETALLINWINDOW)
112 };
113 class TabsQueryFunction : public ChromeSyncExtensionFunction {
114   virtual ~TabsQueryFunction() {}
115   virtual bool RunImpl() OVERRIDE;
116   DECLARE_EXTENSION_FUNCTION("tabs.query", TABS_QUERY)
117 };
118 class TabsCreateFunction : public ChromeSyncExtensionFunction {
119   virtual ~TabsCreateFunction() {}
120   virtual bool RunImpl() OVERRIDE;
121   DECLARE_EXTENSION_FUNCTION("tabs.create", TABS_CREATE)
122 };
123 class TabsDuplicateFunction : public ChromeSyncExtensionFunction {
124   virtual ~TabsDuplicateFunction() {}
125   virtual bool RunImpl() OVERRIDE;
126   DECLARE_EXTENSION_FUNCTION("tabs.duplicate", TABS_DUPLICATE)
127 };
128 class TabsHighlightFunction : public ChromeSyncExtensionFunction {
129   virtual ~TabsHighlightFunction() {}
130   virtual bool RunImpl() OVERRIDE;
131   bool HighlightTab(TabStripModel* tabstrip,
132                     ui::ListSelectionModel* selection,
133                     int *active_index,
134                     int index);
135   DECLARE_EXTENSION_FUNCTION("tabs.highlight", TABS_HIGHLIGHT)
136 };
137 class TabsUpdateFunction : public ChromeAsyncExtensionFunction {
138  public:
139   TabsUpdateFunction();
140
141  protected:
142   virtual ~TabsUpdateFunction() {}
143   virtual bool UpdateURL(const std::string& url,
144                          int tab_id,
145                          bool* is_async);
146   virtual void PopulateResult();
147
148   content::WebContents* web_contents_;
149
150  private:
151   virtual bool RunImpl() OVERRIDE;
152   void OnExecuteCodeFinished(const std::string& error,
153                              int32 on_page_id,
154                              const GURL& on_url,
155                              const base::ListValue& script_result);
156
157   DECLARE_EXTENSION_FUNCTION("tabs.update", TABS_UPDATE)
158 };
159 class TabsMoveFunction : public ChromeSyncExtensionFunction {
160   virtual ~TabsMoveFunction() {}
161   virtual bool RunImpl() OVERRIDE;
162   bool MoveTab(int tab_id,
163                int* new_index,
164                int iteration,
165                base::ListValue* tab_values,
166                int* window_id);
167   DECLARE_EXTENSION_FUNCTION("tabs.move", TABS_MOVE)
168 };
169 class TabsReloadFunction : public ChromeSyncExtensionFunction {
170   virtual ~TabsReloadFunction() {}
171   virtual bool RunImpl() OVERRIDE;
172   DECLARE_EXTENSION_FUNCTION("tabs.reload", TABS_RELOAD)
173 };
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)
179 };
180 class TabsDetectLanguageFunction : public ChromeAsyncExtensionFunction,
181                                    public content::NotificationObserver {
182  private:
183   virtual ~TabsDetectLanguageFunction() {}
184   virtual bool RunImpl() OVERRIDE;
185
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)
192 };
193 class TabsCaptureVisibleTabFunction : public ChromeAsyncExtensionFunction {
194  public:
195   static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
196
197  protected:
198   typedef api::tabs::CaptureVisibleTab::Params::Options::Format ImageFormat;
199
200   // The default quality setting used when encoding jpegs.
201   static const int kDefaultQuality;
202
203   virtual ~TabsCaptureVisibleTabFunction() {}
204   virtual bool RunImpl() OVERRIDE;
205   virtual bool GetTabToCapture(content::WebContents** web_contents);
206   void SendResultFromBitmap(const SkBitmap& screen_capture);
207
208  private:
209   // Callback for the RWH::CopyFromBackingStore call.
210   void CopyFromBackingStoreComplete(bool succeeded,
211                                     const SkBitmap& bitmap);
212
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();
218
219   // The format (JPEG vs PNG) of the resulting image.  Set in RunImpl().
220   ImageFormat image_format_;
221
222   // Quality setting to use when encoding jpegs.  Set in RunImpl().
223   int image_quality_;
224
225   DECLARE_EXTENSION_FUNCTION("tabs.captureVisibleTab", TABS_CAPTUREVISIBLETAB)
226 };
227
228 // Implement API call tabs.executeScript and tabs.insertCSS.
229 class ExecuteCodeInTabFunction : public ExecuteCodeFunction {
230  public:
231   ExecuteCodeInTabFunction();
232
233  protected:
234   virtual ~ExecuteCodeInTabFunction();
235
236   // ExtensionFunction:
237   virtual bool HasPermission() OVERRIDE;
238
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;
245
246  private:
247   // Id of tab which executes code.
248   int execute_tab_id_;
249 };
250
251 class TabsExecuteScriptFunction : public ExecuteCodeInTabFunction {
252  protected:
253   virtual bool ShouldInsertCSS() const OVERRIDE;
254
255  private:
256   virtual ~TabsExecuteScriptFunction() {}
257
258   virtual void OnExecuteCodeFinished(
259       const std::string& error,
260       int32 on_page_id,
261       const GURL& on_url,
262       const base::ListValue& script_result) OVERRIDE;
263
264   DECLARE_EXTENSION_FUNCTION("tabs.executeScript", TABS_EXECUTESCRIPT)
265 };
266
267 class TabsInsertCSSFunction : public ExecuteCodeInTabFunction {
268  private:
269   virtual ~TabsInsertCSSFunction() {}
270
271   virtual bool ShouldInsertCSS() const OVERRIDE;
272
273   DECLARE_EXTENSION_FUNCTION("tabs.insertCSS", TABS_INSERTCSS)
274 };
275
276 }  // namespace extensions
277
278 #endif  // CHROME_BROWSER_EXTENSIONS_API_TABS_TABS_API_H_