gecko-dev/dom/ipc/ContentProcessParent.cpp

280 lines
7.9 KiB
C++
Raw Normal View History

2009-08-18 19:05:15 +00:00
/* -*- 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"
2009-08-18 19:05:15 +00:00
#include "mozilla/net/NeckoParent.h"
#include "nsIObserverService.h"
#include "nsAutoPtr.h"
#include "nsCOMPtr.h"
#include "nsServiceManagerUtils.h"
#include "nsThreadUtils.h"
#include "nsChromeRegistryChrome.h"
using namespace mozilla::ipc;
2009-08-18 19:05:15 +00:00
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<ContentProcessParent> parent = new ContentProcessParent();
if (parent) {
nsCOMPtr<nsIObserverService> obs =
do_GetService("@mozilla.org/observer-service;1");
if (obs) {
if (NS_SUCCEEDED(obs->AddObserver(parent, "xpcom-shutdown",
PR_FALSE))) {
gSingleton = parent;
}
}
nsCOMPtr<nsIThreadInternal>
threadInt(do_QueryInterface(NS_GetCurrentThread()));
if (threadInt) {
threadInt->GetObserver(getter_AddRefs(parent->mOldObserver));
threadInt->SetObserver(parent);
}
}
}
return gSingleton;
}
void
ContentProcessParent::ActorDestroy(ActorDestroyReason why)
{
nsCOMPtr<nsIThreadObserver>
kungFuDeathGrip(static_cast<nsIThreadObserver*>(this));
nsCOMPtr<nsIObserverService>
obs(do_GetService("@mozilla.org/observer-service;1"));
if (obs)
obs->RemoveObserver(static_cast<nsIObserver*>(this), "xpcom-shutdown");
nsCOMPtr<nsIThreadInternal>
threadInt(do_QueryInterface(NS_GetCurrentThread()));
if (threadInt)
threadInt->SetObserver(mOldObserver);
if (mRunToCompletionDepth)
mRunToCompletionDepth = 0;
}
TabParent*
ContentProcessParent::CreateTab()
{
return static_cast<TabParent*>(SendPIFrameEmbeddingConstructor());
}
TestShellParent*
ContentProcessParent::CreateTestShell()
{
return static_cast<TestShellParent*>(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());
nsCOMPtr<nsIChromeRegistry> registrySvc = nsChromeRegistry::GetService();
nsChromeRegistryChrome* chromeRegistry =
static_cast<nsChromeRegistryChrome*>(registrySvc.get());
chromeRegistry->SendRegisteredChrome(this);
}
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()
{
TabParent* parent = new TabParent();
if (parent){
NS_ADDREF(parent);
}
return parent;
}
bool
ContentProcessParent::DeallocPIFrameEmbedding(PIFrameEmbeddingParent* frame)
{
TabParent* parent = static_cast<TabParent*>(frame);
NS_RELEASE(parent);
return true;
}
PTestShellParent*
ContentProcessParent::AllocPTestShell()
{
return new TestShellParent();
}
bool
ContentProcessParent::DeallocPTestShell(PTestShellParent* shell)
{
delete shell;
return true;
}
2009-08-18 19:05:15 +00:00
PNeckoParent*
ContentProcessParent::AllocPNecko()
2009-08-18 19:05:15 +00:00
{
return new NeckoParent();
}
bool
ContentProcessParent::DeallocPNecko(PNeckoParent* necko)
2009-08-18 19:05:15 +00:00
{
delete necko;
return true;
2009-08-18 19:05:15 +00:00
}
bool
ContentProcessParent::RequestRunToCompletion()
{
if (!mRunToCompletionDepth &&
BlockChild()) {
#ifdef DEBUG
printf("Running to completion...\n");
#endif
mRunToCompletionDepth = 1;
}
2010-03-28 17:07:16 +00:00
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
2010-03-28 17:07:16 +00:00
UnblockChild();
}
if (mOldObserver)
return mOldObserver->AfterProcessNextEvent(thread, recursionDepth);
return NS_OK;
}
} // namespace dom
} // namespace mozilla