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