Home | History | Annotate | Download | only in extensions
      1 // Copyright (c) 2011 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_EXTENSION_TABS_MODULE_H__
      6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_TABS_MODULE_H__
      7 #pragma once
      8 
      9 #include <string>
     10 
     11 #include "chrome/browser/extensions/extension_function.h"
     12 #include "content/browser/tab_contents/tab_contents_observer.h"
     13 #include "content/common/notification_observer.h"
     14 #include "content/common/notification_registrar.h"
     15 
     16 class BackingStore;
     17 class Browser;
     18 class DictionaryValue;
     19 class ListValue;
     20 class SkBitmap;
     21 class TabContents;
     22 class TabContentsWrapper;
     23 class TabStripModel;
     24 
     25 namespace ExtensionTabUtil {
     26 int GetWindowId(const Browser* browser);
     27 int GetTabId(const TabContents* tab_contents);
     28 std::string GetTabStatusText(bool is_loading);
     29 int GetWindowIdOfTab(const TabContents* tab_contents);
     30 ListValue* CreateTabList(const Browser* browser);
     31 DictionaryValue* CreateTabValue(const TabContents* tab_contents);
     32 DictionaryValue* CreateTabValue(const TabContents* tab_contents,
     33                                 TabStripModel* tab_strip,
     34                                 int tab_index);
     35 DictionaryValue* CreateWindowValue(const Browser* browser,
     36                                    bool populate_tabs);
     37 // Gets the |tab_strip_model| and |tab_index| for the given |tab_contents|.
     38 bool GetTabStripModel(const TabContents* tab_contents,
     39                       TabStripModel** tab_strip_model,
     40                       int* tab_index);
     41 bool GetDefaultTab(Browser* browser, TabContentsWrapper** contents,
     42                    int* tab_id);
     43 // Any out parameter (|browser|, |tab_strip|, |contents|, & |tab_index|) may
     44 // be NULL and will not be set within the function.
     45 bool GetTabById(int tab_id, Profile* profile, bool incognito_enabled,
     46                 Browser** browser,
     47                 TabStripModel** tab_strip,
     48                 TabContentsWrapper** contents,
     49                 int* tab_index);
     50 }
     51 
     52 // Windows
     53 class GetWindowFunction : public SyncExtensionFunction {
     54   ~GetWindowFunction() {}
     55   virtual bool RunImpl();
     56   DECLARE_EXTENSION_FUNCTION_NAME("windows.get")
     57 };
     58 class GetCurrentWindowFunction : public SyncExtensionFunction {
     59   ~GetCurrentWindowFunction() {}
     60   virtual bool RunImpl();
     61   DECLARE_EXTENSION_FUNCTION_NAME("windows.getCurrent")
     62 };
     63 class GetLastFocusedWindowFunction : public SyncExtensionFunction {
     64   ~GetLastFocusedWindowFunction() {}
     65   virtual bool RunImpl();
     66   DECLARE_EXTENSION_FUNCTION_NAME("windows.getLastFocused")
     67 };
     68 class GetAllWindowsFunction : public SyncExtensionFunction {
     69   ~GetAllWindowsFunction() {}
     70   virtual bool RunImpl();
     71   DECLARE_EXTENSION_FUNCTION_NAME("windows.getAll")
     72 };
     73 class CreateWindowFunction : public SyncExtensionFunction {
     74   ~CreateWindowFunction() {}
     75   virtual bool RunImpl();
     76   DECLARE_EXTENSION_FUNCTION_NAME("windows.create")
     77 };
     78 class UpdateWindowFunction : public SyncExtensionFunction {
     79   ~UpdateWindowFunction() {}
     80   virtual bool RunImpl();
     81   DECLARE_EXTENSION_FUNCTION_NAME("windows.update")
     82 };
     83 class RemoveWindowFunction : public SyncExtensionFunction {
     84   ~RemoveWindowFunction() {}
     85   virtual bool RunImpl();
     86   DECLARE_EXTENSION_FUNCTION_NAME("windows.remove")
     87 };
     88 
     89 // Tabs
     90 class GetTabFunction : public SyncExtensionFunction {
     91   ~GetTabFunction() {}
     92   virtual bool RunImpl();
     93   DECLARE_EXTENSION_FUNCTION_NAME("tabs.get")
     94 };
     95 class GetCurrentTabFunction : public SyncExtensionFunction {
     96   ~GetCurrentTabFunction() {}
     97   virtual bool RunImpl();
     98   DECLARE_EXTENSION_FUNCTION_NAME("tabs.getCurrent")
     99 };
    100 class GetSelectedTabFunction : public SyncExtensionFunction {
    101   ~GetSelectedTabFunction() {}
    102   virtual bool RunImpl();
    103   DECLARE_EXTENSION_FUNCTION_NAME("tabs.getSelected")
    104 };
    105 class GetAllTabsInWindowFunction : public SyncExtensionFunction {
    106   ~GetAllTabsInWindowFunction() {}
    107   virtual bool RunImpl();
    108   DECLARE_EXTENSION_FUNCTION_NAME("tabs.getAllInWindow")
    109 };
    110 class CreateTabFunction : public SyncExtensionFunction {
    111   ~CreateTabFunction() {}
    112   virtual bool RunImpl();
    113   DECLARE_EXTENSION_FUNCTION_NAME("tabs.create")
    114 };
    115 class UpdateTabFunction : public AsyncExtensionFunction,
    116                           public TabContentsObserver {
    117  public:
    118   UpdateTabFunction();
    119  private:
    120   ~UpdateTabFunction() {}
    121   virtual bool RunImpl();
    122   virtual bool OnMessageReceived(const IPC::Message& message);
    123   void OnExecuteCodeFinished(int request_id, bool success,
    124                              const std::string& error);
    125   TabContentsObserver::Registrar registrar_;
    126   DECLARE_EXTENSION_FUNCTION_NAME("tabs.update")
    127 };
    128 class MoveTabFunction : public SyncExtensionFunction {
    129   ~MoveTabFunction() {}
    130   virtual bool RunImpl();
    131   DECLARE_EXTENSION_FUNCTION_NAME("tabs.move")
    132 };
    133 class RemoveTabFunction : public SyncExtensionFunction {
    134   ~RemoveTabFunction() {}
    135   virtual bool RunImpl();
    136   DECLARE_EXTENSION_FUNCTION_NAME("tabs.remove")
    137 };
    138 class DetectTabLanguageFunction : public AsyncExtensionFunction,
    139                                   public NotificationObserver {
    140  private:
    141   ~DetectTabLanguageFunction() {}
    142   virtual bool RunImpl();
    143 
    144   virtual void Observe(NotificationType type,
    145                        const NotificationSource& source,
    146                        const NotificationDetails& details);
    147   void GotLanguage(const std::string& language);
    148   NotificationRegistrar registrar_;
    149   DECLARE_EXTENSION_FUNCTION_NAME("tabs.detectLanguage")
    150 };
    151 class CaptureVisibleTabFunction : public AsyncExtensionFunction,
    152                                   public NotificationObserver {
    153  private:
    154   enum ImageFormat {
    155     FORMAT_JPEG,
    156     FORMAT_PNG
    157   };
    158 
    159   // The default quality setting used when encoding jpegs.
    160   static const int kDefaultQuality;
    161 
    162   ~CaptureVisibleTabFunction() {}
    163   virtual bool RunImpl();
    164   virtual bool CaptureSnapshotFromBackingStore(BackingStore* backing_store);
    165   virtual void Observe(NotificationType type,
    166                        const NotificationSource& source,
    167                        const NotificationDetails& details);
    168   virtual void SendResultFromBitmap(const SkBitmap& screen_capture);
    169 
    170   NotificationRegistrar registrar_;
    171 
    172   // The format (JPEG vs PNG) of the resulting image.  Set in RunImpl().
    173   ImageFormat image_format_;
    174 
    175   // Quality setting to use when encoding jpegs.  Set in RunImpl().
    176   int image_quality_;
    177 
    178   DECLARE_EXTENSION_FUNCTION_NAME("tabs.captureVisibleTab")
    179 };
    180 
    181 #endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_TABS_MODULE_H__
    182