Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / content / renderer / media / webcontentdecryptionmodulesession_impl.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_WEBCONTENTDECRYPTIONMODULESESSION_IMPL_H_
6 #define CONTENT_RENDERER_MEDIA_WEBCONTENTDECRYPTIONMODULESESSION_IMPL_H_
7
8 #include <map>
9 #include <string>
10 #include <vector>
11
12 #include "base/basictypes.h"
13 #include "base/callback.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/weak_ptr.h"
16 #include "media/base/media_keys.h"
17 #include "third_party/WebKit/public/platform/WebContentDecryptionModuleSession.h"
18 #include "third_party/WebKit/public/platform/WebString.h"
19
20 namespace media {
21 class MediaKeys;
22 }
23
24 namespace content {
25 class CdmSessionAdapter;
26
27 class WebContentDecryptionModuleSessionImpl
28     : public blink::WebContentDecryptionModuleSession {
29  public:
30   WebContentDecryptionModuleSessionImpl(
31       const scoped_refptr<CdmSessionAdapter>& adapter);
32   virtual ~WebContentDecryptionModuleSessionImpl();
33
34   // blink::WebContentDecryptionModuleSession implementation.
35   virtual void setClientInterface(Client* client);
36   virtual blink::WebString sessionId() const;
37   // TODO(jrummell): Remove the next 3 methods once blink updated.
38   virtual void initializeNewSession(const blink::WebString& mime_type,
39                                     const uint8* init_data,
40                                     size_t init_data_length);
41   virtual void update(const uint8* response, size_t response_length);
42   virtual void release();
43   virtual void initializeNewSession(
44       const blink::WebString& init_data_type,
45       const uint8* init_data,
46       size_t init_data_length,
47       const blink::WebString& session_type,
48       blink::WebContentDecryptionModuleResult result);
49   virtual void update(const uint8* response,
50                       size_t response_length,
51                       blink::WebContentDecryptionModuleResult result);
52   virtual void release(blink::WebContentDecryptionModuleResult result);
53
54   // Callbacks.
55   void OnSessionMessage(const std::vector<uint8>& message,
56                         const GURL& destination_url);
57   void OnSessionReady();
58   void OnSessionClosed();
59   void OnSessionError(media::MediaKeys::Exception exception_code,
60                       uint32 system_code,
61                       const std::string& error_message);
62
63  private:
64   typedef std::map<uint32, blink::WebContentDecryptionModuleResult> ResultMap;
65
66   // These function are used as callbacks when CdmPromise resolves/rejects.
67   // |result_index| = kReservedIndex means that there is no
68   // WebContentDecryptionModuleResult.
69   void SessionCreated(uint32 result_index, const std::string& web_session_id);
70   void SessionUpdatedOrReleased(uint32 result_index);
71   void SessionError(uint32 result_index,
72                     media::MediaKeys::Exception exception_code,
73                     uint32 system_code,
74                     const std::string& error_message);
75
76   // As initializeNewSession(), update(), and release() get passed a
77   // WebContentDecryptionModuleResult, keep track of them since this class owns
78   // it and needs to keep them around until completed. Returns the index used
79   // to locate the WebContentDecryptionModuleResult when the operation is
80   // complete.
81   uint32 AddResult(blink::WebContentDecryptionModuleResult result);
82
83   scoped_refptr<CdmSessionAdapter> adapter_;
84
85   // Non-owned pointer.
86   Client* client_;
87
88   // Web session ID is the app visible ID for this session generated by the CDM.
89   // This value is not set until the CDM resolves the initializeNewSession()
90   // promise.
91   std::string web_session_id_;
92
93   // Don't pass more than 1 close() event to blink::
94   // TODO(jrummell): Remove this once blink tests handle close() promise and
95   // closed() event.
96   bool is_closed_;
97
98   // Keep track of all the outstanding WebContentDecryptionModuleResult objects.
99   uint32 next_available_result_index_;
100   ResultMap outstanding_results_;
101
102   // Since promises will live until they are fired, use a weak reference when
103   // creating a promise in case this class disappears before the promise
104   // actually fires.
105   base::WeakPtrFactory<WebContentDecryptionModuleSessionImpl> weak_ptr_factory_;
106
107   DISALLOW_COPY_AND_ASSIGN(WebContentDecryptionModuleSessionImpl);
108 };
109
110 }  // namespace content
111
112 #endif  // CONTENT_RENDERER_MEDIA_WEBCONTENTDECRYPTIONMODULESESSION_IMPL_H_