/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* vim: set sw=4 ts=8 et tw=80 : */ /* ***** BEGIN LICENSE BLOCK ***** * Version: MPL 1.1/GPL 2.0/LGPL 2.1 * * The contents of this file are subject to the Mozilla Public License Version * 1.1 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is Mozilla Content App. * * The Initial Developer of the Original Code is * The Mozilla Foundation. * Portions created by the Initial Developer are Copyright (C) 2009 * the Initial Developer. All Rights Reserved. * * Contributor(s): * * Alternatively, the contents of this file may be used under the terms of * either the GNU General Public License Version 2 or later (the "GPL"), or * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the MPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the MPL, the GPL or the LGPL. * * ***** END LICENSE BLOCK ***** */ #include "ContentProcessParent.h" #include "TabParent.h" #include "mozilla/ipc/TestShellParent.h" #include "mozilla/net/NeckoParent.h" #include "nsIObserverService.h" #include "nsAutoPtr.h" #include "nsCOMPtr.h" #include "nsServiceManagerUtils.h" #include "nsThreadUtils.h" using namespace mozilla::ipc; using namespace mozilla::net; using mozilla::MonitorAutoEnter; namespace { PRBool gSingletonDied = PR_FALSE; } namespace mozilla { namespace dom { ContentProcessParent* ContentProcessParent::gSingleton; ContentProcessParent* ContentProcessParent::GetSingleton() { if (!gSingleton && !gSingletonDied) { nsRefPtr parent = new ContentProcessParent(); if (parent) { nsCOMPtr obs = do_GetService("@mozilla.org/observer-service;1"); if (obs) { if (NS_SUCCEEDED(obs->AddObserver(parent, "xpcom-shutdown", PR_FALSE))) { gSingleton = parent; } } nsCOMPtr threadInt(do_QueryInterface(NS_GetCurrentThread())); if (threadInt) { threadInt->GetObserver(getter_AddRefs(parent->mOldObserver)); threadInt->SetObserver(parent); } } } return gSingleton; } void ContentProcessParent::ActorDestroy(ActorDestroyReason why) { nsCOMPtr kungFuDeathGrip(static_cast(this)); nsCOMPtr obs(do_GetService("@mozilla.org/observer-service;1")); if (obs) obs->RemoveObserver(static_cast(this), "xpcom-shutdown"); nsCOMPtr threadInt(do_QueryInterface(NS_GetCurrentThread())); if (threadInt) threadInt->SetObserver(mOldObserver); if (mRunToCompletionDepth) mRunToCompletionDepth = 0; } TabParent* ContentProcessParent::CreateTab() { return static_cast(SendPIFrameEmbeddingConstructor()); } TestShellParent* ContentProcessParent::CreateTestShell() { return static_cast(SendPTestShellConstructor()); } bool ContentProcessParent::DestroyTestShell(TestShellParent* aTestShell) { return PTestShellParent::Send__delete__(aTestShell); } ContentProcessParent::ContentProcessParent() : mMonitor("ContentProcessParent::mMonitor") , mRunToCompletionDepth(0) { NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); mSubprocess = new GeckoChildProcessHost(GeckoProcessType_Content); mSubprocess->AsyncLaunch(); Open(mSubprocess->GetChannel(), mSubprocess->GetChildProcessHandle()); } ContentProcessParent::~ContentProcessParent() { NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); NS_ASSERTION(gSingleton == this, "More than one singleton?!"); gSingletonDied = PR_TRUE; gSingleton = nsnull; } NS_IMPL_THREADSAFE_ISUPPORTS2(ContentProcessParent, nsIObserver, nsIThreadObserver) namespace { void DeleteSubprocess(GeckoChildProcessHost* aSubprocess) { delete aSubprocess; } } NS_IMETHODIMP ContentProcessParent::Observe(nsISupports* aSubject, const char* aTopic, const PRUnichar* aData) { if (!strcmp(aTopic, "xpcom-shutdown") && mSubprocess) { Close(); XRE_GetIOMessageLoop()->PostTask( FROM_HERE, NewRunnableFunction(DeleteSubprocess, mSubprocess)); mSubprocess = nsnull; } return NS_OK; } PIFrameEmbeddingParent* ContentProcessParent::AllocPIFrameEmbedding() { return new TabParent(); } bool ContentProcessParent::DeallocPIFrameEmbedding(PIFrameEmbeddingParent* frame) { delete frame; return true; } PTestShellParent* ContentProcessParent::AllocPTestShell() { return new TestShellParent(); } bool ContentProcessParent::DeallocPTestShell(PTestShellParent* shell) { delete shell; return true; } PNeckoParent* ContentProcessParent::AllocPNecko() { return new NeckoParent(); } bool ContentProcessParent::DeallocPNecko(PNeckoParent* necko) { delete necko; return true; } bool ContentProcessParent::RequestRunToCompletion() { if (!mRunToCompletionDepth && BlockChild()) { #ifdef DEBUG printf("Running to completion...\n"); #endif mRunToCompletionDepth = 1; } return !!mRunToCompletionDepth; } /* void onDispatchedEvent (in nsIThreadInternal thread); */ NS_IMETHODIMP ContentProcessParent::OnDispatchedEvent(nsIThreadInternal *thread) { if (mOldObserver) return mOldObserver->OnDispatchedEvent(thread); return NS_OK; } /* void onProcessNextEvent (in nsIThreadInternal thread, in boolean mayWait, in unsigned long recursionDepth); */ NS_IMETHODIMP ContentProcessParent::OnProcessNextEvent(nsIThreadInternal *thread, PRBool mayWait, PRUint32 recursionDepth) { if (mRunToCompletionDepth) ++mRunToCompletionDepth; if (mOldObserver) return mOldObserver->OnProcessNextEvent(thread, mayWait, recursionDepth); return NS_OK; } /* void afterProcessNextEvent (in nsIThreadInternal thread, in unsigned long recursionDepth); */ NS_IMETHODIMP ContentProcessParent::AfterProcessNextEvent(nsIThreadInternal *thread, PRUint32 recursionDepth) { if (mRunToCompletionDepth && !--mRunToCompletionDepth) { #ifdef DEBUG printf("... ran to completion.\n"); #endif UnblockChild(); } if (mOldObserver) return mOldObserver->AfterProcessNextEvent(thread, recursionDepth); return NS_OK; } } // namespace dom } // namespace mozilla