OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "modules/battery/NavigatorBattery.h" | 5 #include "modules/battery/NavigatorBattery.h" |
6 | 6 |
7 #include "core/dom/ExecutionContext.h" | 7 #include "core/dom/DOMException.h" |
8 #include "core/frame/LocalFrame.h" | 8 #include "core/dom/Document.h" |
| 9 #include "core/dom/ExceptionCode.h" |
| 10 #include "core/frame/LocalDOMWindow.h" |
9 #include "modules/battery/BatteryManager.h" | 11 #include "modules/battery/BatteryManager.h" |
10 | 12 |
11 namespace blink { | 13 namespace blink { |
12 | 14 |
13 NavigatorBattery::NavigatorBattery(Navigator& navigator) | 15 NavigatorBattery::NavigatorBattery(Navigator& navigator) |
14 : Supplement<Navigator>(navigator) {} | 16 : Supplement<Navigator>(navigator) {} |
15 | 17 |
16 ScriptPromise NavigatorBattery::getBattery(ScriptState* script_state, | 18 ScriptPromise NavigatorBattery::getBattery(ScriptState* script_state, |
17 Navigator& navigator) { | 19 Navigator& navigator) { |
18 return NavigatorBattery::From(navigator).getBattery(script_state); | 20 return NavigatorBattery::From(navigator).getBattery(script_state); |
19 } | 21 } |
20 | 22 |
21 ScriptPromise NavigatorBattery::getBattery(ScriptState* script_state) { | 23 ScriptPromise NavigatorBattery::getBattery(ScriptState* script_state) { |
22 if (!battery_manager_) { | 24 ExecutionContext* execution_context = ExecutionContext::From(script_state); |
23 battery_manager_ = | 25 |
24 BatteryManager::Create(ExecutionContext::From(script_state)); | 26 // Check secure context. |
| 27 String error_message; |
| 28 if (!execution_context->IsSecureContext(error_message)) { |
| 29 return ScriptPromise::RejectWithDOMException( |
| 30 script_state, DOMException::Create(kSecurityError, error_message)); |
25 } | 31 } |
| 32 |
| 33 // Check top-level browsing context. |
| 34 if (!ToDocument(execution_context)->domWindow()->GetFrame() || |
| 35 !ToDocument(execution_context)->GetFrame()->IsMainFrame()) { |
| 36 return ScriptPromise::RejectWithDOMException( |
| 37 script_state, DOMException::Create( |
| 38 kSecurityError, "Not a top-level browsing context.")); |
| 39 } |
| 40 |
| 41 if (!battery_manager_) |
| 42 battery_manager_ = BatteryManager::Create(execution_context); |
| 43 |
26 return battery_manager_->StartRequest(script_state); | 44 return battery_manager_->StartRequest(script_state); |
27 } | 45 } |
28 | 46 |
29 const char* NavigatorBattery::SupplementName() { | 47 const char* NavigatorBattery::SupplementName() { |
30 return "NavigatorBattery"; | 48 return "NavigatorBattery"; |
31 } | 49 } |
32 | 50 |
33 NavigatorBattery& NavigatorBattery::From(Navigator& navigator) { | 51 NavigatorBattery& NavigatorBattery::From(Navigator& navigator) { |
34 NavigatorBattery* supplement = static_cast<NavigatorBattery*>( | 52 NavigatorBattery* supplement = static_cast<NavigatorBattery*>( |
35 Supplement<Navigator>::From(navigator, SupplementName())); | 53 Supplement<Navigator>::From(navigator, SupplementName())); |
36 if (!supplement) { | 54 if (!supplement) { |
37 supplement = new NavigatorBattery(navigator); | 55 supplement = new NavigatorBattery(navigator); |
38 ProvideTo(navigator, SupplementName(), supplement); | 56 ProvideTo(navigator, SupplementName(), supplement); |
39 } | 57 } |
40 return *supplement; | 58 return *supplement; |
41 } | 59 } |
42 | 60 |
43 DEFINE_TRACE(NavigatorBattery) { | 61 DEFINE_TRACE(NavigatorBattery) { |
44 visitor->Trace(battery_manager_); | 62 visitor->Trace(battery_manager_); |
45 Supplement<Navigator>::Trace(visitor); | 63 Supplement<Navigator>::Trace(visitor); |
46 } | 64 } |
47 | 65 |
48 } // namespace blink | 66 } // namespace blink |
OLD | NEW |