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