2009-08-18 12:05:15 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* vim: set sw=4 ts=8 et tw=80 : */
|
2009-09-09 15:00:14 -07:00
|
|
|
/* ***** 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):
|
2010-05-25 17:13:47 -07:00
|
|
|
* Frederic Plourde <frederic.plourde@collabora.co.uk>
|
2009-09-09 15:00:14 -07:00
|
|
|
*
|
|
|
|
* 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 ***** */
|
2009-08-12 09:18:08 -07:00
|
|
|
|
2010-07-19 11:33:33 -07:00
|
|
|
#include "ContentChild.h"
|
2009-08-12 09:18:08 -07:00
|
|
|
#include "TabChild.h"
|
|
|
|
|
2009-08-12 11:31:48 -07:00
|
|
|
#include "mozilla/ipc/TestShellChild.h"
|
2009-08-18 12:05:15 -07:00
|
|
|
#include "mozilla/net/NeckoChild.h"
|
2009-11-06 12:43:39 -08:00
|
|
|
#include "mozilla/ipc/XPCShellEnvironment.h"
|
|
|
|
#include "mozilla/jsipc/PContextWrapperChild.h"
|
2009-08-12 11:31:48 -07:00
|
|
|
|
2010-07-21 11:42:32 -07:00
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "nsTObserverArray.h"
|
|
|
|
#include "nsIObserver.h"
|
|
|
|
#include "nsIPrefService.h"
|
|
|
|
#include "nsIPrefBranch.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
2009-08-25 16:39:30 -07:00
|
|
|
#include "nsXULAppAPI.h"
|
2010-07-21 11:42:32 -07:00
|
|
|
#include "nsWeakReference.h"
|
2009-08-25 16:07:22 -07:00
|
|
|
|
2010-07-02 08:50:41 -07:00
|
|
|
#include "History.h"
|
|
|
|
#include "nsDocShellCID.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
|
2009-09-02 17:18:27 -07:00
|
|
|
#include "base/message_loop.h"
|
|
|
|
#include "base/task.h"
|
|
|
|
|
2010-03-10 21:33:00 -08:00
|
|
|
#include "nsChromeRegistryContent.h"
|
|
|
|
#include "mozilla/chrome/RegistryMessageUtils.h"
|
|
|
|
|
2009-08-12 11:31:48 -07:00
|
|
|
using namespace mozilla::ipc;
|
2009-08-18 12:05:15 -07:00
|
|
|
using namespace mozilla::net;
|
2010-07-02 08:50:41 -07:00
|
|
|
using namespace mozilla::places;
|
2009-08-12 11:31:48 -07:00
|
|
|
|
2009-08-12 09:18:08 -07:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2010-07-21 11:42:32 -07:00
|
|
|
class PrefObserver
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Pass |aHoldWeak=true| to force this to hold a weak ref to
|
|
|
|
* |aObserver|. Otherwise, this holds a strong ref.
|
|
|
|
*
|
|
|
|
* XXX/cjones: what do domain and prefRoot mean?
|
|
|
|
*/
|
|
|
|
PrefObserver(nsIObserver *aObserver, bool aHoldWeak,
|
|
|
|
const nsCString& aPrefRoot, const nsCString& aDomain)
|
|
|
|
: mPrefRoot(aPrefRoot)
|
|
|
|
, mDomain(aDomain)
|
|
|
|
{
|
|
|
|
if (aHoldWeak) {
|
|
|
|
nsCOMPtr<nsISupportsWeakReference> supportsWeakRef =
|
|
|
|
do_QueryInterface(aObserver);
|
|
|
|
if (supportsWeakRef)
|
|
|
|
mWeakObserver = do_GetWeakReference(aObserver);
|
|
|
|
} else {
|
|
|
|
mObserver = aObserver;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~PrefObserver() {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return true if this observer can no longer receive
|
|
|
|
* notifications.
|
|
|
|
*/
|
|
|
|
bool IsDead() const
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIObserver> observer = GetObserver();
|
2010-07-26 23:26:41 -07:00
|
|
|
return !observer;
|
2010-07-21 11:42:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return true iff a request to remove observers matching
|
|
|
|
* <aObserver, aDomain, aPrefRoot> entails removal of this.
|
|
|
|
*/
|
|
|
|
bool ShouldRemoveFrom(nsIObserver* aObserver,
|
|
|
|
const nsCString& aPrefRoot,
|
|
|
|
const nsCString& aDomain) const
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIObserver> observer = GetObserver();
|
|
|
|
return (observer == aObserver &&
|
|
|
|
mDomain == aDomain && mPrefRoot == aPrefRoot);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return true iff this should be notified of changes to |aPref|.
|
|
|
|
*/
|
|
|
|
bool Observes(const nsCString& aPref) const
|
|
|
|
{
|
|
|
|
nsCAutoString myPref(mPrefRoot);
|
|
|
|
myPref += mDomain;
|
|
|
|
return StringBeginsWith(aPref, myPref);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notify this of a pref change that's relevant to our interests
|
|
|
|
* (see Observes() above). Return false iff this no longer cares
|
|
|
|
* to observe any more pref changes.
|
|
|
|
*/
|
|
|
|
bool Notify() const
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIObserver> observer = GetObserver();
|
|
|
|
if (!observer) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrefBranch> prefBranch;
|
|
|
|
nsCOMPtr<nsIPrefService> prefService =
|
|
|
|
do_GetService(NS_PREFSERVICE_CONTRACTID);
|
|
|
|
if (prefService) {
|
|
|
|
prefService->GetBranch(mPrefRoot.get(),
|
|
|
|
getter_AddRefs(prefBranch));
|
|
|
|
observer->Observe(prefBranch, "nsPref:changed",
|
|
|
|
NS_ConvertASCIItoUTF16(mDomain).get());
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
already_AddRefed<nsIObserver> GetObserver() const
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIObserver> observer =
|
|
|
|
mObserver ? mObserver : do_QueryReferent(mWeakObserver);
|
|
|
|
return observer.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
// We only either hold a strong or a weak reference to the
|
|
|
|
// observer, so only either mObserver or
|
|
|
|
// GetReferent(mWeakObserver) is ever non-null.
|
|
|
|
nsCOMPtr<nsIObserver> mObserver;
|
|
|
|
nsWeakPtr mWeakObserver;
|
|
|
|
nsCString mPrefRoot;
|
|
|
|
nsCString mDomain;
|
|
|
|
|
|
|
|
// disable these
|
|
|
|
PrefObserver(const PrefObserver&);
|
|
|
|
PrefObserver& operator=(const PrefObserver&);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-07-19 11:33:33 -07:00
|
|
|
ContentChild* ContentChild::sSingleton;
|
2009-08-12 17:32:50 -07:00
|
|
|
|
2010-07-19 11:33:33 -07:00
|
|
|
ContentChild::ContentChild()
|
2010-07-21 11:42:32 -07:00
|
|
|
: mDead(false)
|
2009-08-12 09:18:08 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-07-19 11:33:33 -07:00
|
|
|
ContentChild::~ContentChild()
|
2009-08-12 09:18:08 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2010-07-19 11:33:33 -07:00
|
|
|
ContentChild::Init(MessageLoop* aIOLoop,
|
|
|
|
base::ProcessHandle aParentHandle,
|
|
|
|
IPC::Channel* aChannel)
|
2009-08-12 09:18:08 -07:00
|
|
|
{
|
2010-07-19 11:33:33 -07:00
|
|
|
NS_ASSERTION(!sSingleton, "only one ContentChild per child");
|
2009-08-12 17:32:50 -07:00
|
|
|
|
2009-10-27 14:52:37 -07:00
|
|
|
Open(aChannel, aParentHandle, aIOLoop);
|
2009-08-12 17:32:50 -07:00
|
|
|
sSingleton = this;
|
|
|
|
|
|
|
|
return true;
|
2009-08-12 09:18:08 -07:00
|
|
|
}
|
|
|
|
|
2010-07-19 11:33:33 -07:00
|
|
|
PBrowserChild*
|
2010-07-19 11:33:33 -07:00
|
|
|
ContentChild::AllocPBrowser(const PRUint32& aChromeFlags)
|
2009-08-12 09:18:08 -07:00
|
|
|
{
|
2010-07-19 11:33:33 -07:00
|
|
|
nsRefPtr<TabChild> iframe = new TabChild(aChromeFlags);
|
2010-05-14 22:27:52 -07:00
|
|
|
return NS_SUCCEEDED(iframe->Init()) ? iframe.forget().get() : NULL;
|
2009-08-12 09:18:08 -07:00
|
|
|
}
|
|
|
|
|
2009-09-17 16:09:20 -07:00
|
|
|
bool
|
2010-07-19 11:33:33 -07:00
|
|
|
ContentChild::DeallocPBrowser(PBrowserChild* iframe)
|
2009-08-12 09:18:08 -07:00
|
|
|
{
|
2010-05-14 22:27:52 -07:00
|
|
|
TabChild* child = static_cast<TabChild*>(iframe);
|
|
|
|
NS_RELEASE(child);
|
2009-09-17 16:09:20 -07:00
|
|
|
return true;
|
2009-08-12 09:18:08 -07:00
|
|
|
}
|
|
|
|
|
2009-09-09 15:59:06 -07:00
|
|
|
PTestShellChild*
|
2010-07-19 11:33:33 -07:00
|
|
|
ContentChild::AllocPTestShell()
|
2009-08-12 11:31:48 -07:00
|
|
|
{
|
2010-05-14 22:27:52 -07:00
|
|
|
return new TestShellChild();
|
2009-08-12 11:31:48 -07:00
|
|
|
}
|
|
|
|
|
2009-09-17 16:09:20 -07:00
|
|
|
bool
|
2010-07-19 11:33:33 -07:00
|
|
|
ContentChild::DeallocPTestShell(PTestShellChild* shell)
|
2009-08-12 11:31:48 -07:00
|
|
|
{
|
2010-05-14 22:27:52 -07:00
|
|
|
delete shell;
|
2009-09-17 16:09:20 -07:00
|
|
|
return true;
|
2009-08-25 16:07:22 -07:00
|
|
|
}
|
|
|
|
|
2009-11-06 12:43:39 -08:00
|
|
|
bool
|
2010-07-19 11:33:33 -07:00
|
|
|
ContentChild::RecvPTestShellConstructor(PTestShellChild* actor)
|
2009-11-06 12:43:39 -08:00
|
|
|
{
|
|
|
|
actor->SendPContextWrapperConstructor()->SendPObjectWrapperConstructor(true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-08-18 12:05:15 -07:00
|
|
|
PNeckoChild*
|
2010-07-19 11:33:33 -07:00
|
|
|
ContentChild::AllocPNecko()
|
2009-08-18 12:05:15 -07:00
|
|
|
{
|
|
|
|
return new NeckoChild();
|
|
|
|
}
|
|
|
|
|
2009-09-17 16:09:20 -07:00
|
|
|
bool
|
2010-07-19 11:33:33 -07:00
|
|
|
ContentChild::DeallocPNecko(PNeckoChild* necko)
|
2009-08-18 12:05:15 -07:00
|
|
|
{
|
|
|
|
delete necko;
|
2009-09-17 16:09:20 -07:00
|
|
|
return true;
|
2009-08-18 12:05:15 -07:00
|
|
|
}
|
|
|
|
|
2010-03-10 21:33:00 -08:00
|
|
|
bool
|
2010-07-19 11:33:33 -07:00
|
|
|
ContentChild::RecvRegisterChrome(const nsTArray<ChromePackage>& packages,
|
|
|
|
const nsTArray<ResourceMapping>& resources,
|
|
|
|
const nsTArray<OverrideMapping>& overrides)
|
2010-03-10 21:33:00 -08:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIChromeRegistry> registrySvc = nsChromeRegistry::GetService();
|
|
|
|
nsChromeRegistryContent* chromeRegistry =
|
|
|
|
static_cast<nsChromeRegistryContent*>(registrySvc.get());
|
|
|
|
chromeRegistry->RegisterRemoteChrome(packages, resources, overrides);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-05-11 05:44:12 -07:00
|
|
|
bool
|
2010-07-19 11:33:33 -07:00
|
|
|
ContentChild::RecvSetOffline(const PRBool& offline)
|
2010-05-11 05:44:12 -07:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIIOService> io (do_GetIOService());
|
|
|
|
NS_ASSERTION(io, "IO Service can not be null");
|
|
|
|
|
|
|
|
io->SetOffline(offline);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-12-06 22:03:49 -08:00
|
|
|
void
|
2010-07-19 11:33:33 -07:00
|
|
|
ContentChild::ActorDestroy(ActorDestroyReason why)
|
2009-09-02 17:18:27 -07:00
|
|
|
{
|
2009-12-06 22:03:49 -08:00
|
|
|
if (AbnormalShutdown == why)
|
|
|
|
NS_WARNING("shutting down because of crash!");
|
2009-09-02 17:18:27 -07:00
|
|
|
|
2010-07-21 11:42:32 -07:00
|
|
|
// We might be holding the last ref to some of the observers in
|
|
|
|
// mPrefObserverArray. Some of them try to unregister themselves
|
|
|
|
// in their dtors (sketchy). To side-step uaf problems and so
|
|
|
|
// forth, we set this mDead flag. Then, if during a Clear() a
|
|
|
|
// being-deleted observer tries to unregister itself, it hits the
|
|
|
|
// |if (mDead)| special case below and we're safe.
|
|
|
|
mDead = true;
|
|
|
|
mPrefObservers.Clear();
|
|
|
|
|
2009-12-06 22:03:49 -08:00
|
|
|
XRE_ShutdownChildProcess();
|
2009-09-02 17:18:27 -07:00
|
|
|
}
|
|
|
|
|
2010-05-25 17:13:47 -07:00
|
|
|
nsresult
|
2010-07-21 11:42:32 -07:00
|
|
|
ContentChild::AddRemotePrefObserver(const nsCString& aDomain,
|
|
|
|
const nsCString& aPrefRoot,
|
|
|
|
nsIObserver* aObserver,
|
2010-07-19 11:33:33 -07:00
|
|
|
PRBool aHoldWeak)
|
2010-05-25 17:13:47 -07:00
|
|
|
{
|
2010-07-21 11:42:32 -07:00
|
|
|
if (aObserver) {
|
|
|
|
mPrefObservers.AppendElement(
|
|
|
|
new PrefObserver(aObserver, aHoldWeak, aPrefRoot, aDomain));
|
|
|
|
}
|
2010-05-25 17:13:47 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2010-07-21 11:42:32 -07:00
|
|
|
ContentChild::RemoveRemotePrefObserver(const nsCString& aDomain,
|
|
|
|
const nsCString& aPrefRoot,
|
|
|
|
nsIObserver* aObserver)
|
2010-05-25 17:13:47 -07:00
|
|
|
{
|
2010-07-21 11:42:32 -07:00
|
|
|
if (mDead) {
|
|
|
|
// Silently ignore, we're about to exit. See comment in
|
|
|
|
// ActorDestroy().
|
2010-05-25 17:13:47 -07:00
|
|
|
return NS_OK;
|
2010-07-21 11:42:32 -07:00
|
|
|
}
|
2010-05-25 17:13:47 -07:00
|
|
|
|
2010-07-21 11:42:32 -07:00
|
|
|
for (PRUint32 i = 0; i < mPrefObservers.Length();
|
|
|
|
/*we mutate the array during the loop; ++i iff no mutation*/) {
|
|
|
|
PrefObserver* observer = mPrefObservers[i];
|
|
|
|
if (observer->IsDead()) {
|
|
|
|
mPrefObservers.RemoveElementAt(i);
|
|
|
|
continue;
|
|
|
|
} else if (observer->ShouldRemoveFrom(aObserver, aPrefRoot, aDomain)) {
|
|
|
|
mPrefObservers.RemoveElementAt(i);
|
2010-05-25 17:13:47 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-07-21 11:42:32 -07:00
|
|
|
++i;
|
2010-05-25 17:13:47 -07:00
|
|
|
}
|
2010-07-21 11:42:32 -07:00
|
|
|
|
|
|
|
NS_WARNING("RemoveRemotePrefObserver(): no observer was matched!");
|
2010-05-25 17:13:47 -07:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2010-07-21 11:42:32 -07:00
|
|
|
ContentChild::RecvNotifyRemotePrefObserver(const nsCString& aPref)
|
|
|
|
{
|
|
|
|
for (PRUint32 i = 0; i < mPrefObservers.Length();
|
|
|
|
/*we mutate the array during the loop; ++i iff no mutation*/) {
|
|
|
|
PrefObserver* observer = mPrefObservers[i];
|
|
|
|
if (observer->Observes(aPref) &&
|
|
|
|
!observer->Notify()) {
|
|
|
|
// |observer| had a weak ref that went away, so it no
|
|
|
|
// longer cares about pref changes
|
|
|
|
mPrefObservers.RemoveElementAt(i);
|
|
|
|
continue;
|
2010-05-25 17:13:47 -07:00
|
|
|
}
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-07-02 08:50:41 -07:00
|
|
|
bool
|
|
|
|
ContentChild::RecvNotifyVisited(const IPC::URI& aURI)
|
|
|
|
{
|
2010-07-26 12:59:34 -07:00
|
|
|
nsCOMPtr<nsIURI> newURI(aURI);
|
2010-07-02 08:50:41 -07:00
|
|
|
History::GetService()->NotifyVisited(newURI);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-08-12 09:18:08 -07:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|