Skip to content
This repository was archived by the owner on Jan 4, 2019. It is now read-only.

Commit cc183f2

Browse files
committed
update chome.tabs api
add additional tab properties to tab helper auditors: @bbondy @jonathansampson
1 parent 730d5fe commit cc183f2

File tree

6 files changed

+45
-3
lines changed

6 files changed

+45
-3
lines changed

atom/browser/api/atom_api_web_contents.cc

+9
Original file line numberDiff line numberDiff line change
@@ -1586,6 +1586,14 @@ bool WebContents::ExecuteScriptInTab(mate::Arguments* args) {
15861586

15871587
return tab_helper->ExecuteScriptInTab(args);
15881588
}
1589+
1590+
void WebContents::SetTabValues(const base::DictionaryValue& values) {
1591+
auto tab_helper = extensions::TabHelper::FromWebContents(web_contents());
1592+
if (!tab_helper)
1593+
return;
1594+
1595+
return tab_helper->SetTabValues(values);
1596+
}
15891597
#endif
15901598

15911599
void WebContents::TabTraverse(bool reverse) {
@@ -2034,6 +2042,7 @@ void WebContents::BuildPrototype(v8::Isolate* isolate,
20342042
&WebContents::EnablePreferredSizeMode)
20352043
#if defined(ENABLE_EXTENSIONS)
20362044
.SetMethod("executeScriptInTab", &WebContents::ExecuteScriptInTab)
2045+
.SetMethod("setTabValues", &WebContents::SetTabValues)
20372046
#endif
20382047
.SetMethod("autofillSelect", &WebContents::AutofillSelect)
20392048
.SetMethod("autofillPopupHidden", &WebContents::AutofillPopupHidden)

atom/browser/api/atom_api_web_contents.h

+1
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ class WebContents : public mate::TrackableObject<WebContents>,
195195

196196
#if defined(ENABLE_EXTENSIONS)
197197
bool ExecuteScriptInTab(mate::Arguments* args);
198+
void SetTabValues(const base::DictionaryValue& values);
198199
#endif
199200

200201
// Send messages to browser.

atom/browser/extensions/tab_helper.cc

+13-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ const char kWindowIdKey[] = "windowId";
3737
const char kTitleKey[] = "title";
3838
const char kUrlKey[] = "url";
3939
const char kStatusKey[] = "status";
40-
}
40+
const char kAudibleKey[] = "audible";
41+
const char kMutedKey[] = "muted";
42+
} // namespace keys
4143

4244
static std::map<int, std::pair<int, int>> render_view_map_;
4345
static std::map<int, int> active_tab_map_;
@@ -47,6 +49,7 @@ namespace extensions {
4749

4850
TabHelper::TabHelper(content::WebContents* contents)
4951
: content::WebContentsObserver(contents),
52+
values_(new base::DictionaryValue),
5053
script_executor_(
5154
new ScriptExecutor(contents, &script_execution_observers_)) {
5255
session_id_ = next_id++;
@@ -75,6 +78,10 @@ void TabHelper::SetActive(bool active) {
7578
active_tab_map_[window_id_] = -1;
7679
}
7780

81+
void TabHelper::SetTabValues(const base::DictionaryValue& values) {
82+
values_->MergeDictionary(&values);
83+
}
84+
7885
void TabHelper::RenderViewCreated(content::RenderViewHost* render_view_host) {
7986
render_view_map_[session_id_] = std::make_pair(
8087
render_view_host->GetProcess()->GetID(),
@@ -264,8 +271,10 @@ base::DictionaryValue* TabHelper::CreateTabValue(
264271
content::WebContents* contents) {
265272
auto tab_id = IdForTab(contents);
266273
auto window_id = IdForWindowContainingTab(contents);
274+
auto tab_helper = TabHelper::FromWebContents(contents);
267275

268-
std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue);
276+
std::unique_ptr<base::DictionaryValue> result(
277+
tab_helper->getTabValues()->CreateDeepCopy());
269278

270279
result->SetInteger(keys::kIdKey, tab_id);
271280
result->SetInteger(keys::kTabIdKey, tab_id);
@@ -277,6 +286,8 @@ base::DictionaryValue* TabHelper::CreateTabValue(
277286
result->SetString(keys::kTitleKey, contents->GetTitle());
278287
result->SetString(keys::kStatusKey, contents->IsLoading()
279288
? "loading" : "complete");
289+
result->SetBoolean(keys::kAudibleKey, contents->WasRecentlyAudible());
290+
result->SetBoolean(keys::kMutedKey, contents->IsAudioMuted());
280291
return result.release();
281292
}
282293

atom/browser/extensions/tab_helper.h

+8
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ extern const char kIdKey[];
3333
extern const char kTabIdKey[];
3434
extern const char kIncognitoKey[];
3535
extern const char kWindowIdKey[];
36+
extern const char kAudibleKey[];
37+
extern const char kMutedKey[];
3638
}
3739

3840
namespace extensions {
@@ -57,6 +59,11 @@ class TabHelper : public content::WebContentsObserver,
5759
// Set this tab as the active tab in its window
5860
void SetActive(bool active);
5961

62+
void SetTabValues(const base::DictionaryValue& values);
63+
base::DictionaryValue* getTabValues() {
64+
return values_.get();
65+
}
66+
6067
bool ExecuteScriptInTab(mate::Arguments* args);
6168

6269
ScriptExecutor* script_executor() {
@@ -112,6 +119,7 @@ class TabHelper : public content::WebContentsObserver,
112119
// Unique identifier of the window the tab is in.
113120
int32_t window_id_ = -1;
114121

122+
std::unique_ptr<base::DictionaryValue> values_;
115123
std::unique_ptr<ScriptExecutor> script_executor_;
116124

117125
DISALLOW_COPY_AND_ASSIGN(TabHelper);

atom/common/api/resources/tabs_bindings.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,15 @@ var bindings = {
137137
ipc.once('chrome-tabs-get-response-' + responseId, function (evt, tab) {
138138
cb(tab)
139139
})
140-
ipc.send('chrome-tabs-get', responseId, {})
140+
ipc.send('chrome-tabs-get', responseId, tabId)
141+
},
142+
143+
getCurrent: function(cb) {
144+
var responseId = ++id
145+
ipc.once('chrome-tabs-get-current-response-' + responseId, function (evt, tab) {
146+
cb(tab)
147+
})
148+
ipc.send('chrome-tabs-get-current', responseId)
141149
},
142150

143151
query: function(queryInfo, cb) {

lib/browser/api/extensions.js

+5
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,11 @@ ipcMain.on('chrome-tabs-remove', function (evt, responseId, tabIds) {
441441
evt.sender.send('chrome-tabs-remove-' + responseId)
442442
})
443443

444+
ipcMain.on('chrome-tabs-get-current', function (evt, responseId) {
445+
var response = getTabValue(evt.sender.getId())
446+
evt.sender.send('chrome-tabs-get-current-response-' + responseId, response)
447+
})
448+
444449
ipcMain.on('chrome-tabs-get', function (evt, responseId, tabId) {
445450
var response = getTabValue(tabId)
446451
evt.sender.send('chrome-tabs-get-response-' + responseId, response)

0 commit comments

Comments
 (0)