Chromium Code Reviews| Index: Source/modules/presentation/PresentationAvailability.cpp |
| diff --git a/Source/modules/presentation/PresentationAvailability.cpp b/Source/modules/presentation/PresentationAvailability.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6ee66540369524168d1373e77f7d17f3233056c9 |
| --- /dev/null |
| +++ b/Source/modules/presentation/PresentationAvailability.cpp |
| @@ -0,0 +1,135 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "config.h" |
| +#include "modules/presentation/PresentationAvailability.h" |
| + |
| +#include "bindings/core/v8/ScriptPromiseResolver.h" |
| +#include "core/dom/Document.h" |
| +#include "core/events/Event.h" |
| +#include "modules/EventTargetModulesNames.h" |
| +#include "modules/presentation/PresentationController.h" |
| +#include "public/platform/Platform.h" |
| +#include "public/platform/modules/presentation/WebPresentationClient.h" |
| + |
| +namespace blink { |
| + |
| +namespace { |
| + |
| +WebPresentationClient* presentationClient(ExecutionContext* executionContext) |
| +{ |
| + ASSERT(executionContext && executionContext->isDocument()); |
| + |
| + Document* document = toDocument(executionContext); |
| + if (!document->frame()) |
| + return nullptr; |
| + PresentationController* controller = PresentationController::from(*document->frame()); |
| + return controller ? controller->client() : nullptr; |
| +} |
| + |
| +} // anonymous namespace |
| + |
| +// static |
| +PresentationAvailability* PresentationAvailability::take(ScriptPromiseResolver* resolver, bool value) |
| +{ |
| + PresentationAvailability* presentationAvailability = PresentationAvailability::create(resolver->executionContext(), value); |
| + presentationAvailability->startListening(); |
| + return presentationAvailability; |
| +} |
| + |
| +PresentationAvailability* PresentationAvailability::create(ExecutionContext* executionContext, bool value) |
|
whywhat
2015/07/02 22:32:37
What's different about this create() method? Why s
|
| +{ |
| + PresentationAvailability* presentationAvailability = new PresentationAvailability(executionContext, value); |
| + presentationAvailability->suspendIfNeeded(); |
| + return presentationAvailability; |
| +} |
| + |
| +PresentationAvailability::PresentationAvailability(ExecutionContext* executionContext, bool value) |
| + : ActiveDOMObject(executionContext) |
| + , m_value(value) |
| + , m_listening(false) |
| +{ |
| +} |
| + |
| +PresentationAvailability::~PresentationAvailability() |
| +{ |
| + stopListening(); |
| +} |
| + |
| +const AtomicString& PresentationAvailability::interfaceName() const |
| +{ |
| + return EventTargetNames::PresentationAvailability; |
| +} |
| + |
| +ExecutionContext* PresentationAvailability::executionContext() const |
| +{ |
| + return ActiveDOMObject::executionContext(); |
| +} |
| + |
| +void PresentationAvailability::availabilityChanged(bool value) |
| +{ |
| + if (m_value == value) |
| + return; |
| + |
| + m_value = value; |
| + dispatchEvent(Event::create(EventTypeNames::change)); |
| +} |
| + |
| +bool PresentationAvailability::hasPendingActivity() const |
| +{ |
| + return m_listening; |
| +} |
| + |
| +void PresentationAvailability::resume() |
| +{ |
| + startListening(); |
| +} |
| + |
| +void PresentationAvailability::suspend() |
|
whywhat
2015/07/02 22:32:37
We thought about suspending the monitoring when th
mlamouri (slow - plz ping)
2015/07/03 13:58:26
I do not think it will include page visibility but
|
| +{ |
| + stopListening(); |
| +} |
| + |
| +void PresentationAvailability::stop() |
| +{ |
| + stopListening(); |
| +} |
| + |
| +void PresentationAvailability::startListening() |
| +{ |
| + ASSERT(!m_listening); |
| + |
| + WebPresentationClient* client = presentationClient(executionContext()); |
| + if (!client) |
| + return; |
| + m_listening = true; |
| + client->startListening(this); |
| +} |
| + |
| +void PresentationAvailability::stopListening() |
| +{ |
| + if (!m_listening) |
| + return; |
| + |
| + ASSERT(executionContext()); |
| + |
| + m_listening = false; |
|
whywhat
2015/07/02 22:32:37
nit: perhaps swap this line with the ASSERT above?
mlamouri (slow - plz ping)
2015/07/03 13:58:26
I prefer to have ASSERT() as early as possible.
whywhat
2015/07/03 15:40:47
I think logically m_listening belongs to the if ab
|
| + WebPresentationClient* client = presentationClient(executionContext()); |
| + if (!client) |
| + return; |
| + client->stopListening(this); |
| +} |
| + |
| +bool PresentationAvailability::value() const |
| +{ |
| + return m_value; |
| +} |
| + |
| +DEFINE_TRACE(PresentationAvailability) |
| +{ |
| + RefCountedGarbageCollectedEventTargetWithInlineData<PresentationAvailability>::trace(visitor); |
| + ActiveDOMObject::trace(visitor); |
| +} |
| + |
| +} // namespace |