Chromium Code Reviews| 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); |
|
timvolodine
2017/05/25 16:01:14
nit: maybe one extra blank line for readability
riju_
2017/05/30 06:36:00
Done.
| |
| 23 battery_manager_ = | 25 // Check secure context. |
| 24 BatteryManager::Create(ExecutionContext::From(script_state)); | 26 String error_message; |
| 27 if (!execution_context->IsSecureContext(error_message)) { | |
| 28 return ScriptPromise::RejectWithDOMException( | |
| 29 script_state, DOMException::Create(kSecurityError, error_message)); | |
| 25 } | 30 } |
| 31 | |
| 32 // Check top-level browsing context. | |
| 33 if (!ToDocument(execution_context)->domWindow()->GetFrame() || | |
| 34 !ToDocument(execution_context)->GetFrame()->IsMainFrame()) { | |
| 35 return ScriptPromise::RejectWithDOMException( | |
| 36 script_state, DOMException::Create( | |
| 37 kSecurityError, "Not a top-level browsing context.")); | |
| 38 } | |
| 39 | |
| 40 if (!battery_manager_) | |
| 41 battery_manager_ = BatteryManager::Create(execution_context); | |
| 42 | |
| 26 return battery_manager_->StartRequest(script_state); | 43 return battery_manager_->StartRequest(script_state); |
| 27 } | 44 } |
| 28 | 45 |
| 29 const char* NavigatorBattery::SupplementName() { | 46 const char* NavigatorBattery::SupplementName() { |
| 30 return "NavigatorBattery"; | 47 return "NavigatorBattery"; |
| 31 } | 48 } |
| 32 | 49 |
| 33 NavigatorBattery& NavigatorBattery::From(Navigator& navigator) { | 50 NavigatorBattery& NavigatorBattery::From(Navigator& navigator) { |
| 34 NavigatorBattery* supplement = static_cast<NavigatorBattery*>( | 51 NavigatorBattery* supplement = static_cast<NavigatorBattery*>( |
| 35 Supplement<Navigator>::From(navigator, SupplementName())); | 52 Supplement<Navigator>::From(navigator, SupplementName())); |
| 36 if (!supplement) { | 53 if (!supplement) { |
| 37 supplement = new NavigatorBattery(navigator); | 54 supplement = new NavigatorBattery(navigator); |
| 38 ProvideTo(navigator, SupplementName(), supplement); | 55 ProvideTo(navigator, SupplementName(), supplement); |
| 39 } | 56 } |
| 40 return *supplement; | 57 return *supplement; |
| 41 } | 58 } |
| 42 | 59 |
| 43 DEFINE_TRACE(NavigatorBattery) { | 60 DEFINE_TRACE(NavigatorBattery) { |
| 44 visitor->Trace(battery_manager_); | 61 visitor->Trace(battery_manager_); |
| 45 Supplement<Navigator>::Trace(visitor); | 62 Supplement<Navigator>::Trace(visitor); |
| 46 } | 63 } |
| 47 | 64 |
| 48 } // namespace blink | 65 } // namespace blink |
| OLD | NEW |