Home | History | Annotate | Download | only in tabs
      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/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/common/extensions/api/tabs.h"
     16 #include "content/public/browser/notification_observer.h"
     17 #include "content/public/browser/notification_registrar.h"
     18 #include "extensions/common/extension_resource.h"
     19 #include "extensions/common/user_script.h"
     20 #include "url/gurl.h"
     21 
     22 class BackingStore;
     23 class GURL;
     24 class SkBitmap;
     25 class TabStripModel;
     26 
     27 namespace base {
     28 class DictionaryValue;
     29 }
     30 
     31 namespace content {
     32 class WebContents;
     33 }
     34 
     35 namespace ui {
     36 class ListSelectionModel;
     37 }
     38 
     39 namespace user_prefs {
     40 class PrefRegistrySyncable;
     41 }
     42 
     43 namespace extensions {
     44 
     45 // Windows
     46 class WindowsGetFunction : public ChromeSyncExtensionFunction {
     47   virtual ~WindowsGetFunction() {}
     48   virtual bool RunImpl() OVERRIDE;
     49   DECLARE_EXTENSION_FUNCTION("windows.get", WINDOWS_GET)
     50 };
     51 class WindowsGetCurrentFunction : public ChromeSyncExtensionFunction {
     52   virtual ~WindowsGetCurrentFunction() {}
     53   virtual bool RunImpl() OVERRIDE;
     54   DECLARE_EXTENSION_FUNCTION("windows.getCurrent", WINDOWS_GETCURRENT)
     55 };
     56 class WindowsGetLastFocusedFunction : public ChromeSyncExtensionFunction {
     57   virtual ~WindowsGetLastFocusedFunction() {}
     58   virtual bool RunImpl() OVERRIDE;
     59   DECLARE_EXTENSION_FUNCTION("windows.getLastFocused", WINDOWS_GETLASTFOCUSED)
     60 };
     61 class WindowsGetAllFunction : public ChromeSyncExtensionFunction {
     62   virtual ~WindowsGetAllFunction() {}
     63   virtual bool RunImpl() OVERRIDE;
     64   DECLARE_EXTENSION_FUNCTION("windows.getAll", WINDOWS_GETALL)
     65 };
     66 class WindowsCreateFunction : public ChromeSyncExtensionFunction {
     67   virtual ~WindowsCreateFunction() {}
     68   virtual bool RunImpl() OVERRIDE;
     69   // Returns whether the window should be created in incognito mode.
     70   // |create_data| are the options passed by the extension. It may be NULL.
     71   // |urls| is the list of urls to open. If we are creating an incognito window,
     72   // the function will remove these urls which may not be opened in incognito
     73   // mode.  If window creation leads the browser into an erroneous state,
     74   // |is_error| is set to true (also, error_ member variable is assigned
     75   // the proper error message).
     76   bool ShouldOpenIncognitoWindow(
     77       const api::windows::Create::Params::CreateData* create_data,
     78       std::vector<GURL>* urls,
     79       bool* is_error);
     80   DECLARE_EXTENSION_FUNCTION("windows.create", WINDOWS_CREATE)
     81 };
     82 class WindowsUpdateFunction : public ChromeSyncExtensionFunction {
     83   virtual ~WindowsUpdateFunction() {}
     84   virtual bool RunImpl() OVERRIDE;
     85   DECLARE_EXTENSION_FUNCTION("windows.update", WINDOWS_UPDATE)
     86 };
     87 class WindowsRemoveFunction : public ChromeSyncExtensionFunction {
     88   virtual ~WindowsRemoveFunction() {}
     89   virtual bool RunImpl() OVERRIDE;
     90   DECLARE_EXTENSION_FUNCTION("windows.remove", WINDOWS_REMOVE)
     91 };
     92 
     93 // Tabs
     94 class TabsGetFunction : public ChromeSyncExtensionFunction {
     95   virtual ~TabsGetFunction() {}
     96   virtual bool RunImpl() OVERRIDE;
     97   DECLARE_EXTENSION_FUNCTION("tabs.get", TABS_GET)
     98 };
     99 class TabsGetCurrentFunction : public ChromeSyncExtensionFunction {
    100   virtual ~TabsGetCurrentFunction() {}
    101   virtual bool RunImpl() OVERRIDE;
    102   DECLARE_EXTENSION_FUNCTION("tabs.getCurrent", TABS_GETCURRENT)
    103 };
    104 class TabsGetSelectedFunction : public ChromeSyncExtensionFunction {
    105   virtual ~TabsGetSelectedFunction() {}
    106   virtual bool RunImpl() OVERRIDE;
    107   DECLARE_EXTENSION_FUNCTION("tabs.getSelected", TABS_GETSELECTED)
    108 };
    109 class TabsGetAllInWindowFunction : public ChromeSyncExtensionFunction {
    110   virtual ~TabsGetAllInWindowFunction() {}
    111   virtual bool RunImpl() OVERRIDE;
    112   DECLARE_EXTENSION_FUNCTION("tabs.getAllInWindow", TABS_GETALLINWINDOW)
    113 };
    114 class TabsQueryFunction : public ChromeSyncExtensionFunction {
    115   virtual ~TabsQueryFunction() {}
    116   virtual bool RunImpl() OVERRIDE;
    117   DECLARE_EXTENSION_FUNCTION("tabs.query", TABS_QUERY)
    118 };
    119 class TabsCreateFunction : public ChromeSyncExtensionFunction {
    120   virtual ~TabsCreateFunction() {}
    121   virtual bool RunImpl() OVERRIDE;
    122   DECLARE_EXTENSION_FUNCTION("tabs.create", TABS_CREATE)
    123 };
    124 class TabsDuplicateFunction : public ChromeSyncExtensionFunction {
    125   virtual ~TabsDuplicateFunction() {}
    126   virtual bool RunImpl() OVERRIDE;
    127   DECLARE_EXTENSION_FUNCTION("tabs.duplicate", TABS_DUPLICATE)
    128 };
    129 class TabsHighlightFunction : public ChromeSyncExtensionFunction {
    130   virtual ~TabsHighlightFunction() {}
    131   virtual bool RunImpl() OVERRIDE;
    132   bool HighlightTab(TabStripModel* tabstrip,
    133                     ui::ListSelectionModel* selection,
    134                     int *active_index,
    135                     int index);
    136   DECLARE_EXTENSION_FUNCTION("tabs.highlight", TABS_HIGHLIGHT)
    137 };
    138 class TabsUpdateFunction : public ChromeAsyncExtensionFunction {
    139  public:
    140   TabsUpdateFunction();
    141 
    142  protected:
    143   virtual ~TabsUpdateFunction() {}
    144   virtual bool UpdateURL(const std::string& url,
    145                          int tab_id,
    146                          bool* is_async);
    147   virtual void PopulateResult();
    148 
    149   content::WebContents* web_contents_;
    150 
    151  private:
    152   virtual bool RunImpl() OVERRIDE;
    153   void OnExecuteCodeFinished(const std::string& error,
    154                              int32 on_page_id,
    155                              const GURL& on_url,
    156                              const base::ListValue& script_result);
    157 
    158   DECLARE_EXTENSION_FUNCTION("tabs.update", TABS_UPDATE)
    159 };
    160 class TabsMoveFunction : public ChromeSyncExtensionFunction {
    161   virtual ~TabsMoveFunction() {}
    162   virtual bool RunImpl() OVERRIDE;
    163   bool MoveTab(int tab_id,
    164                int* new_index,
    165                int iteration,
    166                base::ListValue* tab_values,
    167                int* window_id);
    168   DECLARE_EXTENSION_FUNCTION("tabs.move", TABS_MOVE)
    169 };
    170 class TabsReloadFunction : public ChromeSyncExtensionFunction {
    171   virtual ~TabsReloadFunction() {}
    172   virtual bool RunImpl() OVERRIDE;
    173   DECLARE_EXTENSION_FUNCTION("tabs.reload", TABS_RELOAD)
    174 };
    175 class TabsRemoveFunction : public ChromeSyncExtensionFunction {
    176   virtual ~TabsRemoveFunction() {}
    177   virtual bool RunImpl() OVERRIDE;
    178   bool RemoveTab(int tab_id);
    179   DECLARE_EXTENSION_FUNCTION("tabs.remove", TABS_REMOVE)
    180 };
    181 class TabsDetectLanguageFunction : public ChromeAsyncExtensionFunction,
    182                                    public content::NotificationObserver {
    183  private:
    184   virtual ~TabsDetectLanguageFunction() {}
    185   virtual bool RunImpl() OVERRIDE;
    186 
    187   virtual void Observe(int type,
    188                        const content::NotificationSource& source,
    189                        const content::NotificationDetails& details) OVERRIDE;
    190   void GotLanguage(const std::string& language);
    191   content::NotificationRegistrar registrar_;
    192   DECLARE_EXTENSION_FUNCTION("tabs.detectLanguage", TABS_DETECTLANGUAGE)
    193 };
    194 class TabsCaptureVisibleTabFunction
    195     : public extensions::CaptureWebContentsFunction {
    196  public:
    197   static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
    198 
    199  protected:
    200   virtual ~TabsCaptureVisibleTabFunction() {}
    201 
    202  private:
    203   // extensions::CaptureWebContentsFunction:
    204   virtual bool IsScreenshotEnabled() OVERRIDE;
    205   virtual content::WebContents* GetWebContentsForID(int id) OVERRIDE;
    206   virtual void OnCaptureFailure(FailureReason reason) OVERRIDE;
    207 
    208   DECLARE_EXTENSION_FUNCTION("tabs.captureVisibleTab", TABS_CAPTUREVISIBLETAB)
    209 };
    210 
    211 // Implement API call tabs.executeScript and tabs.insertCSS.
    212 class ExecuteCodeInTabFunction : public ExecuteCodeFunction {
    213  public:
    214   ExecuteCodeInTabFunction();
    215 
    216  protected:
    217   virtual ~ExecuteCodeInTabFunction();
    218 
    219   // ExtensionFunction:
    220   virtual bool HasPermission() OVERRIDE;
    221 
    222   // Initialize the |execute_tab_id_| and |details_| if they haven't already
    223   // been. Returns whether initialization was successful.
    224   virtual bool Init() OVERRIDE;
    225   virtual bool CanExecuteScriptOnPage() OVERRIDE;
    226   virtual ScriptExecutor* GetScriptExecutor() OVERRIDE;
    227   virtual bool IsWebView() const OVERRIDE;
    228 
    229  private:
    230   // Id of tab which executes code.
    231   int execute_tab_id_;
    232 };
    233 
    234 class TabsExecuteScriptFunction : public ExecuteCodeInTabFunction {
    235  protected:
    236   virtual bool ShouldInsertCSS() const OVERRIDE;
    237 
    238  private:
    239   virtual ~TabsExecuteScriptFunction() {}
    240 
    241   virtual void OnExecuteCodeFinished(
    242       const std::string& error,
    243       int32 on_page_id,
    244       const GURL& on_url,
    245       const base::ListValue& script_result) OVERRIDE;
    246 
    247   DECLARE_EXTENSION_FUNCTION("tabs.executeScript", TABS_EXECUTESCRIPT)
    248 };
    249 
    250 class TabsInsertCSSFunction : public ExecuteCodeInTabFunction {
    251  private:
    252   virtual ~TabsInsertCSSFunction() {}
    253 
    254   virtual bool ShouldInsertCSS() const OVERRIDE;
    255 
    256   DECLARE_EXTENSION_FUNCTION("tabs.insertCSS", TABS_INSERTCSS)
    257 };
    258 
    259 }  // namespace extensions
    260 
    261 #endif  // CHROME_BROWSER_EXTENSIONS_API_TABS_TABS_API_H_
    262