Home | History | Annotate | Download | only in crypto
      1 // Copyright 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef CONTENT_RENDERER_MEDIA_CRYPTO_PROXY_DECRYPTOR_H_
      6 #define CONTENT_RENDERER_MEDIA_CRYPTO_PROXY_DECRYPTOR_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/basictypes.h"
     12 #include "base/memory/scoped_ptr.h"
     13 #include "base/memory/weak_ptr.h"
     14 #include "base/synchronization/lock.h"
     15 #include "media/base/decryptor.h"
     16 #include "media/base/media_keys.h"
     17 
     18 namespace WebKit {
     19 #if defined(ENABLE_PEPPER_CDMS)
     20 class WebFrame;
     21 class WebMediaPlayerClient;
     22 #endif  // defined(ENABLE_PEPPER_CDMS)
     23 }
     24 
     25 namespace content {
     26 
     27 class WebMediaPlayerProxyAndroid;
     28 
     29 // ProxyDecryptor is for EME v0.1b only. It should not be used for the WD API.
     30 // A decryptor proxy that creates a real decryptor object on demand and
     31 // forwards decryptor calls to it.
     32 // TODO(xhwang): Currently we don't support run-time switching among decryptor
     33 // objects. Fix this when needed.
     34 // TODO(xhwang): The ProxyDecryptor is not a Decryptor. Find a better name!
     35 class ProxyDecryptor : public media::MediaKeys {
     36  public:
     37   ProxyDecryptor(
     38 #if defined(ENABLE_PEPPER_CDMS)
     39       WebKit::WebMediaPlayerClient* web_media_player_client,
     40       WebKit::WebFrame* web_frame,
     41 #elif defined(OS_ANDROID)
     42       WebMediaPlayerProxyAndroid* proxy,
     43       int media_keys_id,
     44 #endif  // defined(ENABLE_PEPPER_CDMS)
     45       const media::KeyAddedCB& key_added_cb,
     46       const media::KeyErrorCB& key_error_cb,
     47       const media::KeyMessageCB& key_message_cb);
     48   virtual ~ProxyDecryptor();
     49 
     50   // Only call this once.
     51   bool InitializeCDM(const std::string& key_system);
     52 
     53   // Requests the ProxyDecryptor to notify the decryptor when it's ready through
     54   // the |decryptor_ready_cb| provided.
     55   // If |decryptor_ready_cb| is null, the existing callback will be fired with
     56   // NULL immediately and reset.
     57   void SetDecryptorReadyCB(const media::DecryptorReadyCB& decryptor_ready_cb);
     58 
     59   // MediaKeys implementation.
     60   // May only be called after InitializeCDM() succeeds.
     61   virtual bool GenerateKeyRequest(const std::string& type,
     62                                   const uint8* init_data,
     63                                   int init_data_length) OVERRIDE;
     64   virtual void AddKey(const uint8* key, int key_length,
     65                       const uint8* init_data, int init_data_length,
     66                       const std::string& session_id) OVERRIDE;
     67   virtual void CancelKeyRequest(const std::string& session_id) OVERRIDE;
     68 
     69  private:
     70   // Helper function to create MediaKeys to handle the given |key_system|.
     71   scoped_ptr<media::MediaKeys> CreateMediaKeys(const std::string& key_system);
     72 
     73   // Callbacks for firing key events.
     74   void KeyAdded(const std::string& session_id);
     75   void KeyError(const std::string& session_id,
     76                 media::MediaKeys::KeyError error_code,
     77                 int system_code);
     78   void KeyMessage(const std::string& session_id,
     79                   const std::vector<uint8>& message,
     80                   const std::string& default_url);
     81 
     82   base::WeakPtrFactory<ProxyDecryptor> weak_ptr_factory_;
     83 
     84 #if defined(ENABLE_PEPPER_CDMS)
     85   // Callback for cleaning up a Pepper-based CDM.
     86   void DestroyHelperPlugin();
     87 
     88   // Needed to create the PpapiDecryptor.
     89   WebKit::WebMediaPlayerClient* web_media_player_client_;
     90   WebKit::WebFrame* web_frame_;
     91 #elif defined(OS_ANDROID)
     92   WebMediaPlayerProxyAndroid* proxy_;
     93   int media_keys_id_;
     94 #endif  // defined(ENABLE_PEPPER_CDMS)
     95 
     96   // The real MediaKeys that manages key operations for the ProxyDecryptor.
     97   // This pointer is protected by the |lock_|.
     98   scoped_ptr<media::MediaKeys> media_keys_;
     99 
    100   // Callbacks for firing key events.
    101   media::KeyAddedCB key_added_cb_;
    102   media::KeyErrorCB key_error_cb_;
    103   media::KeyMessageCB key_message_cb_;
    104 
    105   // Protects the |decryptor_|. Note that |decryptor_| itself should be thread
    106   // safe as per the Decryptor interface.
    107   base::Lock lock_;
    108 
    109   media::DecryptorReadyCB decryptor_ready_cb_;
    110 
    111   DISALLOW_COPY_AND_ASSIGN(ProxyDecryptor);
    112 };
    113 
    114 }  // namespace content
    115 
    116 #endif  // CONTENT_RENDERER_MEDIA_CRYPTO_PROXY_DECRYPTOR_H_
    117