- add sources.
[platform/framework/web/crosswalk.git] / src / content / renderer / media / crypto / proxy_decryptor.h
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 class GURL;
19
20 namespace WebKit {
21 #if defined(ENABLE_PEPPER_CDMS)
22 class WebFrame;
23 class WebMediaPlayerClient;
24 #endif  // defined(ENABLE_PEPPER_CDMS)
25 }
26
27 namespace content {
28
29 #if defined(OS_ANDROID)
30 class RendererMediaPlayerManager;
31 #endif  // defined(OS_ANDROID)
32
33 // ProxyDecryptor is for EME v0.1b only. It should not be used for the WD API.
34 // A decryptor proxy that creates a real decryptor object on demand and
35 // forwards decryptor calls to it.
36 // TODO(xhwang): Currently we don't support run-time switching among decryptor
37 // objects. Fix this when needed.
38 // TODO(xhwang): The ProxyDecryptor is not a Decryptor. Find a better name!
39 class ProxyDecryptor : public media::MediaKeys {
40  public:
41   ProxyDecryptor(
42 #if defined(ENABLE_PEPPER_CDMS)
43       WebKit::WebMediaPlayerClient* web_media_player_client,
44       WebKit::WebFrame* web_frame,
45 #elif defined(OS_ANDROID)
46       RendererMediaPlayerManager* manager,
47       int media_keys_id,
48 #endif  // defined(ENABLE_PEPPER_CDMS)
49       const media::KeyAddedCB& key_added_cb,
50       const media::KeyErrorCB& key_error_cb,
51       const media::KeyMessageCB& key_message_cb);
52   virtual ~ProxyDecryptor();
53
54   // Only call this once.
55   bool InitializeCDM(const std::string& key_system, const GURL& frame_url);
56
57   // Requests the ProxyDecryptor to notify the decryptor when it's ready through
58   // the |decryptor_ready_cb| provided.
59   // If |decryptor_ready_cb| is null, the existing callback will be fired with
60   // NULL immediately and reset.
61   void SetDecryptorReadyCB(const media::DecryptorReadyCB& decryptor_ready_cb);
62
63   // MediaKeys implementation.
64   // May only be called after InitializeCDM() succeeds.
65   virtual bool GenerateKeyRequest(const std::string& type,
66                                   const uint8* init_data,
67                                   int init_data_length) OVERRIDE;
68   virtual void AddKey(const uint8* key, int key_length,
69                       const uint8* init_data, int init_data_length,
70                       const std::string& session_id) OVERRIDE;
71   virtual void CancelKeyRequest(const std::string& session_id) OVERRIDE;
72
73  private:
74   // Helper function to create MediaKeys to handle the given |key_system|.
75   scoped_ptr<media::MediaKeys> CreateMediaKeys(const std::string& key_system,
76                                                const GURL& frame_url);
77
78   // Callbacks for firing key events.
79   void KeyAdded(const std::string& session_id);
80   void KeyError(const std::string& session_id,
81                 media::MediaKeys::KeyError error_code,
82                 int system_code);
83   void KeyMessage(const std::string& session_id,
84                   const std::vector<uint8>& message,
85                   const std::string& default_url);
86
87   base::WeakPtrFactory<ProxyDecryptor> weak_ptr_factory_;
88
89 #if defined(ENABLE_PEPPER_CDMS)
90   // Callback for cleaning up a Pepper-based CDM.
91   void DestroyHelperPlugin();
92
93   // Needed to create the PpapiDecryptor.
94   WebKit::WebMediaPlayerClient* web_media_player_client_;
95   WebKit::WebFrame* web_frame_;
96 #elif defined(OS_ANDROID)
97   RendererMediaPlayerManager* manager_;
98   int media_keys_id_;
99 #endif  // defined(ENABLE_PEPPER_CDMS)
100
101   // The real MediaKeys that manages key operations for the ProxyDecryptor.
102   // This pointer is protected by the |lock_|.
103   scoped_ptr<media::MediaKeys> media_keys_;
104
105   // Callbacks for firing key events.
106   media::KeyAddedCB key_added_cb_;
107   media::KeyErrorCB key_error_cb_;
108   media::KeyMessageCB key_message_cb_;
109
110   // Protects the |decryptor_|. Note that |decryptor_| itself should be thread
111   // safe as per the Decryptor interface.
112   base::Lock lock_;
113
114   media::DecryptorReadyCB decryptor_ready_cb_;
115
116   DISALLOW_COPY_AND_ASSIGN(ProxyDecryptor);
117 };
118
119 }  // namespace content
120
121 #endif  // CONTENT_RENDERER_MEDIA_CRYPTO_PROXY_DECRYPTOR_H_