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/execute_code_function.h"
     13 #include "chrome/browser/extensions/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 SyncExtensionFunction {
     46   virtual ~WindowsGetFunction() {}
     47   virtual bool RunImpl() OVERRIDE;
     48   DECLARE_EXTENSION_FUNCTION("windows.get", WINDOWS_GET)
     49 };
     50 class WindowsGetCurrentFunction : public SyncExtensionFunction {
     51   virtual ~WindowsGetCurrentFunction() {}
     52   virtual bool RunImpl() OVERRIDE;
     53   DECLARE_EXTENSION_FUNCTION("windows.getCurrent", WINDOWS_GETCURRENT)
     54 };
     55 class WindowsGetLastFocusedFunction : public SyncExtensionFunction {
     56   virtual ~WindowsGetLastFocusedFunction() {}
     57   virtual bool RunImpl() OVERRIDE;
     58   DECLARE_EXTENSION_FUNCTION("windows.getLastFocused", WINDOWS_GETLASTFOCUSED)
     59 };
     60 class WindowsGetAllFunction : public SyncExtensionFunction {
     61   virtual ~WindowsGetAllFunction() {}
     62   virtual bool RunImpl() OVERRIDE;
     63   DECLARE_EXTENSION_FUNCTION("windows.getAll", 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("windows.create", WINDOWS_CREATE)
     78 };
     79 class WindowsUpdateFunction : public SyncExtensionFunction {
     80   virtual ~WindowsUpdateFunction() {}
     81   virtual bool RunImpl() OVERRIDE;
     82   DECLARE_EXTENSION_FUNCTION("windows.update", WINDOWS_UPDATE)
     83 };
     84 class WindowsRemoveFunction : public SyncExtensionFunction {
     85   virtual ~WindowsRemoveFunction() {}
     86   virtual bool RunImpl() OVERRIDE;
     87   DECLARE_EXTENSION_FUNCTION("windows.remove", WINDOWS_REMOVE)
     88 };
     89 
     90 // Tabs
     91 class TabsGetFunction : public SyncExtensionFunction {
     92   virtual ~TabsGetFunction() {}
     93   virtual bool RunImpl() OVERRIDE;
     94   DECLARE_EXTENSION_FUNCTION("tabs.get", TABS_GET)
     95 };
     96 class TabsGetCurrentFunction : public SyncExtensionFunction {
     97   virtual ~TabsGetCurrentFunction() {}
     98   virtual bool RunImpl() OVERRIDE;
     99   DECLARE_EXTENSION_FUNCTION("tabs.getCurrent", TABS_GETCURRENT)
    100 };
    101 class TabsGetSelectedFunction : public SyncExtensionFunction {
    102   virtual ~TabsGetSelectedFunction() {}
    103   virtual bool RunImpl() OVERRIDE;
    104   DECLARE_EXTENSION_FUNCTION("tabs.getSelected", TABS_GETSELECTED)
    105 };
    106 class TabsGetAllInWindowFunction : public SyncExtensionFunction {
    107   virtual ~TabsGetAllInWindowFunction() {}
    108   virtual bool RunImpl() OVERRIDE;
    109   DECLARE_EXTENSION_FUNCTION("tabs.getAllInWindow", TABS_GETALLINWINDOW)
    110 };
    111 class TabsQueryFunction : public SyncExtensionFunction {
    112   virtual ~TabsQueryFunction() {}
    113   virtual bool RunImpl() OVERRIDE;
    114   DECLARE_EXTENSION_FUNCTION("tabs.query", TABS_QUERY)
    115 };
    116 class TabsCreateFunction : public SyncExtensionFunction {
    117   virtual ~TabsCreateFunction() {}
    118   virtual bool RunImpl() OVERRIDE;
    119   DECLARE_EXTENSION_FUNCTION("tabs.create", TABS_CREATE)
    120 };
    121 class TabsDuplicateFunction : public SyncExtensionFunction {
    122   virtual ~TabsDuplicateFunction() {}
    123   virtual bool RunImpl() OVERRIDE;
    124   DECLARE_EXTENSION_FUNCTION("tabs.duplicate", TABS_DUPLICATE)
    125 };
    126 class TabsHighlightFunction : public SyncExtensionFunction {
    127   virtual ~TabsHighlightFunction() {}
    128   virtual bool RunImpl() OVERRIDE;
    129   bool HighlightTab(TabStripModel* tabstrip,
    130                     ui::ListSelectionModel* selection,
    131                     int *active_index,
    132                     int index);
    133   DECLARE_EXTENSION_FUNCTION("tabs.highlight", TABS_HIGHLIGHT)
    134 };
    135 class TabsUpdateFunction : public AsyncExtensionFunction {
    136  public:
    137   TabsUpdateFunction();
    138 
    139  protected:
    140   virtual ~TabsUpdateFunction() {}
    141   virtual bool UpdateURL(const std::string& url,
    142                          int tab_id,
    143                          bool* is_async);
    144   virtual void PopulateResult();
    145 
    146   content::WebContents* web_contents_;
    147 
    148  private:
    149   virtual bool RunImpl() OVERRIDE;
    150   void OnExecuteCodeFinished(const std::string& error,
    151                              int32 on_page_id,
    152                              const GURL& on_url,
    153                              const base::ListValue& script_result);
    154 
    155   DECLARE_EXTENSION_FUNCTION("tabs.update", TABS_UPDATE)
    156 };
    157 class TabsMoveFunction : public SyncExtensionFunction {
    158   virtual ~TabsMoveFunction() {}
    159   virtual bool RunImpl() OVERRIDE;
    160   bool MoveTab(int tab_id,
    161                int* new_index,
    162                int iteration,
    163                base::ListValue* tab_values,
    164                int* window_id);
    165   DECLARE_EXTENSION_FUNCTION("tabs.move", TABS_MOVE)
    166 };
    167 class TabsReloadFunction : public SyncExtensionFunction {
    168   virtual ~TabsReloadFunction() {}
    169   virtual bool RunImpl() OVERRIDE;
    170   DECLARE_EXTENSION_FUNCTION("tabs.reload", TABS_RELOAD)
    171 };
    172 class TabsRemoveFunction : public SyncExtensionFunction {
    173   virtual ~TabsRemoveFunction() {}
    174   virtual bool RunImpl() OVERRIDE;
    175   bool RemoveTab(int tab_id);
    176   DECLARE_EXTENSION_FUNCTION("tabs.remove", TABS_REMOVE)
    177 };
    178 class TabsDetectLanguageFunction : public AsyncExtensionFunction,
    179                                   public content::NotificationObserver {
    180  private:
    181   virtual ~TabsDetectLanguageFunction() {}
    182   virtual bool RunImpl() OVERRIDE;
    183 
    184   virtual void Observe(int type,
    185                        const content::NotificationSource& source,
    186                        const content::NotificationDetails& details) OVERRIDE;
    187   void GotLanguage(const std::string& language);
    188   content::NotificationRegistrar registrar_;
    189   DECLARE_EXTENSION_FUNCTION("tabs.detectLanguage", TABS_DETECTLANGUAGE)
    190 };
    191 class TabsCaptureVisibleTabFunction : public AsyncExtensionFunction {
    192  public:
    193   static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
    194 
    195  protected:
    196   typedef api::tabs::CaptureVisibleTab::Params::Options::Format ImageFormat;
    197 
    198   // The default quality setting used when encoding jpegs.
    199   static const int kDefaultQuality;
    200 
    201   virtual ~TabsCaptureVisibleTabFunction() {}
    202   virtual bool RunImpl() OVERRIDE;
    203   virtual bool GetTabToCapture(content::WebContents** web_contents);
    204   void SendResultFromBitmap(const SkBitmap& screen_capture);
    205 
    206  private:
    207   // Callback for the RWH::CopyFromBackingStore call.
    208   void CopyFromBackingStoreComplete(bool succeeded,
    209                                     const SkBitmap& bitmap);
    210 
    211   // Callback for the RWH::GetSnapshotFromRenderer call. This path is used if
    212   // CopyFromBackingStore fails for some reason.
    213   void GetSnapshotFromRendererComplete(bool succeeded,
    214                                        const SkBitmap& bitmap);
    215   void SendInternalError();
    216 
    217   // The format (JPEG vs PNG) of the resulting image.  Set in RunImpl().
    218   ImageFormat image_format_;
    219 
    220   // Quality setting to use when encoding jpegs.  Set in RunImpl().
    221   int image_quality_;
    222 
    223   DECLARE_EXTENSION_FUNCTION("tabs.captureVisibleTab", TABS_CAPTUREVISIBLETAB)
    224 };
    225 
    226 // Implement API call tabs.executeScript and tabs.insertCSS.
    227 class ExecuteCodeInTabFunction : public ExecuteCodeFunction {
    228  public:
    229   ExecuteCodeInTabFunction();
    230 
    231  protected:
    232   virtual ~ExecuteCodeInTabFunction();
    233 
    234   // ExtensionFunction:
    235   virtual bool HasPermission() OVERRIDE;
    236 
    237   // Initialize the |execute_tab_id_| and |details_| if they haven't already
    238   // been. Returns whether initialization was successful.
    239   virtual bool Init() OVERRIDE;
    240   virtual bool CanExecuteScriptOnPage() OVERRIDE;
    241   virtual ScriptExecutor* GetScriptExecutor() OVERRIDE;
    242   virtual bool IsWebView() const OVERRIDE;
    243 
    244  private:
    245   // Id of tab which executes code.
    246   int execute_tab_id_;
    247 };
    248 
    249 class TabsExecuteScriptFunction : public ExecuteCodeInTabFunction {
    250  protected:
    251   virtual bool ShouldInsertCSS() const OVERRIDE;
    252 
    253  private:
    254   virtual ~TabsExecuteScriptFunction() {}
    255 
    256   virtual void OnExecuteCodeFinished(
    257       const std::string& error,
    258       int32 on_page_id,
    259       const GURL& on_url,
    260       const base::ListValue& script_result) OVERRIDE;
    261 
    262   DECLARE_EXTENSION_FUNCTION("tabs.executeScript", TABS_EXECUTESCRIPT)
    263 };
    264 
    265 class TabsInsertCSSFunction : public ExecuteCodeInTabFunction {
    266  private:
    267   virtual ~TabsInsertCSSFunction() {}
    268 
    269   virtual bool ShouldInsertCSS() const OVERRIDE;
    270 
    271   DECLARE_EXTENSION_FUNCTION("tabs.insertCSS", TABS_INSERTCSS)
    272 };
    273 
    274 }  // namespace extensions
    275 
    276 #endif  // CHROME_BROWSER_EXTENSIONS_API_TABS_TABS_API_H_
    277