| OLD | NEW | 
|   1 // Copyright 2015 The Chromium Authors. All rights reserved. |   1 // Copyright 2015 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/mediasession/MediaSession.h" |   5 #include "modules/mediasession/MediaSession.h" | 
|   6  |   6  | 
|   7 #include "bindings/core/v8/CallbackPromiseAdapter.h" |  | 
|   8 #include "bindings/core/v8/ScriptPromiseResolver.h" |  | 
|   9 #include "bindings/core/v8/ScriptState.h" |   7 #include "bindings/core/v8/ScriptState.h" | 
|  10 #include "core/dom/DOMException.h" |   8 #include "core/dom/Document.h" | 
|  11 #include "core/dom/ExceptionCode.h" |   9 #include "core/dom/ExecutionContext.h" | 
|  12 #include "core/frame/LocalDOMWindow.h" |  | 
|  13 #include "core/frame/LocalFrame.h" |  10 #include "core/frame/LocalFrame.h" | 
|  14 #include "core/loader/FrameLoaderClient.h" |  | 
|  15 #include "modules/mediasession/MediaMetadata.h" |  11 #include "modules/mediasession/MediaMetadata.h" | 
|  16 #include "modules/mediasession/MediaSessionError.h" |  12 #include "modules/mediasession/MediaMetadataSanitizer.h" | 
 |  13 #include "public/platform/InterfaceProvider.h" | 
|  17 #include <memory> |  14 #include <memory> | 
|  18  |  15  | 
|  19 namespace blink { |  16 namespace blink { | 
|  20  |  17  | 
|  21 MediaSession::MediaSession(std::unique_ptr<WebMediaSession> webMediaSession) |  18 MediaSession::MediaSession() = default; | 
|  22     : m_webMediaSession(std::move(webMediaSession)) { |  19  | 
|  23   DCHECK(m_webMediaSession); |  20 MediaSession* MediaSession::create() { | 
 |  21   return new MediaSession(); | 
|  24 } |  22 } | 
|  25  |  23  | 
|  26 MediaSession* MediaSession::create(ExecutionContext* context, |  24 void MediaSession::setMetadata(ScriptState* scriptState, | 
|  27                                    ExceptionState& exceptionState) { |  25                                MediaMetadata* metadata) { | 
|  28   Document* document = toDocument(context); |  26   if (getService(scriptState)) { | 
|  29   LocalFrame* frame = document->frame(); |  27     getService(scriptState) | 
|  30   FrameLoaderClient* client = frame->loader().client(); |  28         ->SetMetadata( | 
|  31   std::unique_ptr<WebMediaSession> webMediaSession = |  29             MediaMetadataSanitizer::sanitizeAndConvertToMojo(metadata)); | 
|  32       client->createWebMediaSession(); |  | 
|  33   if (!webMediaSession) { |  | 
|  34     exceptionState.throwDOMException(NotSupportedError, |  | 
|  35                                      "Missing platform implementation."); |  | 
|  36     return nullptr; |  | 
|  37   } |  | 
|  38   return new MediaSession(std::move(webMediaSession)); |  | 
|  39 } |  | 
|  40  |  | 
|  41 ScriptPromise MediaSession::activate(ScriptState* scriptState) { |  | 
|  42   ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |  | 
|  43   ScriptPromise promise = resolver->promise(); |  | 
|  44  |  | 
|  45   m_webMediaSession->activate( |  | 
|  46       new CallbackPromiseAdapter<void, MediaSessionError>(resolver)); |  | 
|  47   return promise; |  | 
|  48 } |  | 
|  49  |  | 
|  50 ScriptPromise MediaSession::deactivate(ScriptState* scriptState) { |  | 
|  51   ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |  | 
|  52   ScriptPromise promise = resolver->promise(); |  | 
|  53  |  | 
|  54   m_webMediaSession->deactivate( |  | 
|  55       new CallbackPromiseAdapter<void, void>(resolver)); |  | 
|  56   return promise; |  | 
|  57 } |  | 
|  58  |  | 
|  59 void MediaSession::setMetadata(MediaMetadata* metadata) { |  | 
|  60   m_metadata = metadata; |  | 
|  61   if (metadata) { |  | 
|  62     WebMediaMetadata webMetadata = (WebMediaMetadata)*metadata; |  | 
|  63     m_webMediaSession->setMetadata(&webMetadata); |  | 
|  64   } else { |  | 
|  65     m_webMediaSession->setMetadata(nullptr); |  | 
|  66   } |  30   } | 
|  67 } |  31 } | 
|  68  |  32  | 
|  69 MediaMetadata* MediaSession::metadata() const { |  33 MediaMetadata* MediaSession::metadata(ScriptState*) const { | 
|  70   return m_metadata; |  34   return m_metadata; | 
|  71 } |  35 } | 
|  72  |  36  | 
 |  37 mojom::blink::MediaSessionService* MediaSession::getService( | 
 |  38     ScriptState* scriptState) { | 
 |  39   if (!m_service) { | 
 |  40     InterfaceProvider* interfaceProvider = nullptr; | 
 |  41     DCHECK(scriptState->getExecutionContext()->isDocument()) | 
 |  42         << "MediaSession::getService() is only available from a frame"; | 
 |  43     Document* document = toDocument(scriptState->getExecutionContext()); | 
 |  44     if (document->frame()) | 
 |  45       interfaceProvider = document->frame()->interfaceProvider(); | 
 |  46  | 
 |  47     if (interfaceProvider) | 
 |  48       interfaceProvider->getInterface(mojo::GetProxy(&m_service)); | 
 |  49   } | 
 |  50   return m_service.get(); | 
 |  51 } | 
 |  52  | 
|  73 DEFINE_TRACE(MediaSession) { |  53 DEFINE_TRACE(MediaSession) { | 
|  74   visitor->trace(m_metadata); |  54   visitor->trace(m_metadata); | 
|  75 } |  55 } | 
|  76  |  56  | 
|  77 }  // namespace blink |  57 }  // namespace blink | 
| OLD | NEW |