Skip to content
This repository has been archived by the owner on Jul 18, 2018. It is now read-only.

Commit

Permalink
Revert of [Battery] Allow usage from SecureContext or top-level brows…
Browse files Browse the repository at this point in the history
…ing context only. (patchset #6 id:160001 of https://codereview.chromium.org/2880763002/ )

Reason for revert:
This CL should have gotten a API OWNER lgtm with an Intent to Ship. If the change is accepted by the API OWNERS, we should re-land this.

Original issue's description:
> [Battery] Allow usage from SecureContext or top-level browsing context 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}
> Committed: https://chromium.googlesource.com/chromium/src/+/3543d97ca7a33fb8ad48261ad252428555747896

[email protected],[email protected],[email protected]
# Not skipping CQ checks because original CL landed more than 1 days ago.
BUG=661792

Review-Url: https://codereview.chromium.org/2937553005
Cr-Commit-Position: refs/heads/master@{#479025}
  • Loading branch information
mounirlamouri authored and Commit Bot committed Jun 13, 2017
1 parent 78cedc3 commit f5f500c
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 33 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
Error Code is 18
PASS nav.getBattery() is undefined.
PASS successfullyParsed is true

TEST COMPLETE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,8 @@
w.close();
w = null;
} else if (event.data == "closed") {
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);
shouldBeUndefined("nav.getBattery()");
finishJSTest();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
This is a testharness.js-based test.
FAIL throw a 'SecurityError' when invoking navigator.getBattery() within iframe assert_unreached: Should have rejected: undefined Reached unreachable code
Harness: the test ran to completion.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
This is a testharness.js-based test.
FAIL navigator.getBattery() shall throw a 'SecurityError' in an insecure context assert_unreached: Should have rejected: undefined Reached unreachable code
Harness: the test ran to completion.

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

#include "modules/battery/NavigatorBattery.h"

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

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

ScriptPromise NavigatorBattery::getBattery(ScriptState* 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(ExecutionContext::From(script_state));
}

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

return battery_manager_->StartRequest(script_state);
}

Expand Down

0 comments on commit f5f500c

Please sign in to comment.