Update To 11.40.268.0
[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/containers/hash_tables.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "media/base/decryptor.h"
16 #include "media/base/media_keys.h"
17
18 class GURL;
19
20 namespace media {
21 class CdmFactory;
22 }
23
24 namespace content {
25
26 // ProxyDecryptor is for EME v0.1b only. It should not be used for the WD API.
27 // A decryptor proxy that creates a real decryptor object on demand and
28 // forwards decryptor calls to it.
29 //
30 // TODO(xhwang): Currently we don't support run-time switching among decryptor
31 // objects. Fix this when needed.
32 // TODO(xhwang): The ProxyDecryptor is not a Decryptor. Find a better name!
33 class ProxyDecryptor {
34  public:
35   // These are similar to the callbacks in media_keys.h, but pass back the
36   // web session ID rather than the internal session ID.
37   typedef base::Callback<void(const std::string& session_id)> KeyAddedCB;
38   typedef base::Callback<void(const std::string& session_id,
39                               media::MediaKeys::KeyError error_code,
40                               uint32 system_code)> KeyErrorCB;
41   typedef base::Callback<void(const std::string& session_id,
42                               const std::vector<uint8>& message,
43                               const GURL& destination_url)> KeyMessageCB;
44
45   ProxyDecryptor(const KeyAddedCB& key_added_cb,
46                  const KeyErrorCB& key_error_cb,
47                  const KeyMessageCB& key_message_cb);
48   virtual ~ProxyDecryptor();
49
50   // Returns the Decryptor associated with this object. May be NULL if no
51   // Decryptor is associated.
52   media::Decryptor* GetDecryptor();
53
54 #if defined(ENABLE_BROWSER_CDMS)
55   // Returns the CDM ID associated with this object. May be kInvalidCdmId if no
56   // CDM ID is associated, such as when Clear Key is used.
57   int GetCdmId();
58 #endif
59
60   // Only call this once.
61   bool InitializeCDM(media::CdmFactory* cdm_factory,
62                      const std::string& key_system,
63                      const GURL& security_origin);
64
65   // May only be called after InitializeCDM() succeeds.
66   bool GenerateKeyRequest(const std::string& init_data_type,
67                           const uint8* init_data,
68                           int init_data_length);
69   void AddKey(const uint8* key, int key_length,
70               const uint8* init_data, int init_data_length,
71               const std::string& session_id);
72   void CancelKeyRequest(const std::string& session_id);
73
74  private:
75   // Helper function to create MediaKeys to handle the given |key_system|.
76   scoped_ptr<media::MediaKeys> CreateMediaKeys(
77       media::CdmFactory* cdm_factory,
78       const std::string& key_system,
79       const GURL& security_origin);
80
81   // Callbacks for firing session events.
82   void OnSessionMessage(const std::string& web_session_id,
83                         const std::vector<uint8>& message,
84                         const GURL& default_url);
85   void OnSessionKeysChange(const std::string& web_session_id,
86                            bool has_additional_usable_key);
87   void OnSessionExpirationUpdate(const std::string& web_session_id,
88                                  const base::Time& new_expiry_time);
89   void OnSessionReady(const std::string& web_session_id);
90   void OnSessionClosed(const std::string& web_session_id);
91   void OnSessionError(const std::string& web_session_id,
92                       media::MediaKeys::Exception exception_code,
93                       uint32 system_code,
94                       const std::string& error_message);
95
96   enum SessionCreationType {
97     TemporarySession,
98     PersistentSession,
99     LoadSession
100   };
101
102   // Called when a session is actually created or loaded.
103   void SetSessionId(SessionCreationType session_type,
104                     const std::string& web_session_id);
105
106   // The real MediaKeys that manages key operations for the ProxyDecryptor.
107   scoped_ptr<media::MediaKeys> media_keys_;
108
109   // Callbacks for firing key events.
110   KeyAddedCB key_added_cb_;
111   KeyErrorCB key_error_cb_;
112   KeyMessageCB key_message_cb_;
113
114   // Keep track of both persistent and non-persistent sessions.
115   base::hash_map<std::string, bool> active_sessions_;
116
117   bool is_clear_key_;
118
119 #if defined(ENABLE_BROWSER_CDMS)
120   int cdm_id_;
121 #endif
122
123   // NOTE: Weak pointers must be invalidated before all other member variables.
124   base::WeakPtrFactory<ProxyDecryptor> weak_ptr_factory_;
125
126   DISALLOW_COPY_AND_ASSIGN(ProxyDecryptor);
127 };
128
129 }  // namespace content
130
131 #endif  // CONTENT_RENDERER_MEDIA_CRYPTO_PROXY_DECRYPTOR_H_