OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/extension_tab_util.h" | 5 #include "chrome/browser/extensions/extension_tab_util.h" |
6 | 6 |
7 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" | 7 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" |
| 8 #include "chrome/browser/extensions/event_names.h" |
8 #include "chrome/browser/extensions/tab_helper.h" | 9 #include "chrome/browser/extensions/tab_helper.h" |
9 #include "chrome/browser/extensions/window_controller.h" | 10 #include "chrome/browser/extensions/window_controller.h" |
10 #include "chrome/browser/net/url_fixer_upper.h" | 11 #include "chrome/browser/net/url_fixer_upper.h" |
11 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
12 #include "chrome/browser/sessions/session_id.h" | 13 #include "chrome/browser/sessions/session_id.h" |
13 #include "chrome/browser/ui/browser.h" | 14 #include "chrome/browser/ui/browser.h" |
14 #include "chrome/browser/ui/browser_finder.h" | 15 #include "chrome/browser/ui/browser_finder.h" |
15 #include "chrome/browser/ui/browser_list.h" | 16 #include "chrome/browser/ui/browser_list.h" |
16 #include "chrome/browser/ui/browser_tabstrip.h" | 17 #include "chrome/browser/ui/browser_tabstrip.h" |
17 #include "chrome/browser/ui/browser_window.h" | 18 #include "chrome/browser/ui/browser_window.h" |
18 #include "chrome/browser/ui/tab_contents/tab_contents.h" | 19 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
19 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h" | 20 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h" |
20 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 21 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
21 #include "chrome/common/extensions/extension.h" | 22 #include "chrome/common/extensions/extension.h" |
22 #include "chrome/common/extensions/extension_manifest_constants.h" | 23 #include "chrome/common/extensions/extension_manifest_constants.h" |
| 24 #include "chrome/common/extensions/permissions/api_permission.h" |
23 #include "chrome/common/url_constants.h" | 25 #include "chrome/common/url_constants.h" |
24 #include "content/public/browser/favicon_status.h" | 26 #include "content/public/browser/favicon_status.h" |
25 #include "content/public/browser/navigation_entry.h" | 27 #include "content/public/browser/navigation_entry.h" |
26 #include "content/public/browser/web_contents.h" | 28 #include "content/public/browser/web_contents.h" |
27 #include "googleurl/src/gurl.h" | 29 #include "googleurl/src/gurl.h" |
28 | 30 |
29 namespace keys = extensions::tabs_constants; | 31 namespace keys = extensions::tabs_constants; |
30 | 32 |
31 using content::NavigationEntry; | 33 using content::NavigationEntry; |
32 using content::WebContents; | 34 using content::WebContents; |
33 | 35 |
| 36 namespace { |
| 37 |
| 38 void StripTabOfSensitiveData(DictionaryValue* tab) { |
| 39 tab->SetString(keys::kUrlKey, std::string()); |
| 40 tab->SetString(keys::kTitleKey, std::string()); |
| 41 tab->SetString(keys::kFaviconUrlKey, std::string()); |
| 42 } |
| 43 |
| 44 } |
| 45 |
34 int ExtensionTabUtil::GetWindowId(const Browser* browser) { | 46 int ExtensionTabUtil::GetWindowId(const Browser* browser) { |
35 return browser->session_id().id(); | 47 return browser->session_id().id(); |
36 } | 48 } |
37 | 49 |
38 int ExtensionTabUtil::GetWindowIdOfTabStripModel( | 50 int ExtensionTabUtil::GetWindowIdOfTabStripModel( |
39 const TabStripModel* tab_strip_model) { | 51 const TabStripModel* tab_strip_model) { |
40 for (BrowserList::const_iterator it = BrowserList::begin(); | 52 for (BrowserList::const_iterator it = BrowserList::begin(); |
41 it != BrowserList::end(); ++it) { | 53 it != BrowserList::end(); ++it) { |
42 if ((*it)->tab_strip_model() == tab_strip_model) | 54 if ((*it)->tab_strip_model() == tab_strip_model) |
43 return GetWindowId(*it); | 55 return GetWindowId(*it); |
44 } | 56 } |
45 return -1; | 57 return -1; |
46 } | 58 } |
47 | 59 |
48 int ExtensionTabUtil::GetTabId(const WebContents* web_contents) { | 60 int ExtensionTabUtil::GetTabId(const WebContents* web_contents) { |
49 return SessionID::IdForTab(TabContents::FromWebContents(web_contents)); | 61 return SessionID::IdForTab(TabContents::FromWebContents(web_contents)); |
50 } | 62 } |
51 | 63 |
52 std::string ExtensionTabUtil::GetTabStatusText(bool is_loading) { | 64 std::string ExtensionTabUtil::GetTabStatusText(bool is_loading) { |
53 return is_loading ? keys::kStatusValueLoading : keys::kStatusValueComplete; | 65 return is_loading ? keys::kStatusValueLoading : keys::kStatusValueComplete; |
54 } | 66 } |
55 | 67 |
56 int ExtensionTabUtil::GetWindowIdOfTab(const WebContents* web_contents) { | 68 int ExtensionTabUtil::GetWindowIdOfTab(const WebContents* web_contents) { |
57 return SessionID::IdForWindowContainingTab( | 69 return SessionID::IdForWindowContainingTab( |
58 TabContents::FromWebContents(web_contents)); | 70 TabContents::FromWebContents(web_contents)); |
59 } | 71 } |
60 | 72 |
61 DictionaryValue* ExtensionTabUtil::CreateTabValue(const WebContents* contents) { | 73 DictionaryValue* ExtensionTabUtil::CreateTabValue( |
| 74 const WebContents* contents, |
| 75 const extensions::Extension* extension) { |
62 // Find the tab strip and index of this guy. | 76 // Find the tab strip and index of this guy. |
63 TabStripModel* tab_strip = NULL; | 77 TabStripModel* tab_strip = NULL; |
64 int tab_index; | 78 int tab_index; |
65 if (ExtensionTabUtil::GetTabStripModel(contents, &tab_strip, &tab_index)) | 79 if (ExtensionTabUtil::GetTabStripModel(contents, &tab_strip, &tab_index)) { |
66 return ExtensionTabUtil::CreateTabValue(contents, tab_strip, tab_index); | 80 return ExtensionTabUtil::CreateTabValue(contents, |
| 81 tab_strip, |
| 82 tab_index, |
| 83 extension); |
| 84 } |
67 | 85 |
68 // Couldn't find it. This can happen if the tab is being dragged. | 86 // Couldn't find it. This can happen if the tab is being dragged. |
69 return ExtensionTabUtil::CreateTabValue(contents, NULL, -1); | 87 return ExtensionTabUtil::CreateTabValue(contents, NULL, -1, extension); |
70 } | 88 } |
71 | 89 |
72 ListValue* ExtensionTabUtil::CreateTabList(const Browser* browser) { | 90 ListValue* ExtensionTabUtil::CreateTabList( |
| 91 const Browser* browser, |
| 92 const extensions::Extension* extension) { |
73 ListValue* tab_list = new ListValue(); | 93 ListValue* tab_list = new ListValue(); |
74 TabStripModel* tab_strip = browser->tab_strip_model(); | 94 TabStripModel* tab_strip = browser->tab_strip_model(); |
75 for (int i = 0; i < tab_strip->count(); ++i) { | 95 for (int i = 0; i < tab_strip->count(); ++i) { |
76 tab_list->Append(ExtensionTabUtil::CreateTabValue( | 96 tab_list->Append(CreateTabValue( |
77 tab_strip->GetTabContentsAt(i)->web_contents(), tab_strip, i)); | 97 tab_strip->GetTabContentsAt(i)->web_contents(), |
| 98 tab_strip, |
| 99 i, |
| 100 extension)); |
78 } | 101 } |
79 | 102 |
80 return tab_list; | 103 return tab_list; |
81 } | 104 } |
82 | 105 |
83 DictionaryValue* ExtensionTabUtil::CreateTabValue(const WebContents* contents, | 106 DictionaryValue* ExtensionTabUtil::CreateTabValue( |
84 TabStripModel* tab_strip, | 107 const WebContents* contents, |
85 int tab_index) { | 108 TabStripModel* tab_strip, |
| 109 int tab_index, |
| 110 const extensions::Extension* extension) { |
86 DictionaryValue* result = new DictionaryValue(); | 111 DictionaryValue* result = new DictionaryValue(); |
87 bool is_loading = contents->IsLoading(); | 112 bool is_loading = contents->IsLoading(); |
88 result->SetInteger(keys::kIdKey, ExtensionTabUtil::GetTabId(contents)); | 113 result->SetInteger(keys::kIdKey, GetTabId(contents)); |
89 result->SetInteger(keys::kIndexKey, tab_index); | 114 result->SetInteger(keys::kIndexKey, tab_index); |
90 result->SetInteger(keys::kWindowIdKey, | 115 result->SetInteger(keys::kWindowIdKey, GetWindowIdOfTab(contents)); |
91 ExtensionTabUtil::GetWindowIdOfTab(contents)); | |
92 result->SetString(keys::kUrlKey, contents->GetURL().spec()); | |
93 result->SetString(keys::kStatusKey, GetTabStatusText(is_loading)); | 116 result->SetString(keys::kStatusKey, GetTabStatusText(is_loading)); |
94 result->SetBoolean(keys::kActiveKey, | 117 result->SetBoolean(keys::kActiveKey, |
95 tab_strip && tab_index == tab_strip->active_index()); | 118 tab_strip && tab_index == tab_strip->active_index()); |
96 result->SetBoolean(keys::kSelectedKey, | 119 result->SetBoolean(keys::kSelectedKey, |
97 tab_strip && tab_index == tab_strip->active_index()); | 120 tab_strip && tab_index == tab_strip->active_index()); |
98 result->SetBoolean(keys::kHighlightedKey, | 121 result->SetBoolean(keys::kHighlightedKey, |
99 tab_strip && tab_strip->IsTabSelected(tab_index)); | 122 tab_strip && tab_strip->IsTabSelected(tab_index)); |
100 result->SetBoolean(keys::kPinnedKey, | 123 result->SetBoolean(keys::kPinnedKey, |
101 tab_strip && tab_strip->IsTabPinned(tab_index)); | 124 tab_strip && tab_strip->IsTabPinned(tab_index)); |
102 result->SetString(keys::kTitleKey, contents->GetTitle()); | |
103 result->SetBoolean(keys::kIncognitoKey, | 125 result->SetBoolean(keys::kIncognitoKey, |
104 contents->GetBrowserContext()->IsOffTheRecord()); | 126 contents->GetBrowserContext()->IsOffTheRecord()); |
105 | 127 |
| 128 // If we have an extension without permissions, we don't add sensitive data. |
| 129 if (!extension || (extension && extension->HasAPIPermissionForTab( |
| 130 tab_index, extensions::APIPermission::kTab))) { |
| 131 result->SetString(keys::kUrlKey, contents->GetURL().spec()); |
| 132 result->SetString(keys::kTitleKey, contents->GetTitle()); |
| 133 if (!is_loading) { |
| 134 NavigationEntry* entry = contents->GetController().GetActiveEntry(); |
| 135 if (entry && entry->GetFavicon().valid) |
| 136 result->SetString(keys::kFaviconUrlKey, entry->GetFavicon().url.spec()); |
| 137 } |
| 138 } |
| 139 |
106 if (tab_strip) { | 140 if (tab_strip) { |
107 content::NavigationController* opener = | 141 content::NavigationController* opener = |
108 tab_strip->GetOpenerOfTabContentsAt(tab_index); | 142 tab_strip->GetOpenerOfTabContentsAt(tab_index); |
109 if (opener) { | 143 if (opener) { |
110 result->SetInteger(keys::kOpenerTabIdKey, | 144 result->SetInteger(keys::kOpenerTabIdKey, |
111 ExtensionTabUtil::GetTabId(opener->GetWebContents())); | 145 GetTabId(opener->GetWebContents())); |
112 } | 146 } |
113 } | 147 } |
114 | 148 |
115 if (!is_loading) { | |
116 NavigationEntry* entry = contents->GetController().GetActiveEntry(); | |
117 if (entry) { | |
118 if (entry->GetFavicon().valid) | |
119 result->SetString(keys::kFaviconUrlKey, entry->GetFavicon().url.spec()); | |
120 } | |
121 } | |
122 | |
123 return result; | 149 return result; |
124 } | 150 } |
125 | 151 |
126 DictionaryValue* ExtensionTabUtil::CreateTabValueActive( | 152 DictionaryValue* ExtensionTabUtil::CreateTabValueActive( |
127 const WebContents* contents, | 153 const WebContents* contents, |
128 bool active) { | 154 bool active, |
129 DictionaryValue* result = ExtensionTabUtil::CreateTabValue(contents); | 155 const extensions::Extension* extension) { |
| 156 DictionaryValue* result = CreateTabValue(contents, extension); |
130 result->SetBoolean(keys::kSelectedKey, active); | 157 result->SetBoolean(keys::kSelectedKey, active); |
131 return result; | 158 return result; |
132 } | 159 } |
133 | 160 |
| 161 void ExtensionTabUtil::MaybeStripEventArgsOfSensitiveData( |
| 162 const std::string& event_name, |
| 163 const extensions::Extension* extension, |
| 164 base::ListValue* event_args) { |
| 165 int arg = 0; |
| 166 if (event_name == extensions::event_names::kOnTabUpdated) |
| 167 arg = 2; |
| 168 else if (event_name == extensions::event_names::kOnTabUpdated) |
| 169 arg = 0; |
| 170 else |
| 171 return; |
| 172 |
| 173 if (!extension->HasAPIPermission(extensions::APIPermission::kTab)) { |
| 174 DictionaryValue* tab; |
| 175 event_args->GetDictionary(arg, &tab); |
| 176 StripTabOfSensitiveData(tab); |
| 177 } |
| 178 } |
| 179 |
134 bool ExtensionTabUtil::GetTabStripModel(const WebContents* web_contents, | 180 bool ExtensionTabUtil::GetTabStripModel(const WebContents* web_contents, |
135 TabStripModel** tab_strip_model, | 181 TabStripModel** tab_strip_model, |
136 int* tab_index) { | 182 int* tab_index) { |
137 DCHECK(web_contents); | 183 DCHECK(web_contents); |
138 DCHECK(tab_strip_model); | 184 DCHECK(tab_strip_model); |
139 DCHECK(tab_index); | 185 DCHECK(tab_index); |
140 | 186 |
141 for (BrowserList::const_iterator it = BrowserList::begin(); | 187 for (BrowserList::const_iterator it = BrowserList::begin(); |
142 it != BrowserList::end(); ++it) { | 188 it != BrowserList::end(); ++it) { |
143 TabStripModel* tab_strip = (*it)->tab_strip_model(); | 189 TabStripModel* tab_strip = (*it)->tab_strip_model(); |
(...skipping 10 matching lines...) Expand all Loading... |
154 | 200 |
155 bool ExtensionTabUtil::GetDefaultTab(Browser* browser, | 201 bool ExtensionTabUtil::GetDefaultTab(Browser* browser, |
156 TabContents** contents, | 202 TabContents** contents, |
157 int* tab_id) { | 203 int* tab_id) { |
158 DCHECK(browser); | 204 DCHECK(browser); |
159 DCHECK(contents); | 205 DCHECK(contents); |
160 | 206 |
161 *contents = chrome::GetActiveTabContents(browser); | 207 *contents = chrome::GetActiveTabContents(browser); |
162 if (*contents) { | 208 if (*contents) { |
163 if (tab_id) | 209 if (tab_id) |
164 *tab_id = ExtensionTabUtil::GetTabId((*contents)->web_contents()); | 210 *tab_id = GetTabId((*contents)->web_contents()); |
165 return true; | 211 return true; |
166 } | 212 } |
167 | 213 |
168 return false; | 214 return false; |
169 } | 215 } |
170 | 216 |
171 bool ExtensionTabUtil::GetTabById(int tab_id, | 217 bool ExtensionTabUtil::GetTabById(int tab_id, |
172 Profile* profile, | 218 Profile* profile, |
173 bool include_incognito, | 219 bool include_incognito, |
174 Browser** browser, | 220 Browser** browser, |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
264 | 310 |
265 // static | 311 // static |
266 extensions::WindowController* ExtensionTabUtil::GetWindowControllerOfTab( | 312 extensions::WindowController* ExtensionTabUtil::GetWindowControllerOfTab( |
267 const WebContents* web_contents) { | 313 const WebContents* web_contents) { |
268 Browser* browser = browser::FindBrowserWithWebContents(web_contents); | 314 Browser* browser = browser::FindBrowserWithWebContents(web_contents); |
269 if (browser != NULL) | 315 if (browser != NULL) |
270 return browser->extension_window_controller(); | 316 return browser->extension_window_controller(); |
271 | 317 |
272 return NULL; | 318 return NULL; |
273 } | 319 } |
OLD | NEW |