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, |
Aaron Boodman
2012/08/22 21:22:05
This was here before, but I think you can just cal
chebert
2012/08/23 17:34:18
Done.
| |
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(ExtensionTabUtil::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, ExtensionTabUtil::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, |
91 ExtensionTabUtil::GetWindowIdOfTab(contents)); | 116 ExtensionTabUtil::GetWindowIdOfTab(contents)); |
92 result->SetString(keys::kUrlKey, contents->GetURL().spec()); | |
93 result->SetString(keys::kStatusKey, GetTabStatusText(is_loading)); | 117 result->SetString(keys::kStatusKey, GetTabStatusText(is_loading)); |
94 result->SetBoolean(keys::kActiveKey, | 118 result->SetBoolean(keys::kActiveKey, |
95 tab_strip && tab_index == tab_strip->active_index()); | 119 tab_strip && tab_index == tab_strip->active_index()); |
96 result->SetBoolean(keys::kSelectedKey, | 120 result->SetBoolean(keys::kSelectedKey, |
97 tab_strip && tab_index == tab_strip->active_index()); | 121 tab_strip && tab_index == tab_strip->active_index()); |
98 result->SetBoolean(keys::kHighlightedKey, | 122 result->SetBoolean(keys::kHighlightedKey, |
99 tab_strip && tab_strip->IsTabSelected(tab_index)); | 123 tab_strip && tab_strip->IsTabSelected(tab_index)); |
100 result->SetBoolean(keys::kPinnedKey, | 124 result->SetBoolean(keys::kPinnedKey, |
101 tab_strip && tab_strip->IsTabPinned(tab_index)); | 125 tab_strip && tab_strip->IsTabPinned(tab_index)); |
102 result->SetString(keys::kTitleKey, contents->GetTitle()); | |
103 result->SetBoolean(keys::kIncognitoKey, | 126 result->SetBoolean(keys::kIncognitoKey, |
104 contents->GetBrowserContext()->IsOffTheRecord()); | 127 contents->GetBrowserContext()->IsOffTheRecord()); |
128 result->SetString(keys::kUrlKey, contents->GetURL().spec()); | |
129 result->SetString(keys::kTitleKey, contents->GetTitle()); | |
105 | 130 |
106 if (tab_strip) { | 131 if (tab_strip) { |
107 content::NavigationController* opener = | 132 content::NavigationController* opener = |
108 tab_strip->GetOpenerOfTabContentsAt(tab_index); | 133 tab_strip->GetOpenerOfTabContentsAt(tab_index); |
109 if (opener) { | 134 if (opener) { |
110 result->SetInteger(keys::kOpenerTabIdKey, | 135 result->SetInteger(keys::kOpenerTabIdKey, |
111 ExtensionTabUtil::GetTabId(opener->GetWebContents())); | 136 ExtensionTabUtil::GetTabId(opener->GetWebContents())); |
112 } | 137 } |
113 } | 138 } |
114 | 139 |
115 if (!is_loading) { | 140 if (!is_loading) { |
116 NavigationEntry* entry = contents->GetController().GetActiveEntry(); | 141 NavigationEntry* entry = contents->GetController().GetActiveEntry(); |
117 if (entry) { | 142 if (entry) { |
118 if (entry->GetFavicon().valid) | 143 if (entry->GetFavicon().valid) |
119 result->SetString(keys::kFaviconUrlKey, entry->GetFavicon().url.spec()); | 144 result->SetString(keys::kFaviconUrlKey, entry->GetFavicon().url.spec()); |
120 } | 145 } |
121 } | 146 } |
122 | 147 |
148 // If we have an extension without permissions, we need to strip off the | |
149 // sensitive data. | |
150 if (extension && | |
151 !extension->HasAPIPermission(extensions::APIPermission::kTab)) { | |
Aaron Boodman
2012/08/22 21:22:05
You should use HasAPIPermissionForTab instead. It
chebert
2012/08/23 17:34:18
Done.
| |
152 StripTabOfSensitiveData(result); | |
Aaron Boodman
2012/08/22 21:22:05
Rather than stripping sensitive data after the fac
chebert
2012/08/23 17:34:18
Done.
| |
153 } | |
154 | |
123 return result; | 155 return result; |
124 } | 156 } |
125 | 157 |
126 DictionaryValue* ExtensionTabUtil::CreateTabValueActive( | 158 DictionaryValue* ExtensionTabUtil::CreateTabValueActive( |
127 const WebContents* contents, | 159 const WebContents* contents, |
128 bool active) { | 160 bool active, |
129 DictionaryValue* result = ExtensionTabUtil::CreateTabValue(contents); | 161 const extensions::Extension* extension) { |
162 DictionaryValue* result = ExtensionTabUtil::CreateTabValue(contents, | |
163 extension); | |
130 result->SetBoolean(keys::kSelectedKey, active); | 164 result->SetBoolean(keys::kSelectedKey, active); |
131 return result; | 165 return result; |
132 } | 166 } |
133 | 167 |
168 void ExtensionTabUtil::MaybeStripEventArgsOfSensitiveData( | |
169 const std::string& event_name, | |
170 const extensions::Extension* extension, | |
171 base::ListValue* event_args) { | |
172 int arg = 0; | |
173 if (event_name == extensions::event_names::kOnTabUpdated) | |
174 arg = 2; | |
175 else if (event_name == extensions::event_names::kOnTabUpdated) | |
176 arg = 0; | |
177 else | |
178 return; | |
179 | |
180 if (!extension->HasAPIPermission(extensions::APIPermission::kTab)) { | |
181 DictionaryValue* tab; | |
182 event_args->GetDictionary(arg, &tab); | |
183 StripTabOfSensitiveData(tab); | |
184 } | |
185 } | |
186 | |
134 bool ExtensionTabUtil::GetTabStripModel(const WebContents* web_contents, | 187 bool ExtensionTabUtil::GetTabStripModel(const WebContents* web_contents, |
135 TabStripModel** tab_strip_model, | 188 TabStripModel** tab_strip_model, |
136 int* tab_index) { | 189 int* tab_index) { |
137 DCHECK(web_contents); | 190 DCHECK(web_contents); |
138 DCHECK(tab_strip_model); | 191 DCHECK(tab_strip_model); |
139 DCHECK(tab_index); | 192 DCHECK(tab_index); |
140 | 193 |
141 for (BrowserList::const_iterator it = BrowserList::begin(); | 194 for (BrowserList::const_iterator it = BrowserList::begin(); |
142 it != BrowserList::end(); ++it) { | 195 it != BrowserList::end(); ++it) { |
143 TabStripModel* tab_strip = (*it)->tab_strip_model(); | 196 TabStripModel* tab_strip = (*it)->tab_strip_model(); |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
263 | 316 |
264 // static | 317 // static |
265 extensions::WindowController* ExtensionTabUtil::GetWindowControllerOfTab( | 318 extensions::WindowController* ExtensionTabUtil::GetWindowControllerOfTab( |
266 const WebContents* web_contents) { | 319 const WebContents* web_contents) { |
267 Browser* browser = browser::FindBrowserWithWebContents(web_contents); | 320 Browser* browser = browser::FindBrowserWithWebContents(web_contents); |
268 if (browser != NULL) | 321 if (browser != NULL) |
269 return browser->extension_window_controller(); | 322 return browser->extension_window_controller(); |
270 | 323 |
271 return NULL; | 324 return NULL; |
272 } | 325 } |
OLD | NEW |