-
Notifications
You must be signed in to change notification settings - Fork 16
/
EmbedLiteApp.cpp
622 lines (541 loc) · 16.2 KB
/
EmbedLiteApp.cpp
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "EmbedLog.h"
#include "EmbedLiteApp.h"
#include "nsISupports.h"
#include "nsIFile.h"
#include "base/at_exit.h"
#include "mozilla/unused.h"
#include "base/message_loop.h" // for MessageLoop
#include "mozilla/embedlite/EmbedLiteAPI.h"
#include "mozilla/layers/CompositorParent.h"
#include "EmbedLiteUILoop.h"
#include "EmbedLiteSubThread.h"
#include "GeckoLoader.h"
#include "EmbedLiteAppThreadParent.h"
#include "EmbedLiteAppThreadChild.h"
#include "EmbedLiteView.h"
#include "EmbedLiteWindow.h"
#include "nsXULAppAPI.h"
#include "EmbedLiteMessagePump.h"
#include "EmbedLiteCompositorParent.h"
#include "EmbedLiteAppProcessParent.h"
namespace mozilla {
namespace startup {
extern bool sIsEmbedlite;
extern GeckoProcessType sChildProcessType;
}
namespace embedlite {
EmbedLiteApp* EmbedLiteApp::sSingleton = nullptr;
static nsTArray<nsCString> sComponentDirs;
EmbedLiteApp*
EmbedLiteApp::GetInstance()
{
if (!sSingleton) {
sSingleton = new EmbedLiteApp();
NS_ASSERTION(sSingleton, "not initialized");
}
return sSingleton;
}
EmbedLiteApp::EmbedLiteApp()
: mListener(NULL)
, mUILoop(NULL)
, mSubThread(NULL)
, mAppParent(NULL)
, mAppChild(NULL)
, mEmbedType(EMBED_INVALID)
, mState(STOPPED)
, mRenderType(RENDER_AUTO)
, mProfilePath(strdup("mozembed"))
, mIsAsyncLoop(false)
{
LOGT();
sSingleton = this;
}
EmbedLiteApp::~EmbedLiteApp()
{
LOGT();
NS_ASSERTION(!mUILoop, "Main Loop not stopped before destroy");
NS_ASSERTION(!mSubThread, "Thread not stopped/destroyed before destroy");
NS_ASSERTION(mState == STOPPED, "Pre-mature deletion of still running application");
sSingleton = NULL;
if (mProfilePath) {
free(mProfilePath);
mProfilePath = nullptr;
}
}
void
EmbedLiteApp::SetListener(EmbedLiteAppListener* aListener)
{
LOGT();
// Assert with XOR that either mListener or aListener is NULL and not both.
NS_ASSERTION((!mListener != !aListener), "App listener is supposed to be set only once by embedder");
mListener = aListener;
}
EmbedLiteAppListener*
EmbedLiteApp::GetListener() {
if (!mListener) {
// No listener provided by embedder thus lazily create a stub object for EmbedLiteAppListener interface.
// TODO: the instance is not refcounted and is going to leak memory when EmbedLiteApp::SetListener()
// is called. If embedder is supposed to set a listener always then it'd make sense
// to be less defensive and to crash instead of creating a stub.
mListener = new EmbedLiteAppListener();
}
return mListener;
}
MessageLoop*
EmbedLiteApp::GetUILoop() {
return static_cast<MessageLoop*>(mUILoop);
};
void*
EmbedLiteApp::PostTask(EMBEDTaskCallback callback, void* userData, int timeout)
{
CancelableTask* newTask = NewRunnableFunction(callback, userData);
if (timeout) {
mUILoop->PostDelayedTask(FROM_HERE, newTask, timeout);
} else {
mUILoop->PostTask(FROM_HERE, newTask);
}
return (void*)newTask;
}
void*
EmbedLiteApp::PostCompositorTask(EMBEDTaskCallback callback, void* userData, int timeout)
{
if (!mozilla::layers::CompositorParent::CompositorLoop()) {
// Can't post compositor task if gecko compositor thread has not been initialized, yet.
return nullptr;
}
CancelableTask* newTask = NewRunnableFunction(callback, userData);
MessageLoop* compositorLoop = mozilla::layers::CompositorParent::CompositorLoop();
MOZ_ASSERT(compositorLoop);
if (timeout) {
compositorLoop->PostDelayedTask(FROM_HERE, newTask, timeout);
} else {
compositorLoop->PostTask(FROM_HERE, newTask);
}
return (void*)newTask;
}
void
EmbedLiteApp::CancelTask(void* aTask)
{
if (aTask) {
static_cast<CancelableTask*>(aTask)->Cancel();
}
}
void
EmbedLiteApp::StartChild(EmbedLiteApp* aApp)
{
LOGT();
NS_ASSERTION(aApp->mState == STARTING, "Wrong timing");
if (aApp->mEmbedType == EMBED_THREAD) {
if (!aApp->mListener ||
!aApp->mListener->ExecuteChildThread()) {
// If toolkit hasn't started a child thread we have to create the thread on our own
aApp->mSubThread = new EmbedLiteSubThread(aApp);
if (!aApp->mSubThread->StartEmbedThread()) {
LOGE("Failed to start child thread");
}
}
} else if (aApp->mEmbedType == EMBED_PROCESS) {
aApp->mAppParent = EmbedLiteAppProcessParent::CreateEmbedLiteAppProcessParent();
}
}
void
EmbedLiteApp::SetProfilePath(const char* aPath)
{
NS_ASSERTION(mState == STOPPED, "SetProfilePath must be called before Start");
if (mProfilePath)
free(mProfilePath);
mProfilePath = aPath ? strdup(aPath) : nullptr;
}
EmbedLiteMessagePump*
EmbedLiteApp::CreateEmbedLiteMessagePump(EmbedLiteMessagePumpListener* aListener)
{
return new EmbedLiteMessagePump(aListener);
}
bool
EmbedLiteApp::StartWithCustomPump(EmbedType aEmbedType, EmbedLiteMessagePump* aEventLoop)
{
LOGT("Type: %s", aEmbedType == EMBED_THREAD ? "Thread" : "Process");
mozilla::startup::sIsEmbedlite = aEmbedType == EMBED_PROCESS;
NS_ASSERTION(mState == STOPPED, "App can be started only when it stays still");
NS_ASSERTION(!mUILoop, "Start called twice");
SetState(STARTING);
mEmbedType = aEmbedType;
mUILoop = aEventLoop->GetMessageLoop();
mUILoop->PostTask(FROM_HERE,
NewRunnableFunction(&EmbedLiteApp::StartChild, this));
mUILoop->StartLoop();
mIsAsyncLoop = true;
return true;
}
bool
EmbedLiteApp::Start(EmbedType aEmbedType)
{
LOGT("Type: %s", aEmbedType == EMBED_THREAD ? "Thread" : "Process");
mozilla::startup::sIsEmbedlite = aEmbedType == EMBED_PROCESS;
NS_ASSERTION(mState == STOPPED, "App can be started only when it stays still");
NS_ASSERTION(!mUILoop, "Start called twice");
SetState(STARTING);
mEmbedType = aEmbedType;
base::AtExitManager exitManager;
mUILoop = new EmbedLiteUILoop();
mUILoop->PostTask(FROM_HERE,
NewRunnableFunction(&EmbedLiteApp::StartChild, this));
mUILoop->StartLoop();
if (mSubThread) {
mSubThread->Stop();
mSubThread = NULL;
} else if (mListener) {
NS_ASSERTION(mListener->StopChildThread(),
"StopChildThread must be implemented when ExecuteChildThread defined");
}
if (mUILoop) {
delete mUILoop;
mUILoop = NULL;
}
if (mListener) {
mListener->Destroyed();
}
return true;
}
void
EmbedLiteApp::AddManifestLocation(const char* manifest)
{
if (mState == INITIALIZED) {
unused << mAppParent->SendLoadComponentManifest(nsDependentCString(manifest));
} else {
sComponentDirs.AppendElement(nsCString(manifest));
}
}
bool
EmbedLiteApp::StartChildThread()
{
NS_ENSURE_TRUE(mEmbedType == EMBED_THREAD, false);
LOGT("mUILoop:%p, current:%p", mUILoop, MessageLoop::current());
NS_ASSERTION(MessageLoop::current() != mUILoop,
"Current message loop must be null and not equals to mUILoop");
for (unsigned int i = 0; i < sComponentDirs.Length(); i++) {
nsCOMPtr<nsIFile> f;
NS_NewNativeLocalFile(sComponentDirs[i], true,
getter_AddRefs(f));
XRE_AddManifestLocation(NS_COMPONENT_LOCATION, f);
}
GeckoLoader::InitEmbedding(mProfilePath);
mAppParent = new EmbedLiteAppThreadParent();
mAppChild = new EmbedLiteAppThreadChild(mUILoop);
MessageLoop::current()->PostTask(FROM_HERE,
NewRunnableMethod(mAppChild.get(),
&EmbedLiteAppThreadChild::Init,
mAppParent->GetIPCChannel()));
return true;
}
bool
EmbedLiteApp::StopChildThread()
{
NS_ENSURE_TRUE(mEmbedType == EMBED_THREAD, false);
LOGT("mUILoop:%p, current:%p", mUILoop, MessageLoop::current());
if (!mUILoop || !MessageLoop::current() ||
mUILoop == MessageLoop::current()) {
NS_ERROR("Wrong thread? StartChildThread called? Stop() already called?");
return false;
}
mAppChild->Close();
delete mAppParent;
mAppParent = nullptr;
mAppChild = nullptr;
GeckoLoader::TermEmbedding();
return true;
}
void _FinalStop(EmbedLiteApp* app)
{
app->Shutdown();
}
void
EmbedLiteApp::PreDestroy(EmbedLiteApp* app)
{
if (app->mAppParent == nullptr) {
LOGE("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!app->mAppParent is null, wrong logic?");
return;
}
unused << app->mAppParent->SendPreDestroy();
}
void
EmbedLiteApp::Stop()
{
LOGT();
NS_ASSERTION(mState == STARTING || mState == INITIALIZED, "Wrong timing");
if (mState == INITIALIZED) {
if (mViews.empty() && mWindows.empty()) {
mUILoop->PostTask(FROM_HERE,
NewRunnableFunction(&EmbedLiteApp::PreDestroy, this));
} else {
for (auto viewPair : mViews) {
viewPair.second->Destroy();
}
for (auto winPair: mWindows) {
winPair.second->Destroy();
}
}
}
SetState(DESTROYING);
}
void
EmbedLiteApp::Shutdown()
{
LOGT();
NS_ASSERTION(mState == DESTROYING, "Wrong timing");
if (mIsAsyncLoop) {
if (mEmbedType == EMBED_THREAD) {
if (mSubThread) {
mSubThread->Stop();
mSubThread = NULL;
} else if (mListener) {
NS_ASSERTION(mListener->StopChildThread(),
"StopChildThread must be implemented when ExecuteChildThread defined");
}
} else if (mEmbedType == EMBED_PROCESS) {
delete mAppParent;
}
}
mUILoop->DoQuit();
if (mIsAsyncLoop) {
delete mUILoop;
mUILoop = nullptr;
}
if (mListener) {
mListener->Destroyed();
}
SetState(STOPPED);
}
void
EmbedLiteApp::SetBoolPref(const char* aName, bool aValue)
{
NS_ENSURE_TRUE(mState == INITIALIZED, );
NS_ASSERTION(mState == INITIALIZED, "Wrong timing");
unused << mAppParent->SendSetBoolPref(nsDependentCString(aName), aValue);
}
void
EmbedLiteApp::SetCharPref(const char* aName, const char* aValue)
{
NS_ASSERTION(mState == INITIALIZED, "Wrong timing");
unused << mAppParent->SendSetCharPref(nsDependentCString(aName), nsDependentCString(aValue));
}
void
EmbedLiteApp::SetIntPref(const char* aName, int aValue)
{
NS_ASSERTION(mState == INITIALIZED, "Wrong timing");
unused << mAppParent->SendSetIntPref(nsDependentCString(aName), aValue);
}
void
EmbedLiteApp::LoadGlobalStyleSheet(const char* aUri, bool aEnable)
{
LOGT();
NS_ASSERTION(mState == INITIALIZED, "Wrong timing");
unused << mAppParent->SendLoadGlobalStyleSheet(nsDependentCString(aUri), aEnable);
}
void
EmbedLiteApp::SendObserve(const char* aMessageName, const char16_t* aMessage)
{
LOGT("topic:%s", aMessageName);
NS_ENSURE_TRUE(mState == INITIALIZED, );
unused << mAppParent->SendObserve(nsDependentCString(aMessageName), aMessage ? nsDependentString((const char16_t*)aMessage) : nsString());
}
void
EmbedLiteApp::AddObserver(const char* aMessageName)
{
LOGT("topic:%s", aMessageName);
NS_ASSERTION(mState == INITIALIZED, "Wrong timing");
unused << mAppParent->SendAddObserver(nsDependentCString(aMessageName));
}
void
EmbedLiteApp::RemoveObserver(const char* aMessageName)
{
LOGT("topic:%s", aMessageName);
NS_ASSERTION(mState == INITIALIZED, "Wrong timing");
unused << mAppParent->SendRemoveObserver(nsDependentCString(aMessageName));
}
void EmbedLiteApp::AddObservers(nsTArray<nsCString>& observersList)
{
NS_ASSERTION(mState == INITIALIZED, "Wrong timing");
unused << mAppParent->SendAddObservers(observersList);
}
void EmbedLiteApp::RemoveObservers(nsTArray<nsCString>& observersList)
{
NS_ASSERTION(mState == INITIALIZED, "Wrong timing");
unused << mAppParent->SendRemoveObservers(observersList);
}
EmbedLiteView*
EmbedLiteApp::CreateView(EmbedLiteWindow* aWindow, uint32_t aParent, bool aIsPrivateWindow)
{
LOGT();
NS_ASSERTION(mState == INITIALIZED, "The app must be up and runnning by now");
static uint32_t sViewCreateID = 0;
sViewCreateID++;
PEmbedLiteViewParent* viewParent = static_cast<PEmbedLiteViewParent*>(
mAppParent->SendPEmbedLiteViewConstructor(aWindow->GetUniqueID(), sViewCreateID,
aParent, aIsPrivateWindow));
EmbedLiteView* view = new EmbedLiteView(this, aWindow, viewParent, sViewCreateID);
mViews[sViewCreateID] = view;
return view;
}
EmbedLiteWindow*
EmbedLiteApp::CreateWindow()
{
LOGT();
NS_ASSERTION(mState == INITIALIZED, "The app must be up and runnning by now");
static uint32_t sWindowCreateID = 0;
sWindowCreateID++;
PEmbedLiteWindowParent* windowParent = static_cast<PEmbedLiteWindowParent*>(
mAppParent->SendPEmbedLiteWindowConstructor(sWindowCreateID));
EmbedLiteWindow* window = new EmbedLiteWindow(this, windowParent, sWindowCreateID);
mWindows[sWindowCreateID] = window;
return window;
}
EmbedLiteView* EmbedLiteApp::GetViewByID(uint32_t id)
{
std::map<uint32_t, EmbedLiteView*>::iterator it = mViews.find(id);
if (it == mViews.end()) {
NS_ERROR("View not found");
return nullptr;
}
return it->second;
}
EmbedLiteWindow* EmbedLiteApp::GetWindowByID(uint32_t id)
{
std::map<uint32_t, EmbedLiteWindow*>::iterator it = mWindows.find(id);
if (it == mWindows.end()) {
NS_ERROR("Window not found!");
return nullptr;
}
return it->second;
}
void
EmbedLiteApp::ChildReadyToDestroy()
{
LOGT();
if (mState == DESTROYING) {
mUILoop->PostTask(FROM_HERE,
NewRunnableFunction(&_FinalStop, this));
}
if (mEmbedType == EMBED_PROCESS) {
mAppParent = nullptr;
}
}
uint32_t
EmbedLiteApp::CreateWindowRequested(const uint32_t& chromeFlags, const char* uri, const uint32_t& contextFlags, const uint32_t& parentId)
{
EmbedLiteView* view = nullptr;
std::map<uint32_t, EmbedLiteView*>::iterator it;
for (it = mViews.begin(); it != mViews.end(); it++) {
if (it->second && it->second->GetUniqueID() == parentId) {
LOGT("Found parent view:%p", it->second);
view = it->second;
break;
}
}
uint32_t viewId = mListener ? mListener->CreateNewWindowRequested(chromeFlags, uri, contextFlags, view) : 0;
return viewId;
}
void
EmbedLiteApp::ViewDestroyed(uint32_t id)
{
LOGT("id:%i", id);
std::map<uint32_t, EmbedLiteView*>::iterator it = mViews.find(id);
if (it != mViews.end()) {
EmbedLiteView* view = it->second;
mViews.erase(it);
delete view;
}
if (mViews.empty()) {
if (mListener) {
mListener->LastViewDestroyed();
}
if (mState == DESTROYING) {
mUILoop->PostTask(FROM_HERE,
NewRunnableFunction(&EmbedLiteApp::PreDestroy, this));
}
}
}
void
EmbedLiteApp::WindowDestroyed(uint32_t id)
{
LOGT("id:%i", id);
std::map<uint32_t, EmbedLiteWindow*>::iterator it = mWindows.find(id);
if (it != mWindows.end()) {
EmbedLiteWindow* win = it->second;
mWindows.erase(it);
delete win;
}
if (mWindows.empty()) {
if (mListener) {
mListener->LastWindowDestroyed();
}
}
}
void EmbedLiteApp::DestroyView(EmbedLiteView* aView)
{
LOGT();
NS_ASSERTION(mState == INITIALIZED, "Wrong timing");
for (auto elm : mViews) {
if (aView == elm.second) {
elm.second->Destroy();
return;
}
}
MOZ_ASSERT(false, "Invalid EmbedLiteView pointer!");
}
void EmbedLiteApp::DestroyWindow(EmbedLiteWindow* aWindow)
{
LOGT();
NS_ASSERTION(mState == INITIALIZED, "Wrong timing");
for (auto elm : mWindows) {
if (aWindow == elm.second) {
elm.second->Destroy();
return;
}
}
MOZ_ASSERT(false, "Invalid EmbedLiteWindow pointer!");
}
void
EmbedLiteApp::SetIsAccelerated(bool aIsAccelerated)
{
#if defined(GL_PROVIDER_EGL) || defined(GL_PROVIDER_GLX)
if (aIsAccelerated) {
mRenderType = RENDER_HW;
} else
#endif
{
mRenderType = RENDER_SW;
}
}
void
EmbedLiteApp::Initialized()
{
LOGT();
NS_ASSERTION(mState == STARTING || mState == DESTROYING, "Wrong timing");
if (mState == DESTROYING) {
mUILoop->PostTask(FROM_HERE,
NewRunnableFunction(&EmbedLiteApp::PreDestroy, this));
return;
}
SetState(INITIALIZED);
if (mListener) {
mListener->Initialized();
}
}
void
EmbedLiteApp::SetState(State aState)
{
LOGT("State transition: %d -> %d", mState, aState);
mState = aState;
}
} // namespace embedlite
} // namespace mozilla
mozilla::embedlite::EmbedLiteApp*
XRE_GetEmbedLite()
{
return mozilla::embedlite::EmbedLiteApp::GetInstance();
}