summaryrefslogtreecommitdiff
path: root/chromium/media/blink/webcontentdecryptionmodulesession_impl.h
blob: 1b8d1b319032e63daf9feba5ce7125964e6ef375 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MEDIA_BLINK_WEBCONTENTDECRYPTIONMODULESESSION_IMPL_H_
#define MEDIA_BLINK_WEBCONTENTDECRYPTIONMODULESESSION_IMPL_H_

#include <stddef.h>
#include <stdint.h>

#include <string>
#include <vector>

#include "base/callback.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "base/threading/thread_checker.h"
#include "media/base/content_decryption_module.h"
#include "media/blink/new_session_cdm_result_promise.h"
#include "third_party/WebKit/public/platform/WebContentDecryptionModuleSession.h"
#include "third_party/WebKit/public/platform/WebString.h"

namespace media {

class CdmSessionAdapter;

class WebContentDecryptionModuleSessionImpl
    : public blink::WebContentDecryptionModuleSession {
 public:
  WebContentDecryptionModuleSessionImpl(
      const scoped_refptr<CdmSessionAdapter>& adapter);
  ~WebContentDecryptionModuleSessionImpl() override;

  // blink::WebContentDecryptionModuleSession implementation.
  void setClientInterface(Client* client) override;
  blink::WebString sessionId() const override;

  void initializeNewSession(
      blink::WebEncryptedMediaInitDataType init_data_type,
      const unsigned char* initData,
      size_t initDataLength,
      blink::WebEncryptedMediaSessionType session_type,
      blink::WebContentDecryptionModuleResult result) override;
  void load(const blink::WebString& session_id,
            blink::WebContentDecryptionModuleResult result) override;
  void update(const uint8_t* response,
              size_t response_length,
              blink::WebContentDecryptionModuleResult result) override;
  void close(blink::WebContentDecryptionModuleResult result) override;
  void remove(blink::WebContentDecryptionModuleResult result) override;

  // Callbacks.
  void OnSessionMessage(ContentDecryptionModule::MessageType message_type,
                        const std::vector<uint8_t>& message);
  void OnSessionKeysChange(bool has_additional_usable_key,
                           CdmKeysInfo keys_info);
  void OnSessionExpirationUpdate(base::Time new_expiry_time);
  void OnSessionClosed();

 private:
  // Called when a new session is created or loaded. |status| is set as
  // appropriate, depending on whether the session already exists or not.
  void OnSessionInitialized(const std::string& session_id,
                            SessionInitStatus* status);

  scoped_refptr<CdmSessionAdapter> adapter_;

  // Non-owned pointer.
  Client* client_;

  // Session ID is the app visible ID for this session generated by the CDM.
  // This value is not set until the CDM resolves the initializeNewSession()
  // promise.
  std::string session_id_;

  // Keep track of whether the session has been closed or not. The session
  // may be closed as a result of an application calling close(), or the CDM
  // may close the session at any point.
  // https://w3c.github.io/encrypted-media/#session-closed
  // |has_close_been_called_| is used to keep track of whether close() has
  // been called or not. |is_closed_| is used to keep track of whether the
  // close event has been received or not.
  bool has_close_been_called_;
  bool is_closed_;

  base::ThreadChecker thread_checker_;
  // Since promises will live until they are fired, use a weak reference when
  // creating a promise in case this class disappears before the promise
  // actually fires.
  base::WeakPtrFactory<WebContentDecryptionModuleSessionImpl> weak_ptr_factory_;

  DISALLOW_COPY_AND_ASSIGN(WebContentDecryptionModuleSessionImpl);
};

}  // namespace media

#endif  // MEDIA_BLINK_WEBCONTENTDECRYPTIONMODULESESSION_IMPL_H_