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/v8/ScriptWrappable.h"
     30 #include "core/dom/ContextLifecycleObserver.h"
     31 #include "core/events/EventTarget.h"
     32 #include "platform/Timer.h"
     33 #include "platform/drm/ContentDecryptionModuleSession.h"
     34 #include "wtf/Deque.h"
     35 #include "wtf/PassRefPtr.h"
     36 #include "wtf/RefCounted.h"
     37 #include "wtf/Uint8Array.h"
     38 #include "wtf/text/WTFString.h"
     39 
     40 namespace WebCore {
     41 
     42 class ContentDecryptionModule;
     43 class ContentDecryptionModuleSession;
     44 class ExceptionState;
     45 class GenericEventQueue;
     46 class MediaKeyError;
     47 class MediaKeys;
     48 
     49 // References are held by JS and MediaKeys.
     50 // Because this object controls the lifetime of the ContentDecryptionModuleSession,
     51 // it may outlive any references to it as long as the MediaKeys object is alive.
     52 // The ContentDecryptionModuleSession has the same lifetime as this object.
     53 class MediaKeySession
     54     : public RefCounted<MediaKeySession>, public ScriptWrappable, public EventTargetWithInlineData, public ContextLifecycleObserver
     55     , private ContentDecryptionModuleSessionClient {
     56     REFCOUNTED_EVENT_TARGET(MediaKeySession);
     57 public:
     58     static PassRefPtr<MediaKeySession> create(ExecutionContext*, ContentDecryptionModule*, MediaKeys*);
     59     ~MediaKeySession();
     60 
     61     const String& keySystem() const { return m_keySystem; }
     62     String sessionId() const;
     63 
     64     void setError(MediaKeyError*);
     65     MediaKeyError* error() { return m_error.get(); }
     66 
     67     void generateKeyRequest(const String& mimeType, Uint8Array* initData);
     68     void update(Uint8Array* key, ExceptionState&);
     69     void close();
     70 
     71     void enqueueEvent(PassRefPtr<Event>);
     72 
     73     DEFINE_ATTRIBUTE_EVENT_LISTENER(webkitkeyadded);
     74     DEFINE_ATTRIBUTE_EVENT_LISTENER(webkitkeyerror);
     75     DEFINE_ATTRIBUTE_EVENT_LISTENER(webkitkeymessage);
     76 
     77     virtual const AtomicString& interfaceName() const OVERRIDE;
     78     virtual ExecutionContext* executionContext() const OVERRIDE;
     79 
     80 private:
     81     MediaKeySession(ExecutionContext*, ContentDecryptionModule*, MediaKeys*);
     82     void keyRequestTimerFired(Timer<MediaKeySession>*);
     83     void addKeyTimerFired(Timer<MediaKeySession>*);
     84 
     85     // ContentDecryptionModuleSessionClient
     86     virtual void keyAdded() OVERRIDE;
     87     virtual void keyError(MediaKeyErrorCode, unsigned long systemCode) OVERRIDE;
     88     virtual void keyMessage(const unsigned char* message, size_t messageLength, const KURL& destinationURL) OVERRIDE;
     89 
     90     String m_keySystem;
     91     RefPtr<MediaKeyError> m_error;
     92     OwnPtr<GenericEventQueue> m_asyncEventQueue;
     93     OwnPtr<ContentDecryptionModuleSession> m_session;
     94     // Used to remove the reference from the parent MediaKeys when close()'d.
     95     MediaKeys* m_keys;
     96 
     97     struct PendingKeyRequest {
     98         PendingKeyRequest(const String& mimeType, Uint8Array* initData) : mimeType(mimeType), initData(initData) { }
     99         String mimeType;
    100         RefPtr<Uint8Array> initData;
    101     };
    102     Deque<PendingKeyRequest> m_pendingKeyRequests;
    103     Timer<MediaKeySession> m_keyRequestTimer;
    104 
    105     Deque<RefPtr<Uint8Array> > m_pendingKeys;
    106     Timer<MediaKeySession> m_addKeyTimer;
    107 };
    108 
    109 }
    110 
    111 #endif // MediaKeySession_h
    112