Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 : /* This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 : * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #include "mozilla/dom/MediaKeyMessageEvent.h"
8 : #include "mozilla/dom/MediaKeyMessageEventBinding.h"
9 : #include "js/GCAPI.h"
10 : #include "jsfriendapi.h"
11 : #include "mozilla/dom/Nullable.h"
12 : #include "mozilla/dom/PrimitiveConversions.h"
13 : #include "mozilla/HoldDropJSObjects.h"
14 : #include "mozilla/dom/TypedArray.h"
15 : #include "nsContentUtils.h"
16 : #include "mozilla/dom/MediaKeys.h"
17 :
18 : namespace mozilla {
19 : namespace dom {
20 :
21 : NS_IMPL_CYCLE_COLLECTION_CLASS(MediaKeyMessageEvent)
22 :
23 0 : NS_IMPL_ADDREF_INHERITED(MediaKeyMessageEvent, Event)
24 0 : NS_IMPL_RELEASE_INHERITED(MediaKeyMessageEvent, Event)
25 :
26 0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(MediaKeyMessageEvent, Event)
27 0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
28 :
29 0 : NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(MediaKeyMessageEvent, Event)
30 0 : NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mMessage)
31 0 : NS_IMPL_CYCLE_COLLECTION_TRACE_END
32 :
33 0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(MediaKeyMessageEvent, Event)
34 0 : tmp->mMessage = nullptr;
35 0 : mozilla::DropJSObjects(this);
36 0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_END
37 :
38 0 : NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(MediaKeyMessageEvent)
39 0 : NS_INTERFACE_MAP_END_INHERITING(Event)
40 :
41 0 : MediaKeyMessageEvent::MediaKeyMessageEvent(EventTarget* aOwner)
42 0 : : Event(aOwner, nullptr, nullptr)
43 : {
44 0 : mozilla::HoldJSObjects(this);
45 0 : }
46 :
47 0 : MediaKeyMessageEvent::~MediaKeyMessageEvent()
48 : {
49 0 : mMessage = nullptr;
50 0 : mozilla::DropJSObjects(this);
51 0 : }
52 :
53 : MediaKeyMessageEvent*
54 0 : MediaKeyMessageEvent::AsMediaKeyMessageEvent()
55 : {
56 0 : return this;
57 : }
58 :
59 : JSObject*
60 0 : MediaKeyMessageEvent::WrapObjectInternal(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
61 : {
62 0 : return MediaKeyMessageEventBinding::Wrap(aCx, this, aGivenProto);
63 : }
64 :
65 : already_AddRefed<MediaKeyMessageEvent>
66 0 : MediaKeyMessageEvent::Constructor(EventTarget* aOwner,
67 : MediaKeyMessageType aMessageType,
68 : const nsTArray<uint8_t>& aMessage)
69 : {
70 0 : RefPtr<MediaKeyMessageEvent> e = new MediaKeyMessageEvent(aOwner);
71 0 : e->InitEvent(NS_LITERAL_STRING("message"), false, false);
72 0 : e->mMessageType = aMessageType;
73 0 : e->mRawMessage = aMessage;
74 0 : e->SetTrusted(true);
75 0 : return e.forget();
76 : }
77 :
78 : already_AddRefed<MediaKeyMessageEvent>
79 0 : MediaKeyMessageEvent::Constructor(const GlobalObject& aGlobal,
80 : const nsAString& aType,
81 : const MediaKeyMessageEventInit& aEventInitDict,
82 : ErrorResult& aRv)
83 : {
84 0 : nsCOMPtr<EventTarget> owner = do_QueryInterface(aGlobal.GetAsSupports());
85 0 : RefPtr<MediaKeyMessageEvent> e = new MediaKeyMessageEvent(owner);
86 0 : bool trusted = e->Init(owner);
87 0 : e->InitEvent(aType, aEventInitDict.mBubbles, aEventInitDict.mCancelable);
88 0 : aEventInitDict.mMessage.ComputeLengthAndData();
89 0 : e->mMessage = ArrayBuffer::Create(aGlobal.Context(),
90 : aEventInitDict.mMessage.Length(),
91 0 : aEventInitDict.mMessage.Data());
92 0 : if (!e->mMessage) {
93 0 : aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
94 0 : return nullptr;
95 : }
96 0 : e->mMessageType = aEventInitDict.mMessageType;
97 0 : e->SetTrusted(trusted);
98 0 : e->SetComposed(aEventInitDict.mComposed);
99 0 : return e.forget();
100 : }
101 :
102 : void
103 0 : MediaKeyMessageEvent::GetMessage(JSContext* cx,
104 : JS::MutableHandle<JSObject*> aMessage,
105 : ErrorResult& aRv)
106 : {
107 0 : if (!mMessage) {
108 0 : mMessage = ArrayBuffer::Create(cx,
109 : this,
110 0 : mRawMessage.Length(),
111 0 : mRawMessage.Elements());
112 0 : if (!mMessage) {
113 0 : aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
114 0 : return;
115 : }
116 0 : mRawMessage.Clear();
117 : }
118 0 : aMessage.set(mMessage);
119 : }
120 :
121 : } // namespace dom
122 : } // namespace mozilla
|