Skip to content

Commit

Permalink
[Battery] Allow usage from SecureContext or top-level browsing contex…
Browse files Browse the repository at this point in the history
…t only.

Make the Battery Status API available only within a secure context that is
also a top-level browsing context. This disallows the use of the API within
framed content, as well as from any content that is not a secure context.

Details: w3c/battery#10

WPT updated in web-platform-tests/wpt#5871

BUG=661792

Review-Url: https://codereview.chromium.org/2880763002
Cr-Commit-Position: refs/heads/master@{#476263}
  • Loading branch information
riju authored and Commit Bot committed Jun 1, 2017
1 parent 5d0edd9 commit 3543d97
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE


PASS typeof(nav.getBattery()) == 'object' is true
PASS nav.getBattery() is undefined.
Error Code is 18
PASS successfullyParsed is true

TEST COMPLETE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,15 @@
w.close();
w = null;
} else if (event.data == "closed") {
shouldBeUndefined("nav.getBattery()");
finishJSTest();
nav.getBattery().then(battery => {
assert_unreachable('getBattery should reject on a closed window');
})
.catch(error => {
// DOMException.SECURITY_ERR = 18.
debug('Error Code is ' + error.code);
assert_equals(error.code, DOMException.SECURITY_ERR);
});
setTimeout(finishJSTest, 0);
}
}

Expand Down

This file was deleted.

This file was deleted.

28 changes: 23 additions & 5 deletions third_party/WebKit/Source/modules/battery/NavigatorBattery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

#include "modules/battery/NavigatorBattery.h"

#include "core/dom/ExecutionContext.h"
#include "core/frame/LocalFrame.h"
#include "core/dom/DOMException.h"
#include "core/dom/Document.h"
#include "core/dom/ExceptionCode.h"
#include "core/frame/LocalDOMWindow.h"
#include "modules/battery/BatteryManager.h"

namespace blink {
Expand All @@ -19,10 +21,26 @@ ScriptPromise NavigatorBattery::getBattery(ScriptState* script_state,
}

ScriptPromise NavigatorBattery::getBattery(ScriptState* script_state) {
if (!battery_manager_) {
battery_manager_ =
BatteryManager::Create(ExecutionContext::From(script_state));
ExecutionContext* execution_context = ExecutionContext::From(script_state);

// Check secure context.
String error_message;
if (!execution_context->IsSecureContext(error_message)) {
return ScriptPromise::RejectWithDOMException(
script_state, DOMException::Create(kSecurityError, error_message));
}

// Check top-level browsing context.
if (!ToDocument(execution_context)->domWindow()->GetFrame() ||
!ToDocument(execution_context)->GetFrame()->IsMainFrame()) {
return ScriptPromise::RejectWithDOMException(
script_state, DOMException::Create(
kSecurityError, "Not a top-level browsing context."));
}

if (!battery_manager_)
battery_manager_ = BatteryManager::Create(execution_context);

return battery_manager_->StartRequest(script_state);
}

Expand Down

0 comments on commit 3543d97

Please sign in to comment.