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 "VRProcessChild.h"
8 
9 #include "mozilla/BackgroundHangMonitor.h"
10 #include "mozilla/ipc/IOThreadChild.h"
11 #include "ProcessUtils.h"
12 
13 using namespace mozilla;
14 using namespace mozilla::gfx;
15 using mozilla::ipc::IOThreadChild;
16 
17 StaticRefPtr<VRParent> sVRParent;
18 
VRProcessChild(ProcessId aParentPid)19 VRProcessChild::VRProcessChild(ProcessId aParentPid)
20     : ProcessChild(aParentPid) {}
21 
~VRProcessChild()22 VRProcessChild::~VRProcessChild() { sVRParent = nullptr; }
23 
24 /*static*/
GetVRParent()25 VRParent* VRProcessChild::GetVRParent() {
26   MOZ_ASSERT(sVRParent);
27   return sVRParent;
28 }
29 
Init(int aArgc,char * aArgv[])30 bool VRProcessChild::Init(int aArgc, char* aArgv[]) {
31   char* parentBuildID = nullptr;
32   char* prefsHandle = nullptr;
33   char* prefMapHandle = nullptr;
34   char* prefsLen = nullptr;
35   char* prefMapSize = nullptr;
36   for (int i = 1; i < aArgc; i++) {
37     if (!aArgv[i]) {
38       continue;
39     }
40     if (strcmp(aArgv[i], "-parentBuildID") == 0) {
41       parentBuildID = aArgv[i + 1];
42 
43 #ifdef XP_WIN
44     } else if (strcmp(aArgv[i], "-prefsHandle") == 0) {
45       if (++i == aArgc) {
46         return false;
47       }
48       prefsHandle = aArgv[i];
49     } else if (strcmp(aArgv[i], "-prefMapHandle") == 0) {
50       if (++i == aArgc) {
51         return false;
52       }
53       prefMapHandle = aArgv[i];
54 #endif
55     } else if (strcmp(aArgv[i], "-prefsLen") == 0) {
56       if (++i == aArgc) {
57         return false;
58       }
59       prefsLen = aArgv[i];
60     } else if (strcmp(aArgv[i], "-prefMapSize") == 0) {
61       if (++i == aArgc) {
62         return false;
63       }
64       prefMapSize = aArgv[i];
65     }
66   }
67 
68   ipc::SharedPreferenceDeserializer deserializer;
69   if (!deserializer.DeserializeFromSharedMemory(prefsHandle, prefMapHandle,
70                                                 prefsLen, prefMapSize)) {
71     return false;
72   }
73 
74   sVRParent = new VRParent();
75   sVRParent->Init(ParentPid(), parentBuildID, IOThreadChild::message_loop(),
76                   IOThreadChild::TakeChannel());
77 
78   return true;
79 }
80 
CleanUp()81 void VRProcessChild::CleanUp() {
82   sVRParent = nullptr;
83   NS_ShutdownXPCOM(nullptr);
84 }
85