Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/clients/api/Client.cpp
Line
Count
Source (jump to first uncovered line)
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
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#include "Client.h"
8
9
#include "ClientDOMUtil.h"
10
#include "mozilla/dom/ClientHandle.h"
11
#include "mozilla/dom/ClientIPCTypes.h"
12
#include "mozilla/dom/ClientManager.h"
13
#include "mozilla/dom/ClientState.h"
14
#include "mozilla/dom/DOMMozPromiseRequestHolder.h"
15
#include "mozilla/dom/Promise.h"
16
#include "mozilla/dom/WorkerPrivate.h"
17
#include "mozilla/dom/WorkerScope.h"
18
#include "nsIGlobalObject.h"
19
20
namespace mozilla {
21
namespace dom {
22
23
using mozilla::dom::ipc::StructuredCloneData;
24
25
NS_IMPL_CYCLE_COLLECTING_ADDREF(mozilla::dom::Client);
26
NS_IMPL_CYCLE_COLLECTING_RELEASE(mozilla::dom::Client);
27
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(mozilla::dom::Client, mGlobal);
28
29
0
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(mozilla::dom::Client)
30
0
  NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
31
0
  NS_INTERFACE_MAP_ENTRY(nsISupports)
32
0
NS_INTERFACE_MAP_END
33
34
void
35
Client::EnsureHandle()
36
0
{
37
0
  NS_ASSERT_OWNINGTHREAD(mozilla::dom::Client);
38
0
  if (!mHandle) {
39
0
    mHandle = ClientManager::CreateHandle(ClientInfo(mData->info()),
40
0
                                          mGlobal->EventTargetFor(TaskCategory::Other));
41
0
  }
42
0
}
43
44
Client::Client(nsIGlobalObject* aGlobal, const ClientInfoAndState& aData)
45
  : mGlobal(aGlobal)
46
  , mData(MakeUnique<ClientInfoAndState>(aData))
47
0
{
48
0
  MOZ_DIAGNOSTIC_ASSERT(mGlobal);
49
0
}
50
51
TimeStamp
52
Client::CreationTime() const
53
0
{
54
0
  return mData->info().creationTime();
55
0
}
56
57
TimeStamp
58
Client::LastFocusTime() const
59
0
{
60
0
  if (mData->info().type() != ClientType::Window) {
61
0
    return TimeStamp();
62
0
  }
63
0
  return mData->state().get_IPCClientWindowState().lastFocusTime();
64
0
}
65
66
nsContentUtils::StorageAccess
67
Client::GetStorageAccess() const
68
0
{
69
0
  ClientState state(ClientState::FromIPC(mData->state()));
70
0
  return state.GetStorageAccess();
71
0
}
72
73
JSObject*
74
Client::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
75
0
{
76
0
  if (mData->info().type() == ClientType::Window) {
77
0
    return WindowClient_Binding::Wrap(aCx, this, aGivenProto);
78
0
  }
79
0
  return Client_Binding::Wrap(aCx, this, aGivenProto);
80
0
}
81
82
nsIGlobalObject*
83
Client::GetParentObject() const
84
0
{
85
0
  return mGlobal;
86
0
}
87
88
void
89
Client::GetUrl(nsAString& aUrlOut) const
90
0
{
91
0
  CopyUTF8toUTF16(mData->info().url(), aUrlOut);
92
0
}
93
94
void
95
Client::GetId(nsAString& aIdOut) const
96
0
{
97
0
  char buf[NSID_LENGTH];
98
0
  mData->info().id().ToProvidedString(buf);
99
0
  NS_ConvertASCIItoUTF16 uuid(buf);
100
0
101
0
  // Remove {} and the null terminator
102
0
  aIdOut.Assign(Substring(uuid, 1, NSID_LENGTH - 3));
103
0
}
104
105
ClientType
106
Client::Type() const
107
0
{
108
0
  return mData->info().type();
109
0
}
110
111
FrameType
112
Client::GetFrameType() const
113
0
{
114
0
  return mData->info().frameType();
115
0
}
116
117
void
118
Client::PostMessage(JSContext* aCx, JS::Handle<JS::Value> aMessage,
119
                    const Sequence<JSObject*>& aTransferable,
120
                    ErrorResult& aRv)
121
0
{
122
0
  MOZ_ASSERT(!NS_IsMainThread());
123
0
  WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
124
0
  MOZ_DIAGNOSTIC_ASSERT(workerPrivate);
125
0
  MOZ_DIAGNOSTIC_ASSERT(workerPrivate->IsServiceWorker());
126
0
  workerPrivate->AssertIsOnWorkerThread();
127
0
128
0
  JS::Rooted<JS::Value> transferable(aCx, JS::UndefinedValue());
129
0
  aRv = nsContentUtils::CreateJSValueFromSequenceOfObject(aCx, aTransferable,
130
0
                                                          &transferable);
131
0
  if (aRv.Failed()) {
132
0
    return;
133
0
  }
134
0
135
0
  StructuredCloneData data;
136
0
  data.Write(aCx, aMessage, transferable, aRv);
137
0
  if (aRv.Failed()) {
138
0
    return;
139
0
  }
140
0
141
0
  EnsureHandle();
142
0
  mHandle->PostMessage(data, workerPrivate->GetServiceWorkerDescriptor());
143
0
}
144
145
VisibilityState
146
Client::GetVisibilityState() const
147
0
{
148
0
  return mData->state().get_IPCClientWindowState().visibilityState();
149
0
}
150
151
bool
152
Client::Focused() const
153
0
{
154
0
  return mData->state().get_IPCClientWindowState().focused();
155
0
}
156
157
already_AddRefed<Promise>
158
Client::Focus(ErrorResult& aRv)
159
0
{
160
0
  MOZ_ASSERT(!NS_IsMainThread());
161
0
  WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
162
0
  MOZ_DIAGNOSTIC_ASSERT(workerPrivate);
163
0
  MOZ_DIAGNOSTIC_ASSERT(workerPrivate->IsServiceWorker());
164
0
  workerPrivate->AssertIsOnWorkerThread();
165
0
166
0
  RefPtr<Promise> outerPromise = Promise::Create(mGlobal, aRv);
167
0
  if (aRv.Failed()) {
168
0
    return outerPromise.forget();
169
0
  }
170
0
171
0
  if (!workerPrivate->GlobalScope()->WindowInteractionAllowed()) {
172
0
    outerPromise->MaybeReject(NS_ERROR_DOM_INVALID_ACCESS_ERR);
173
0
    return outerPromise.forget();
174
0
  }
175
0
176
0
  EnsureHandle();
177
0
178
0
  IPCClientInfo ipcClientInfo(mData->info());
179
0
  auto holder = MakeRefPtr<DOMMozPromiseRequestHolder<ClientStatePromise>>(mGlobal);
180
0
181
0
  mHandle->Focus()->Then(mGlobal->EventTargetFor(TaskCategory::Other), __func__,
182
0
    [ipcClientInfo, holder, outerPromise] (const ClientState& aResult) {
183
0
      holder->Complete();
184
0
      NS_ENSURE_TRUE_VOID(holder->GetParentObject());
185
0
      RefPtr<Client> newClient = new Client(holder->GetParentObject(),
186
0
                                            ClientInfoAndState(ipcClientInfo,
187
0
                                                               aResult.ToIPC()));
188
0
      outerPromise->MaybeResolve(newClient);
189
0
    }, [holder, outerPromise] (nsresult aResult) {
190
0
      holder->Complete();
191
0
      outerPromise->MaybeReject(aResult);
192
0
    })->Track(*holder);
193
0
194
0
  return outerPromise.forget();
195
0
}
196
197
already_AddRefed<Promise>
198
Client::Navigate(const nsAString& aURL, ErrorResult& aRv)
199
0
{
200
0
  MOZ_ASSERT(!NS_IsMainThread());
201
0
  WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
202
0
  MOZ_DIAGNOSTIC_ASSERT(workerPrivate);
203
0
  MOZ_DIAGNOSTIC_ASSERT(workerPrivate->IsServiceWorker());
204
0
  workerPrivate->AssertIsOnWorkerThread();
205
0
206
0
  RefPtr<Promise> outerPromise = Promise::Create(mGlobal, aRv);
207
0
  if (aRv.Failed()) {
208
0
    return outerPromise.forget();
209
0
  }
210
0
211
0
  ClientNavigateArgs args(mData->info(), NS_ConvertUTF16toUTF8(aURL),
212
0
                          workerPrivate->GetLocationInfo().mHref);
213
0
  RefPtr<Client> self = this;
214
0
215
0
  StartClientManagerOp(&ClientManager::Navigate, args, mGlobal,
216
0
    [self, outerPromise] (const ClientOpResult& aResult) {
217
0
      if (aResult.type() != ClientOpResult::TClientInfoAndState) {
218
0
        outerPromise->MaybeResolve(JS::NullHandleValue);
219
0
        return;
220
0
      }
221
0
      RefPtr<Client> newClient =
222
0
        new Client(self->mGlobal, aResult.get_ClientInfoAndState());
223
0
      outerPromise->MaybeResolve(newClient);
224
0
    }, [self, outerPromise] (nsresult aResult) {
225
0
      // TODO: Improve this error in bug 1412856.  Ideally we should throw
226
0
      //       the TypeError in the child process and pass it back to here.
227
0
      outerPromise->MaybeReject(NS_ERROR_TYPE_ERR);
228
0
    });
229
0
230
0
  return outerPromise.forget();
231
0
}
232
233
} // namespace dom
234
} // namespace mozilla