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
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:expandtab:shiftwidth=2:tabstop=8:
*/
/* 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 "nsRemoteClient.h"
#ifdef MOZ_WIDGET_GTK
# ifdef MOZ_ENABLE_DBUS
# include "nsDBusRemoteServer.h"
# include "nsDBusRemoteClient.h"
# else
# include "nsGTKRemoteServer.h"
# include "nsXRemoteClient.h"
# endif
#elif defined(XP_WIN)
# include "nsWinRemoteServer.h"
# include "nsWinRemoteClient.h"
#elif defined(XP_DARWIN)
# include "nsMacRemoteServer.h"
# include "nsMacRemoteClient.h"
#endif
#include "nsRemoteService.h"
#include "nsIObserverService.h"
#include "nsString.h"
#include "nsServiceManagerUtils.h"
#include "SpecialSystemDirectory.h"
#include "mozilla/StaticPtr.h"
#include "mozilla/TimeStamp.h"
#include "mozilla/UniquePtr.h"
// Time to wait for the startup lock
#define START_TIMEOUT_MSEC 5000
#define START_SLEEP_MSEC 100
extern int gArgc;
extern char** gArgv;
using namespace mozilla;
nsStartupLock::nsStartupLock(nsIFile* aDir, nsProfileLock& aLock) : mDir(aDir) {
mLock = aLock;
}
nsStartupLock::~nsStartupLock() {
mLock.Unlock();
mLock.Cleanup();
mDir->Remove(false);
}
NS_IMPL_ISUPPORTS(nsRemoteService, nsIObserver, nsIRemoteService)
nsRemoteService::nsRemoteService() : mProgram("mozilla") {
ToLowerCase(mProgram);
}
void nsRemoteService::SetProgram(const char* aProgram) {
mProgram = aProgram;
ToLowerCase(mProgram);
}
void nsRemoteService::SetProfile(nsACString& aProfile) { mProfile = aProfile; }
#ifdef MOZ_WIDGET_GTK
void nsRemoteService::SetStartupToken(nsACString& aStartupToken) {
mStartupToken = aStartupToken;
}
#endif
// Attempts to lock the given directory by polling until the timeout is reached.
static nsresult AcquireLock(nsIFile* aMutexDir, double aTimeout,
nsProfileLock& aProfileLock) {
const mozilla::TimeStamp epoch = mozilla::TimeStamp::Now();
do {
// If we have been waiting for another instance to release the lock it will
// have deleted the lock directory when doing so so we have to make sure it
// exists every time we poll for the lock.
nsresult rv = aMutexDir->Create(nsIFile::DIRECTORY_TYPE, 0700);
if (NS_FAILED(rv) && rv != NS_ERROR_FILE_ALREADY_EXISTS) {
NS_WARNING("Unable to create startup lock directory.");
return rv;
}
rv = aProfileLock.Lock(aMutexDir, nullptr);
if (NS_SUCCEEDED(rv)) {
return NS_OK;
}
PR_Sleep(START_SLEEP_MSEC);
} while ((mozilla::TimeStamp::Now() - epoch) <
mozilla::TimeDuration::FromMilliseconds(aTimeout));
return NS_ERROR_FAILURE;
}
RefPtr<nsRemoteService::StartupLockPromise> nsRemoteService::AsyncLockStartup(
double aTimeout) {
// If startup is already locked we can just resolve immediately.
RefPtr<nsStartupLock> lock(mStartupLock);
if (lock) {
return StartupLockPromise::CreateAndResolve(lock, __func__);
}
// If there is already an executing promise we can just return that otherwise
// we have to start a new one.
if (mStartupLockPromise) {
return mStartupLockPromise;
}
nsCOMPtr<nsIFile> mutexDir;
nsresult rv = GetSpecialSystemDirectory(OS_TemporaryDirectory,
getter_AddRefs(mutexDir));
if (NS_FAILED(rv)) {
return StartupLockPromise::CreateAndReject(rv, __func__);
}
rv = mutexDir->AppendNative(mProgram);
if (NS_FAILED(rv)) {
return StartupLockPromise::CreateAndReject(rv, __func__);
}
nsCOMPtr<nsISerialEventTarget> queue;
MOZ_ALWAYS_SUCCEEDS(NS_CreateBackgroundTaskQueue("StartupLockTaskQueue",
getter_AddRefs(queue)));
mStartupLockPromise = InvokeAsync(
queue, __func__, [mutexDir = std::move(mutexDir), aTimeout]() {
nsProfileLock lock;
nsresult rv = AcquireLock(mutexDir, aTimeout, lock);
if (NS_SUCCEEDED(rv)) {
return StartupLockPromise::CreateAndResolve(
new nsStartupLock(mutexDir, lock), __func__);
}
return StartupLockPromise::CreateAndReject(rv, __func__);
});
// Note this is the guaranteed first `Then` and will run before any other
// `Then`s.
mStartupLockPromise->Then(
GetCurrentSerialEventTarget(), __func__,
[this, self = RefPtr{this}](
const StartupLockPromise::ResolveOrRejectValue& aResult) {
if (aResult.IsResolve()) {
mStartupLock = aResult.ResolveValue();
}
mStartupLockPromise = nullptr;
});
return mStartupLockPromise;
}
already_AddRefed<nsStartupLock> nsRemoteService::LockStartup() {
MOZ_RELEASE_ASSERT(!mStartupLockPromise,
"Should not have started an asynchronous lock attempt");
// If we're already locked just return the current lock.
RefPtr<nsStartupLock> lock(mStartupLock);
if (lock) {
return lock.forget();
}
nsCOMPtr<nsIFile> mutexDir;
nsresult rv = GetSpecialSystemDirectory(OS_TemporaryDirectory,
getter_AddRefs(mutexDir));
if (NS_FAILED(rv)) {
return nullptr;
}
rv = mutexDir->AppendNative(mProgram);
if (NS_FAILED(rv)) {
return nullptr;
}
nsProfileLock profileLock;
rv = AcquireLock(mutexDir, START_TIMEOUT_MSEC, profileLock);
if (NS_FAILED(rv)) {
NS_WARNING("Failed to lock for startup, continuing anyway.");
return nullptr;
}
lock = new nsStartupLock(mutexDir, profileLock);
mStartupLock = lock;
return lock.forget();
}
nsresult nsRemoteService::SendCommandLine(const nsACString& aProfile,
size_t aArgc, const char** aArgv,
bool aRaise) {
if (aProfile.IsEmpty()) {
return NS_ERROR_FAILURE;
}
UniquePtr<nsRemoteClient> client;
#ifdef MOZ_WIDGET_GTK
# if defined(MOZ_ENABLE_DBUS)
client = MakeUnique<nsDBusRemoteClient>(mStartupToken);
# else
client = MakeUnique<nsXRemoteClient>(mStartupToken);
# endif
#elif defined(XP_WIN)
client = MakeUnique<nsWinRemoteClient>();
#elif defined(XP_DARWIN)
client = MakeUnique<nsMacRemoteClient>();
#else
return NS_ERROR_NOT_AVAILABLE;
#endif
nsresult rv = client ? client->Init() : NS_ERROR_FAILURE;
NS_ENSURE_SUCCESS(rv, rv);
return client->SendCommandLine(mProgram.get(),
PromiseFlatCString(aProfile).get(), aArgc,
const_cast<const char**>(aArgv), aRaise);
}
NS_IMETHODIMP
nsRemoteService::SendCommandLine(const nsACString& aProfile,
const nsTArray<nsCString>& aArgs,
bool aRaise) {
#ifdef MOZ_WIDGET_GTK
// Linux clients block until they receive a response so it is impossible to
// send a remote command to the current profile.
if (aProfile.Equals(mProfile)) {
return NS_ERROR_INVALID_ARG;
}
#endif
nsAutoCString binaryPath;
nsTArray<const char*> args;
// Note that the command line must include an initial path to the binary but
// this is generally ignored.
args.SetCapacity(aArgs.Length() + 1);
args.AppendElement(binaryPath.get());
for (const nsCString& arg : aArgs) {
args.AppendElement(arg.get());
}
return SendCommandLine(aProfile, args.Length(), args.Elements(), aRaise);
}
nsresult nsRemoteService::StartClient() {
return SendCommandLine(mProfile, gArgc, const_cast<const char**>(gArgv),
true);
}
void nsRemoteService::StartupServer() {
if (mRemoteServer) {
return;
}
if (mProfile.IsEmpty()) {
return;
}
#ifdef MOZ_WIDGET_GTK
# if defined(MOZ_ENABLE_DBUS)
mRemoteServer = MakeUnique<nsDBusRemoteServer>();
# else
mRemoteServer = MakeUnique<nsGTKRemoteServer>();
# endif
#elif defined(XP_WIN)
mRemoteServer = MakeUnique<nsWinRemoteServer>();
#elif defined(XP_DARWIN)
mRemoteServer = MakeUnique<nsMacRemoteServer>();
#else
return;
#endif
if (!mRemoteServer) {
return;
}
nsresult rv = mRemoteServer->Startup(mProgram.get(), mProfile.get());
if (NS_FAILED(rv)) {
mRemoteServer = nullptr;
return;
}
nsCOMPtr<nsIObserverService> obs(
do_GetService("@mozilla.org/observer-service;1"));
if (obs) {
obs->AddObserver(this, "xpcom-shutdown", false);
obs->AddObserver(this, "quit-application", false);
}
}
void nsRemoteService::ShutdownServer() { mRemoteServer = nullptr; }
nsRemoteService::~nsRemoteService() { ShutdownServer(); }
NS_IMETHODIMP
nsRemoteService::Observe(nsISupports* aSubject, const char* aTopic,
const char16_t* aData) {
// This can be xpcom-shutdown or quit-application, but it's the same either
// way.
ShutdownServer();
return NS_OK;
}
|