browser_cdm_manager.h revision f8ee788a64d60abd8f2d742a5fdedde054ecd910
1// Copyright 2014 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_BROWSER_MEDIA_CDM_BROWSER_CDM_MANAGER_H_
6#define CONTENT_BROWSER_MEDIA_CDM_BROWSER_CDM_MANAGER_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/containers/scoped_ptr_hash_map.h"
15#include "base/memory/scoped_ptr.h"
16#include "content/common/content_export.h"
17#include "content/common/media/cdm_messages_enums.h"
18#include "ipc/ipc_message.h"
19// TODO(xhwang): Drop this when KeyError is moved to a common header.
20#include "media/base/media_keys.h"
21#include "url/gurl.h"
22
23namespace media {
24class BrowserCdm;
25}
26
27namespace content {
28
29class RenderFrameHost;
30class WebContents;
31
32// This class manages all CDM objects. It receives control operations from the
33// the render process, and forwards them to corresponding CDM object. Callbacks
34// from CDM objects are converted to IPCs and then sent to the render process.
35class CONTENT_EXPORT BrowserCdmManager {
36 public:
37  // Creates a new BrowserCdmManager for |rfh|.
38  static BrowserCdmManager* Create(RenderFrameHost* rfh);
39
40  ~BrowserCdmManager();
41
42  media::BrowserCdm* GetCdm(int cdm_id);
43
44  // CDM callbacks.
45  void OnSessionCreated(int cdm_id,
46                        uint32 session_id,
47                        const std::string& web_session_id);
48  void OnSessionMessage(int cdm_id,
49                        uint32 session_id,
50                        const std::vector<uint8>& message,
51                        const GURL& destination_url);
52  void OnSessionReady(int cdm_id, uint32 session_id);
53  void OnSessionClosed(int cdm_id, uint32 session_id);
54  void OnSessionError(int cdm_id,
55                      uint32 session_id,
56                      media::MediaKeys::KeyError error_code,
57                      uint32 system_code);
58
59  // Message handlers.
60  void OnInitializeCdm(int cdm_id,
61                       const std::string& key_system,
62                       const GURL& frame_url);
63  void OnCreateSession(int cdm_id,
64                       uint32 session_id,
65                       CdmHostMsg_CreateSession_ContentType content_type,
66                       const std::vector<uint8>& init_data);
67  void OnUpdateSession(int cdm_id,
68                       uint32 session_id,
69                       const std::vector<uint8>& response);
70  void OnReleaseSession(int cdm_id, uint32 session_id);
71  void OnSetCdm(int player_id, int cdm_id);
72  void OnDestroyCdm(int cdm_id);
73
74 private:
75  // Clients must use Create() or subclass constructor.
76  explicit BrowserCdmManager(RenderFrameHost* render_frame_host);
77
78  // Cancels all pending session creations associated with |cdm_id|.
79  void CancelAllPendingSessionCreations(int cdm_id);
80
81  // Adds a new CDM identified by |cdm_id| for the given |key_system| and
82  // |security_origin|.
83  void AddCdm(int cdm_id,
84              const std::string& key_system,
85              const GURL& security_origin);
86
87  // Removes the CDM with the specified id.
88  void RemoveCdm(int cdm_id);
89
90  int RoutingID();
91
92  // Helper function to send messages to RenderFrameObserver.
93  bool Send(IPC::Message* msg);
94
95  // If |permitted| is false, it does nothing but send
96  // |CdmMsg_SessionError| IPC message.
97  // The primary use case is infobar permission callback, i.e., when infobar
98  // can decide user's intention either from interacting with the actual info
99  // bar or from the saved preference.
100  void CreateSessionIfPermitted(int cdm_id,
101                                uint32 session_id,
102                                const std::string& content_type,
103                                const std::vector<uint8>& init_data,
104                                bool permitted);
105
106  RenderFrameHost* const render_frame_host_;
107
108  WebContents* const web_contents_;
109
110  // A map from CDM IDs to managed CDMs.
111  typedef base::ScopedPtrHashMap<int, media::BrowserCdm> CdmMap;
112  CdmMap cdm_map_;
113
114  // Map from CDM ID to CDM's security origin.
115  std::map<int, GURL> cdm_security_origin_map_;
116
117  // Map from CDM ID to a callback to cancel the permission request.
118  std::map<int, base::Closure> cdm_cancel_permision_map_;
119
120  // NOTE: Weak pointers must be invalidated before all other member variables.
121  base::WeakPtrFactory<BrowserCdmManager> weak_ptr_factory_;
122
123  DISALLOW_COPY_AND_ASSIGN(BrowserCdmManager);
124};
125
126}  // namespace content
127
128#endif  // CONTENT_BROWSER_MEDIA_CDM_BROWSER_CDM_MANAGER_H_
129