Home | History | Annotate | Download | only in encryptedmedia
      1 /*
      2  * Copyright (C) 2013 Apple Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  * 1. Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  * 2. Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
     14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
     17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     23  * THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #ifndef MediaKeySession_h
     27 #define MediaKeySession_h
     28 
     29 #include "bindings/core/v8/ScriptPromiseProperty.h"
     30 #include "core/dom/ActiveDOMObject.h"
     31 #include "core/dom/DOMException.h"
     32 #include "modules/EventTargetModules.h"
     33 #include "platform/Timer.h"
     34 #include "platform/heap/Handle.h"
     35 #include "public/platform/WebContentDecryptionModuleSession.h"
     36 #include "wtf/Forward.h"
     37 
     38 namespace blink {
     39 
     40 class ScriptPromise;
     41 class ScriptState;
     42 class GenericEventQueue;
     43 class MediaKeyError;
     44 class MediaKeys;
     45 class WebContentDecryptionModule;
     46 class WebString;
     47 
     48 // References are held by JS only. However, even if all JS references are
     49 // dropped, it won't be garbage collected until close event received or
     50 // MediaKeys goes away (as determined by the validity of a WeakPtr). This allows
     51 // the CDM to continue to fire events for this session, as long as the session
     52 // is open.
     53 //
     54 // WeakPtr<MediaKeys> is used instead of having MediaKeys and MediaKeySession
     55 // keep references to each other, and then having to inform the other object
     56 // when it gets destroyed.
     57 //
     58 // Because this object controls the lifetime of the WebContentDecryptionModuleSession,
     59 // it may outlive any JavaScript references as long as the MediaKeys object is alive.
     60 // The WebContentDecryptionModuleSession has the same lifetime as this object.
     61 class MediaKeySession FINAL
     62     : public RefCountedGarbageCollectedWillBeGarbageCollectedFinalized<MediaKeySession>, public ActiveDOMObject, public EventTargetWithInlineData
     63     , private WebContentDecryptionModuleSession::Client {
     64     DEFINE_EVENT_TARGET_REFCOUNTING_WILL_BE_REMOVED(RefCountedGarbageCollected<MediaKeySession>);
     65     DEFINE_WRAPPERTYPEINFO();
     66     WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(MediaKeySession);
     67 public:
     68     static MediaKeySession* create(ScriptState*, MediaKeys*, const String& sessionType);
     69     virtual ~MediaKeySession();
     70 
     71     const String& keySystem() const { return m_keySystem; }
     72     String sessionId() const;
     73     ScriptPromise closed(ScriptState*);
     74 
     75     ScriptPromise generateRequest(ScriptState*, const String& initDataType, ArrayBuffer* initData);
     76     ScriptPromise generateRequest(ScriptState*, const String& initDataType, ArrayBufferView* initData);
     77 
     78     void setError(MediaKeyError*);
     79     MediaKeyError* error() { return m_error.get(); }
     80 
     81     ScriptPromise update(ScriptState*, ArrayBuffer* response);
     82     ScriptPromise update(ScriptState*, ArrayBufferView* response);
     83     ScriptPromise release(ScriptState*);
     84 
     85     void enqueueEvent(PassRefPtrWillBeRawPtr<Event>);
     86 
     87     // EventTarget
     88     virtual const AtomicString& interfaceName() const OVERRIDE;
     89     virtual ExecutionContext* executionContext() const OVERRIDE;
     90 
     91     // ActiveDOMObject
     92     virtual bool hasPendingActivity() const OVERRIDE;
     93     virtual void stop() OVERRIDE;
     94 
     95     virtual void trace(Visitor*) OVERRIDE;
     96 
     97 private:
     98     class PendingAction;
     99     friend class NewSessionResult;
    100 
    101     MediaKeySession(ScriptState*, MediaKeys*, const String& sessionType);
    102 
    103     void actionTimerFired(Timer<MediaKeySession>*);
    104 
    105     // WebContentDecryptionModuleSession::Client
    106     virtual void message(const unsigned char* message, size_t messageLength, const WebURL& destinationURL) OVERRIDE;
    107     virtual void ready() OVERRIDE;
    108     virtual void close() OVERRIDE;
    109     virtual void error(MediaKeyErrorCode, unsigned long systemCode) OVERRIDE;
    110     virtual void error(WebContentDecryptionModuleException, unsigned long systemCode, const WebString& errorMessage) OVERRIDE;
    111 
    112     ScriptPromise generateRequestInternal(ScriptState*, const String& initDataType, PassRefPtr<ArrayBuffer> initData);
    113     ScriptPromise updateInternal(ScriptState*, PassRefPtr<ArrayBuffer> response);
    114 
    115     // Called by NewSessionResult when the new sesison has been created.
    116     void finishGenerateRequest();
    117 
    118     String m_keySystem;
    119     RefPtrWillBeMember<MediaKeyError> m_error;
    120     OwnPtrWillBeMember<GenericEventQueue> m_asyncEventQueue;
    121     OwnPtr<WebContentDecryptionModuleSession> m_session;
    122 
    123     // Used to determine if MediaKeys is still active.
    124     WeakMember<MediaKeys> m_mediaKeys;
    125 
    126     // Session properties.
    127     String m_sessionType;
    128 
    129     // Session states.
    130     bool m_isUninitialized;
    131     bool m_isCallable;
    132     bool m_isClosed; // Is the CDM finished with this session?
    133 
    134     // Keep track of the closed promise.
    135     typedef ScriptPromiseProperty<Member<MediaKeySession>, V8UndefinedType, RefPtrWillBeMember<DOMException> > ClosedPromise;
    136     Member<ClosedPromise> m_closedPromise;
    137 
    138     HeapDeque<Member<PendingAction> > m_pendingActions;
    139     Timer<MediaKeySession> m_actionTimer;
    140 };
    141 
    142 } // namespace blink
    143 
    144 #endif // MediaKeySession_h
    145