Home | History | Annotate | Download | only in extensions
      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_EXTENSION_TAB_UTIL_H_
      6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_TAB_UTIL_H_
      7 
      8 #include <string>
      9 
     10 #include "base/callback.h"
     11 #include "ui/base/window_open_disposition.h"
     12 
     13 class Browser;
     14 class ChromeExtensionFunctionDetails;
     15 class ChromeUIThreadExtensionFunction;
     16 class ExtensionFunction;
     17 class GURL;
     18 class Profile;
     19 class TabStripModel;
     20 
     21 namespace base {
     22 class DictionaryValue;
     23 class ListValue;
     24 }
     25 
     26 namespace content {
     27 class BrowserContext;
     28 class WebContents;
     29 }
     30 
     31 namespace gfx {
     32 class Rect;
     33 }
     34 
     35 namespace extensions {
     36 
     37 class Extension;
     38 class WindowController;
     39 
     40 namespace api {
     41 namespace tabs {
     42 struct Tab;
     43 }
     44 }
     45 
     46 // Provides various utility functions that help manipulate tabs.
     47 class ExtensionTabUtil {
     48  public:
     49   struct OpenTabParams {
     50     OpenTabParams();
     51     ~OpenTabParams();
     52 
     53     bool create_browser_if_needed;
     54     scoped_ptr<int> window_id;
     55     scoped_ptr<int> opener_tab_id;
     56     scoped_ptr<std::string> url;
     57     scoped_ptr<bool> active;
     58     scoped_ptr<bool> pinned;
     59     scoped_ptr<int> index;
     60   };
     61 
     62   // Opens a new tab given an extension function |function| and creation
     63   // parameters |params|. Returns a Tab object if successful, or NULL and
     64   // optionally sets |error| if an error occurs.
     65   static base::DictionaryValue* OpenTab(
     66       ChromeUIThreadExtensionFunction* function,
     67       const OpenTabParams& params,
     68       std::string* error);
     69 
     70   static int GetWindowId(const Browser* browser);
     71   static int GetWindowIdOfTabStripModel(const TabStripModel* tab_strip_model);
     72   static int GetTabId(const content::WebContents* web_contents);
     73   static std::string GetTabStatusText(bool is_loading);
     74   static int GetWindowIdOfTab(const content::WebContents* web_contents);
     75   static base::ListValue* CreateTabList(const Browser* browser,
     76                                         const Extension* extension);
     77 
     78   // DEPRECATED: Please consider using ChromeExtensionFunctionDetails instead
     79   // of the deprecated ChromeUIThreadExtensionFunction and use the overload
     80   // below
     81   static Browser* GetBrowserFromWindowID(
     82       ChromeUIThreadExtensionFunction* function,
     83       int window_id,
     84       std::string* error_message);
     85 
     86   static Browser* GetBrowserFromWindowID(
     87       const ChromeExtensionFunctionDetails& details,
     88       int window_id,
     89       std::string* error_message);
     90 
     91   // Creates a Tab object (see chrome/common/extensions/api/tabs.json) with
     92   // information about the state of a browser tab.  Depending on the
     93   // permissions of the extension, the object may or may not include sensitive
     94   // data such as the tab's URL.
     95   static base::DictionaryValue* CreateTabValue(
     96       content::WebContents* web_contents,
     97       const Extension* extension) {
     98     return CreateTabValue(web_contents, NULL, -1, extension);
     99   }
    100   static base::DictionaryValue* CreateTabValue(
    101       content::WebContents* web_contents,
    102       TabStripModel* tab_strip,
    103       int tab_index,
    104       const Extension* extension);
    105 
    106   // Creates a Tab object but performs no extension permissions checks; the
    107   // returned object will contain privacy-sensitive data.
    108   static base::DictionaryValue* CreateTabValue(
    109       content::WebContents* web_contents) {
    110     return CreateTabValue(web_contents, NULL, -1);
    111   }
    112   static base::DictionaryValue* CreateTabValue(
    113       content::WebContents* web_contents,
    114       TabStripModel* tab_strip,
    115       int tab_index);
    116 
    117   // Removes any privacy-sensitive fields from a Tab object if appropriate,
    118   // given the permissions of the extension and the tab in question.  The
    119   // tab_info object is modified in place.
    120   static void ScrubTabValueForExtension(content::WebContents* contents,
    121                                         const Extension* extension,
    122                                         base::DictionaryValue* tab_info);
    123 
    124   // Removes any privacy-sensitive fields from a Tab object if appropriate,
    125   // given the permissions of the extension in question.
    126   static void ScrubTabForExtension(const Extension* extension,
    127                                    api::tabs::Tab* tab);
    128 
    129   // Gets the |tab_strip_model| and |tab_index| for the given |web_contents|.
    130   static bool GetTabStripModel(const content::WebContents* web_contents,
    131                                TabStripModel** tab_strip_model,
    132                                int* tab_index);
    133   static bool GetDefaultTab(Browser* browser,
    134                             content::WebContents** contents,
    135                             int* tab_id);
    136   // Any out parameter (|browser|, |tab_strip|, |contents|, & |tab_index|) may
    137   // be NULL and will not be set within the function.
    138   static bool GetTabById(int tab_id,
    139                          content::BrowserContext* browser_context,
    140                          bool incognito_enabled,
    141                          Browser** browser,
    142                          TabStripModel** tab_strip,
    143                          content::WebContents** contents,
    144                          int* tab_index);
    145 
    146   // Takes |url_string| and returns a GURL which is either valid and absolute
    147   // or invalid. If |url_string| is not directly interpretable as a valid (it is
    148   // likely a relative URL) an attempt is made to resolve it. |extension| is
    149   // provided so it can be resolved relative to its extension base
    150   // (chrome-extension://<id>/). Using the source frame url would be more
    151   // correct, but because the api shipped with urls resolved relative to their
    152   // extension base, we decided it wasn't worth breaking existing extensions to
    153   // fix.
    154   static GURL ResolvePossiblyRelativeURL(const std::string& url_string,
    155                                          const Extension* extension);
    156 
    157   // Returns true if |url| is used for testing crashes.
    158   static bool IsCrashURL(const GURL& url);
    159 
    160   // Opens a tab for the specified |web_contents|.
    161   static void CreateTab(content::WebContents* web_contents,
    162                         const std::string& extension_id,
    163                         WindowOpenDisposition disposition,
    164                         const gfx::Rect& initial_pos,
    165                         bool user_gesture);
    166 
    167   // Executes the specified callback for all tabs in all browser windows.
    168   static void ForEachTab(
    169       const base::Callback<void(content::WebContents*)>& callback);
    170 
    171   static WindowController* GetWindowControllerOfTab(
    172       const content::WebContents* web_contents);
    173 
    174   // Open the extension's options page.
    175   static void OpenOptionsPage(const Extension* extension, Browser* browser);
    176 };
    177 
    178 }  // namespace extensions
    179 
    180 #endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_TAB_UTIL_H_
    181