Bug 675221 part D - Fix up URL classifier to stop using XPCOM proxies, r=dcamp
authorBenjamin Smedberg <benjamin@smedbergs.us>
Fri, 19 Aug 2011 11:50:04 -0400
changeset 75571 97407f5d696f516eb56fae979d1d013699ef2f2a
parent 75570 c2ca78ba49e27480fcd038180199d7ca2c45aa07
child 75572 e8cb17fdc2f64f8adc93acb5c90c3d7d778967b1
push id3
push userfelipc@gmail.com
push dateFri, 30 Sep 2011 20:09:13 +0000
reviewersdcamp
bugs675221
milestone9.0a1
Bug 675221 part D - Fix up URL classifier to stop using XPCOM proxies, r=dcamp
toolkit/components/url-classifier/Makefile.in
toolkit/components/url-classifier/nsUrlClassifierDBService.cpp
toolkit/components/url-classifier/nsUrlClassifierDBService.h
toolkit/components/url-classifier/nsUrlClassifierProxies.cpp
toolkit/components/url-classifier/nsUrlClassifierProxies.h
--- a/toolkit/components/url-classifier/Makefile.in
+++ b/toolkit/components/url-classifier/Makefile.in
@@ -56,16 +56,17 @@ XPIDLSRCS = \
   nsIUrlClassifierUtils.idl \
   nsIUrlListManager.idl \
   $(NULL)
 
 CPPSRCS = \
   nsUrlClassifierDBService.cpp \
   nsUrlClassifierStreamUpdater.cpp \
   nsUrlClassifierUtils.cpp \
+  nsUrlClassifierProxies.cpp \
   $(NULL)
 
 LOCAL_INCLUDES = \
   -I$(srcdir)/../build \
   $(SQLITE_CFLAGS) \
   $(NULL)
 
 EXTRA_COMPONENTS = \
--- a/toolkit/components/url-classifier/nsUrlClassifierDBService.cpp
+++ b/toolkit/components/url-classifier/nsUrlClassifierDBService.cpp
@@ -54,22 +54,22 @@
 #include "nsIDirectoryService.h"
 #include "nsIKeyModule.h"
 #include "nsIObserverService.h"
 #include "nsIPermissionManager.h"
 #include "nsIPrefBranch.h"
 #include "nsIPrefBranch2.h"
 #include "nsIPrefService.h"
 #include "nsIProperties.h"
-#include "nsIProxyObjectManager.h"
 #include "nsToolkitCompsCID.h"
 #include "nsIUrlClassifierUtils.h"
 #include "nsIRandomGenerator.h"
 #include "nsUrlClassifierDBService.h"
 #include "nsUrlClassifierUtils.h"
+#include "nsUrlClassifierProxies.h"
 #include "nsURILoader.h"
 #include "nsString.h"
 #include "nsReadableUtils.h"
 #include "nsTArray.h"
 #include "nsNetUtil.h"
 #include "nsNetCID.h"
 #include "nsThreadUtils.h"
 #include "nsXPCOMStrings.h"
@@ -176,18 +176,17 @@ static const PRLogModuleInfo *gUrlClassi
 #define UPDATE_DELAY_TIME           "urlclassifier.updatetime"
 #define UPDATE_DELAY_TIME_DEFAULT   60
 
 class nsUrlClassifierDBServiceWorker;
 
 // Singleton instance.
 static nsUrlClassifierDBService* sUrlClassifierDBService;
 
-// Thread that we do the updates on.
-static nsIThread* gDbBackgroundThread = nsnull;
+nsIThread* nsUrlClassifierDBService::gDbBackgroundThread = nsnull;
 
 // Once we've committed to shutting down, don't do work in the background
 // thread.
 static PRBool gShuttingDownThread = PR_FALSE;
 
 static PRInt32 gFreshnessGuarantee = CONFIRM_AGE_DEFAULT_SEC;
 
 static PRInt32 gUpdateCacheSize = UPDATE_CACHE_SIZE_DEFAULT;
@@ -3915,22 +3914,17 @@ nsUrlClassifierDBService::Init()
 
   rv = mWorker->Init(gethashNoise);
   if (NS_FAILED(rv)) {
     mWorker = nsnull;
     return rv;
   }
 
   // Proxy for calling the worker on the background thread
-  rv = NS_GetProxyForObject(gDbBackgroundThread,
-                            NS_GET_IID(nsIUrlClassifierDBServiceWorker),
-                            mWorker,
-                            NS_PROXY_ASYNC,
-                            getter_AddRefs(mWorkerProxy));
-  NS_ENSURE_SUCCESS(rv, rv);
+  mWorkerProxy = new UrlClassifierDBServiceWorkerProxy(mWorker);
 
   mCompleters.Init();
 
   // Add an observer for shutdown
   nsCOMPtr<nsIObserverService> observerService =
       mozilla::services::GetObserverService();
   if (!observerService)
     return NS_ERROR_FAILURE;
@@ -4033,47 +4027,36 @@ nsUrlClassifierDBService::LookupURI(nsIU
   // Create an nsUrlClassifierLookupCallback object.  This object will
   // take care of confirming partial hash matches if necessary before
   // calling the client's callback.
   nsCOMPtr<nsIUrlClassifierLookupCallback> callback =
     new nsUrlClassifierLookupCallback(this, c);
   if (!callback)
     return NS_ERROR_OUT_OF_MEMORY;
 
-  nsCOMPtr<nsIUrlClassifierLookupCallback> proxyCallback;
-  // The proxy callback uses the current thread.
-  rv = NS_GetProxyForObject(NS_PROXY_TO_CURRENT_THREAD,
-                            NS_GET_IID(nsIUrlClassifierLookupCallback),
-                            callback,
-                            NS_PROXY_ASYNC,
-                            getter_AddRefs(proxyCallback));
-  NS_ENSURE_SUCCESS(rv, rv);
+  nsCOMPtr<nsIUrlClassifierLookupCallback> proxyCallback =
+    new UrlClassifierLookupCallbackProxy(callback);
 
   // Queue this lookup and call the lookup function to flush the queue if
   // necessary.
   rv = mWorker->QueueLookup(key, proxyCallback);
   NS_ENSURE_SUCCESS(rv, rv);
 
   return mWorkerProxy->Lookup(EmptyCString(), nsnull);
 }
 
 NS_IMETHODIMP
 nsUrlClassifierDBService::GetTables(nsIUrlClassifierCallback* c)
 {
   NS_ENSURE_TRUE(gDbBackgroundThread, NS_ERROR_NOT_INITIALIZED);
 
   nsresult rv;
   // The proxy callback uses the current thread.
-  nsCOMPtr<nsIUrlClassifierCallback> proxyCallback;
-  rv = NS_GetProxyForObject(NS_PROXY_TO_CURRENT_THREAD,
-                            NS_GET_IID(nsIUrlClassifierCallback),
-                            c,
-                            NS_PROXY_ASYNC,
-                            getter_AddRefs(proxyCallback));
-  NS_ENSURE_SUCCESS(rv, rv);
+  nsCOMPtr<nsIUrlClassifierCallback> proxyCallback =
+    new UrlClassifierCallbackProxy(c);
 
   return mWorkerProxy->GetTables(proxyCallback);
 }
 
 NS_IMETHODIMP
 nsUrlClassifierDBService::SetHashCompleter(const nsACString &tableName,
                                            nsIUrlClassifierHashCompleter *completer)
 {
@@ -4098,23 +4081,18 @@ nsUrlClassifierDBService::BeginUpdate(ns
   if (mInUpdate)
     return NS_ERROR_NOT_AVAILABLE;
 
   mInUpdate = PR_TRUE;
 
   nsresult rv;
 
   // The proxy observer uses the current thread
-  nsCOMPtr<nsIUrlClassifierUpdateObserver> proxyObserver;
-  rv = NS_GetProxyForObject(NS_PROXY_TO_CURRENT_THREAD,
-                            NS_GET_IID(nsIUrlClassifierUpdateObserver),
-                            observer,
-                            NS_PROXY_ASYNC,
-                            getter_AddRefs(proxyObserver));
-  NS_ENSURE_SUCCESS(rv, rv);
+  nsCOMPtr<nsIUrlClassifierUpdateObserver> proxyObserver =
+    new UrlClassifierUpdateObserverProxy(observer);
 
   return mWorkerProxy->BeginUpdate(proxyObserver, updateTables, clientKey);
 }
 
 NS_IMETHODIMP
 nsUrlClassifierDBService::BeginStream(const nsACString &table,
                                       const nsACString &serverMAC)
 {
@@ -4279,8 +4257,15 @@ nsUrlClassifierDBService::Shutdown()
 
   nsIThread *backgroundThread = gDbBackgroundThread;
   gDbBackgroundThread = nsnull;
   backgroundThread->Shutdown();
   NS_RELEASE(backgroundThread);
 
   return NS_OK;
 }
+
+nsIThread*
+nsUrlClassifierDBService::BackgroundThread()
+{
+  return gDbBackgroundThread;
+}
+
--- a/toolkit/components/url-classifier/nsUrlClassifierDBService.h
+++ b/toolkit/components/url-classifier/nsUrlClassifierDBService.h
@@ -55,16 +55,17 @@
 
 // The hash length of a partial hash entry.
 #define PARTIAL_LENGTH 4
 
 // The hash length of a complete hash entry.
 #define COMPLETE_LENGTH 32
 
 class nsUrlClassifierDBServiceWorker;
+class nsIThread;
 
 // This is a proxy class that just creates a background thread and delagates
 // calls to the background thread.
 class nsUrlClassifierDBService : public nsIUrlClassifierDBService,
                                  public nsIURIClassifier,
                                  public nsIObserver
 {
 public:
@@ -81,31 +82,33 @@ public:
   NS_DECL_NSIURLCLASSIFIERDBSERVICE
   NS_DECL_NSIURICLASSIFIER
   NS_DECL_NSIOBSERVER
 
   PRBool GetCompleter(const nsACString& tableName,
                       nsIUrlClassifierHashCompleter** completer);
   nsresult CacheCompletions(nsTArray<nsUrlClassifierLookupResult> *results);
 
+  static nsIThread* BackgroundThread();
+
 private:
   // No subclassing
   ~nsUrlClassifierDBService();
 
   // Disallow copy constructor
   nsUrlClassifierDBService(nsUrlClassifierDBService&);
 
   nsresult LookupURI(nsIURI* uri, nsIUrlClassifierCallback* c,
                      PRBool forceCheck, PRBool *didCheck);
 
   // Close db connection and join the background thread if it exists. 
   nsresult Shutdown();
   
   nsCOMPtr<nsUrlClassifierDBServiceWorker> mWorker;
-  nsCOMPtr<nsUrlClassifierDBServiceWorker> mWorkerProxy;
+  nsCOMPtr<nsIUrlClassifierDBServiceWorker> mWorkerProxy;
 
   nsInterfaceHashtable<nsCStringHashKey, nsIUrlClassifierHashCompleter> mCompleters;
 
   // TRUE if the nsURIClassifier implementation should check for malware
   // uris on document loads.
   PRBool mCheckMalware;
 
   // TRUE if the nsURIClassifier implementation should check for phishing
@@ -115,13 +118,16 @@ private:
   // TRUE if a BeginUpdate() has been called without an accompanying
   // CancelUpdate()/FinishUpdate().  This is used to prevent competing
   // updates, not to determine whether an update is still being
   // processed.
   PRBool mInUpdate;
 
   // The list of tables that can use the default hash completer object.
   nsTArray<nsCString> mGethashWhitelist;
+
+  // Thread that we do the updates on.
+  static nsIThread* gDbBackgroundThread;
 };
 
 NS_DEFINE_STATIC_IID_ACCESSOR(nsUrlClassifierDBService, NS_URLCLASSIFIERDBSERVICE_CID)
 
 #endif // nsUrlClassifierDBService_h_
new file mode 100644
--- /dev/null
+++ b/toolkit/components/url-classifier/nsUrlClassifierProxies.cpp
@@ -0,0 +1,307 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* ***** 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 Firefox.
+ *
+ * The Initial Developer of the Original Code is
+ * the Mozilla Foundation <http://www.mozilla.org>.
+ * Portions created by the Initial Developer are Copyright (C) 2011
+ * 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 "nsUrlClassifierProxies.h"
+#include "nsUrlClassifierDBService.h"
+
+static nsresult
+DispatchToWorkerThread(nsIRunnable* r)
+{
+  nsIThread* t = nsUrlClassifierDBService::BackgroundThread();
+  if (!t)
+    return NS_ERROR_FAILURE;
+
+  return t->Dispatch(r, NS_DISPATCH_NORMAL);
+}
+
+NS_IMPL_THREADSAFE_ISUPPORTS1(UrlClassifierDBServiceWorkerProxy,
+                              nsIUrlClassifierDBServiceWorker)
+
+NS_IMETHODIMP
+UrlClassifierDBServiceWorkerProxy::Lookup(const nsACString& aSpec,
+                                          nsIUrlClassifierCallback* aCB)
+{
+  nsCOMPtr<nsIRunnable> r = new LookupRunnable(mTarget, aSpec, aCB);
+  return DispatchToWorkerThread(r);
+}
+
+NS_IMETHODIMP
+UrlClassifierDBServiceWorkerProxy::LookupRunnable::Run()
+{
+  mTarget->Lookup(mSpec, mCB);
+  return NS_OK;
+}
+
+NS_IMETHODIMP
+UrlClassifierDBServiceWorkerProxy::GetTables(nsIUrlClassifierCallback* aCB)
+{
+  nsCOMPtr<nsIRunnable> r = new GetTablesRunnable(mTarget, aCB);
+  return DispatchToWorkerThread(r);
+}
+
+NS_IMETHODIMP
+UrlClassifierDBServiceWorkerProxy::GetTablesRunnable::Run()
+{
+  mTarget->GetTables(mCB);
+  return NS_OK;
+}
+
+NS_IMETHODIMP
+UrlClassifierDBServiceWorkerProxy::SetHashCompleter
+  (const nsACString&, nsIUrlClassifierHashCompleter*)
+{
+  NS_NOTREACHED("This method should not be called!");
+  return NS_ERROR_NOT_IMPLEMENTED;
+}
+
+NS_IMETHODIMP
+UrlClassifierDBServiceWorkerProxy::BeginUpdate
+  (nsIUrlClassifierUpdateObserver* aUpdater,
+   const nsACString& aTables,
+   const nsACString& aClientKey)
+{
+  nsCOMPtr<nsIRunnable> r = new BeginUpdateRunnable(mTarget, aUpdater,
+                                                    aTables, aClientKey);
+  return DispatchToWorkerThread(r);
+}
+
+NS_IMETHODIMP
+UrlClassifierDBServiceWorkerProxy::BeginUpdateRunnable::Run()
+{
+  mTarget->BeginUpdate(mUpdater, mTables, mClientKey);
+  return NS_OK;
+}
+
+NS_IMETHODIMP
+UrlClassifierDBServiceWorkerProxy::BeginStream(const nsACString& aTable,
+                                               const nsACString& aServerMAC)
+{
+  nsCOMPtr<nsIRunnable> r =
+    new BeginStreamRunnable(mTarget, aTable, aServerMAC);
+  return DispatchToWorkerThread(r);
+}
+
+NS_IMETHODIMP
+UrlClassifierDBServiceWorkerProxy::BeginStreamRunnable::Run()
+{
+  mTarget->BeginStream(mTable, mServerMAC);
+  return NS_OK;
+}
+
+NS_IMETHODIMP
+UrlClassifierDBServiceWorkerProxy::UpdateStream(const nsACString& aUpdateChunk)
+{
+  nsCOMPtr<nsIRunnable> r =
+    new UpdateStreamRunnable(mTarget, aUpdateChunk);
+  return DispatchToWorkerThread(r);
+}
+
+NS_IMETHODIMP
+UrlClassifierDBServiceWorkerProxy::UpdateStreamRunnable::Run()
+{
+  mTarget->UpdateStream(mUpdateChunk);
+  return NS_OK;
+}
+
+NS_IMETHODIMP
+UrlClassifierDBServiceWorkerProxy::FinishStream()
+{
+  nsCOMPtr<nsIRunnable> r =
+    NS_NewRunnableMethod(mTarget,
+                         &nsIUrlClassifierDBServiceWorker::FinishStream);
+  return DispatchToWorkerThread(r);
+}
+
+NS_IMETHODIMP
+UrlClassifierDBServiceWorkerProxy::FinishUpdate()
+{
+  nsCOMPtr<nsIRunnable> r =
+    NS_NewRunnableMethod(mTarget,
+                         &nsIUrlClassifierDBServiceWorker::FinishUpdate);
+  return DispatchToWorkerThread(r);
+}
+
+NS_IMETHODIMP
+UrlClassifierDBServiceWorkerProxy::CancelUpdate()
+{
+  nsCOMPtr<nsIRunnable> r =
+    NS_NewRunnableMethod(mTarget,
+                         &nsIUrlClassifierDBServiceWorker::CancelUpdate);
+  return DispatchToWorkerThread(r);
+}
+
+NS_IMETHODIMP
+UrlClassifierDBServiceWorkerProxy::ResetDatabase()
+{
+  nsCOMPtr<nsIRunnable> r =
+    NS_NewRunnableMethod(mTarget,
+                         &nsIUrlClassifierDBServiceWorker::ResetDatabase);
+  return DispatchToWorkerThread(r);
+}
+
+NS_IMETHODIMP
+UrlClassifierDBServiceWorkerProxy::CloseDb()
+{
+  nsCOMPtr<nsIRunnable> r =
+    NS_NewRunnableMethod(mTarget,
+                         &nsIUrlClassifierDBServiceWorker::CloseDb);
+  return DispatchToWorkerThread(r);
+}
+
+NS_IMETHODIMP
+UrlClassifierDBServiceWorkerProxy::CacheCompletions(nsTArray<nsUrlClassifierLookupResult>* aEntries)
+{
+  nsCOMPtr<nsIRunnable> r = new CacheCompletionsRunnable(mTarget, aEntries);
+  return DispatchToWorkerThread(r);
+}
+
+NS_IMETHODIMP
+UrlClassifierDBServiceWorkerProxy::CacheCompletionsRunnable::Run()
+{
+  mTarget->CacheCompletions(mEntries);
+  return NS_OK;
+}
+
+NS_IMPL_THREADSAFE_ISUPPORTS1(UrlClassifierLookupCallbackProxy,
+                              nsIUrlClassifierLookupCallback)
+
+NS_IMETHODIMP
+UrlClassifierLookupCallbackProxy::LookupComplete
+  (nsTArray<nsUrlClassifierLookupResult>* aResults)
+{
+  nsCOMPtr<nsIRunnable> r = new LookupCompleteRunnable(mTarget, aResults);
+  return NS_DispatchToMainThread(r);
+}
+
+NS_IMETHODIMP
+UrlClassifierLookupCallbackProxy::LookupCompleteRunnable::Run()
+{
+  mTarget->LookupComplete(mResults);
+  return NS_OK;
+}
+
+NS_IMPL_THREADSAFE_ISUPPORTS1(UrlClassifierCallbackProxy,
+                              nsIUrlClassifierCallback)
+
+NS_IMETHODIMP
+UrlClassifierCallbackProxy::HandleEvent(const nsACString& aValue)
+{
+  nsCOMPtr<nsIRunnable> r = new HandleEventRunnable(mTarget, aValue);
+  return NS_DispatchToMainThread(r);
+}
+
+NS_IMETHODIMP
+UrlClassifierCallbackProxy::HandleEventRunnable::Run()
+{
+  mTarget->HandleEvent(mValue);
+  return NS_OK;
+}
+
+NS_IMPL_THREADSAFE_ISUPPORTS1(UrlClassifierUpdateObserverProxy,
+                              nsIUrlClassifierUpdateObserver)
+
+NS_IMETHODIMP
+UrlClassifierUpdateObserverProxy::UpdateUrlRequested
+  (const nsACString& aURL,
+   const nsACString& aTable,
+   const nsACString& aServerMAC)
+{
+  nsCOMPtr<nsIRunnable> r =
+    new UpdateUrlRequestedRunnable(mTarget, aURL, aTable, aServerMAC);
+  return NS_DispatchToMainThread(r);
+}
+
+NS_IMETHODIMP
+UrlClassifierUpdateObserverProxy::UpdateUrlRequestedRunnable::Run()
+{
+  mTarget->UpdateUrlRequested(mURL, mTable, mServerMAC);
+  return NS_OK;
+}
+
+NS_IMETHODIMP
+UrlClassifierUpdateObserverProxy::RekeyRequested()
+{
+  nsCOMPtr<nsIRunnable> r =
+    NS_NewRunnableMethod(mTarget, &nsIUrlClassifierUpdateObserver::RekeyRequested);
+  return NS_DispatchToMainThread(r);
+}
+
+NS_IMETHODIMP
+UrlClassifierUpdateObserverProxy::StreamFinished(nsresult aStatus,
+                                                 PRUint32 aDelay)
+{
+  nsCOMPtr<nsIRunnable> r =
+    new StreamFinishedRunnable(mTarget, aStatus, aDelay);
+  return NS_DispatchToMainThread(r);
+}
+
+NS_IMETHODIMP
+UrlClassifierUpdateObserverProxy::StreamFinishedRunnable::Run()
+{
+  mTarget->StreamFinished(mStatus, mDelay);
+  return NS_OK;
+}
+
+NS_IMETHODIMP
+UrlClassifierUpdateObserverProxy::UpdateError(nsresult aError)
+{
+  nsCOMPtr<nsIRunnable> r =
+    new UpdateErrorRunnable(mTarget, aError);
+  return NS_DispatchToMainThread(r);
+}
+
+NS_IMETHODIMP
+UrlClassifierUpdateObserverProxy::UpdateErrorRunnable::Run()
+{
+  mTarget->UpdateError(mError);
+  return NS_OK;
+}
+
+NS_IMETHODIMP
+UrlClassifierUpdateObserverProxy::UpdateSuccess(PRUint32 aRequestedTimeout)
+{
+  nsCOMPtr<nsIRunnable> r =
+    new UpdateSuccessRunnable(mTarget, aRequestedTimeout);
+  return NS_DispatchToMainThread(r);
+}
+
+NS_IMETHODIMP
+UrlClassifierUpdateObserverProxy::UpdateSuccessRunnable::Run()
+{
+  mTarget->UpdateSuccess(mRequestedTimeout);
+  return NS_OK;
+}
new file mode 100644
--- /dev/null
+++ b/toolkit/components/url-classifier/nsUrlClassifierProxies.h
@@ -0,0 +1,315 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* ***** 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 Firefox.
+ *
+ * The Initial Developer of the Original Code is
+ * the Mozilla Foundation <http://www.mozilla.org>.
+ * Portions created by the Initial Developer are Copyright (C) 2011
+ * 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 ***** */
+
+#ifndef nsUrlClassifierProxies_h
+#define nsUrlClassifierProxies_h
+
+#include "nsIUrlClassifierDBService.h"
+#include "nsThreadUtils.h"
+
+/**
+ * Thread proxy from the main thread to the worker thread.
+ */
+class UrlClassifierDBServiceWorkerProxy :
+  public nsIUrlClassifierDBServiceWorker
+{
+public:
+  UrlClassifierDBServiceWorkerProxy(nsIUrlClassifierDBServiceWorker* aTarget)
+    : mTarget(aTarget)
+  { }
+
+  NS_DECL_ISUPPORTS
+  NS_DECL_NSIURLCLASSIFIERDBSERVICE
+  NS_DECL_NSIURLCLASSIFIERDBSERVICEWORKER
+
+  class LookupRunnable : public nsRunnable
+  {
+  public:
+    LookupRunnable(nsIUrlClassifierDBServiceWorker* aTarget,
+                   const nsACString& aSpec,
+                   nsIUrlClassifierCallback* aCB)
+      : mTarget(aTarget)
+      , mSpec(aSpec)
+      , mCB(aCB)
+    { }
+
+    NS_DECL_NSIRUNNABLE
+
+  private:
+    nsCOMPtr<nsIUrlClassifierDBServiceWorker> mTarget;
+    nsCString mSpec;
+    nsCOMPtr<nsIUrlClassifierCallback> mCB;
+  };
+
+  class GetTablesRunnable : public nsRunnable
+  {
+  public:
+    GetTablesRunnable(nsIUrlClassifierDBServiceWorker* aTarget,
+                      nsIUrlClassifierCallback* aCB)
+      : mTarget(aTarget)
+      , mCB(aCB)
+    { }
+
+    NS_DECL_NSIRUNNABLE
+
+  private:
+    nsCOMPtr<nsIUrlClassifierDBServiceWorker> mTarget;
+    nsCOMPtr<nsIUrlClassifierCallback> mCB;
+  };
+
+  class BeginUpdateRunnable : public nsRunnable
+  {
+  public:
+    BeginUpdateRunnable(nsIUrlClassifierDBServiceWorker* aTarget,
+                        nsIUrlClassifierUpdateObserver* aUpdater,
+                        const nsACString& aTables,
+                        const nsACString& aClientKey)
+      : mTarget(aTarget)
+      , mUpdater(aUpdater)
+      , mTables(aTables)
+      , mClientKey(aClientKey)
+    { }
+
+    NS_DECL_NSIRUNNABLE
+
+  private:
+    nsCOMPtr<nsIUrlClassifierDBServiceWorker> mTarget;
+    nsCOMPtr<nsIUrlClassifierUpdateObserver> mUpdater;
+    nsCString mTables, mClientKey;
+  };
+
+  class BeginStreamRunnable : public nsRunnable
+  {
+  public:
+    BeginStreamRunnable(nsIUrlClassifierDBServiceWorker* aTarget,
+                        const nsACString& aTable,
+                        const nsACString& aServerMAC)
+      : mTarget(aTarget)
+      , mTable(aTable)
+      , mServerMAC(aServerMAC)
+    { }
+
+    NS_DECL_NSIRUNNABLE
+
+  private:
+    nsCOMPtr<nsIUrlClassifierDBServiceWorker> mTarget;
+    nsCString mTable, mServerMAC;
+  };
+
+  class UpdateStreamRunnable : public nsRunnable
+  {
+  public:
+    UpdateStreamRunnable(nsIUrlClassifierDBServiceWorker* aTarget,
+                         const nsACString& aUpdateChunk)
+      : mTarget(aTarget)
+      , mUpdateChunk(aUpdateChunk)
+    { }
+
+    NS_DECL_NSIRUNNABLE
+
+  private:
+    nsCOMPtr<nsIUrlClassifierDBServiceWorker> mTarget;
+    nsCString mUpdateChunk;
+  };
+
+  class CacheCompletionsRunnable : public nsRunnable
+  {
+  public:
+    CacheCompletionsRunnable(nsIUrlClassifierDBServiceWorker* aTarget,
+                             nsTArray<nsUrlClassifierLookupResult>* aEntries)
+      : mTarget(aTarget)
+      , mEntries(aEntries)
+    { }
+
+    NS_DECL_NSIRUNNABLE
+
+  private:
+    nsCOMPtr<nsIUrlClassifierDBServiceWorker> mTarget;
+    nsTArray<nsUrlClassifierLookupResult>* mEntries;
+  };
+
+private:
+  nsCOMPtr<nsIUrlClassifierDBServiceWorker> mTarget;
+};
+
+// The remaining classes here are all proxies to the main thread
+
+class UrlClassifierLookupCallbackProxy : public nsIUrlClassifierLookupCallback
+{
+public:
+  UrlClassifierLookupCallbackProxy(nsIUrlClassifierLookupCallback* aTarget)
+    : mTarget(aTarget)
+  { }
+
+  NS_DECL_ISUPPORTS
+  NS_DECL_NSIURLCLASSIFIERLOOKUPCALLBACK
+
+  class LookupCompleteRunnable : public nsRunnable
+  {
+  public:
+    LookupCompleteRunnable(nsIUrlClassifierLookupCallback* aTarget,
+                           nsTArray<nsUrlClassifierLookupResult>* aResults)
+      : mTarget(aTarget)
+      , mResults(aResults)
+    { }
+
+    NS_DECL_NSIRUNNABLE
+
+  private:
+    nsCOMPtr<nsIUrlClassifierLookupCallback> mTarget;
+    nsTArray<nsUrlClassifierLookupResult>* mResults;
+  };
+
+private:
+  nsCOMPtr<nsIUrlClassifierLookupCallback> mTarget;
+};
+
+class UrlClassifierCallbackProxy : public nsIUrlClassifierCallback
+{
+public:
+  UrlClassifierCallbackProxy(nsIUrlClassifierCallback* aTarget)
+    : mTarget(aTarget)
+  { }
+
+  NS_DECL_ISUPPORTS
+  NS_DECL_NSIURLCLASSIFIERCALLBACK
+
+  class HandleEventRunnable : public nsRunnable
+  {
+  public:
+    HandleEventRunnable(nsIUrlClassifierCallback* aTarget,
+                        const nsACString& aValue)
+      : mTarget(aTarget)
+      , mValue(aValue)
+    { }
+
+    NS_DECL_NSIRUNNABLE
+
+  private:
+    nsCOMPtr<nsIUrlClassifierCallback> mTarget;
+    nsCString mValue;
+  };
+
+private:
+  nsCOMPtr<nsIUrlClassifierCallback> mTarget;
+};
+
+class UrlClassifierUpdateObserverProxy : public nsIUrlClassifierUpdateObserver
+{
+public:
+  UrlClassifierUpdateObserverProxy(nsIUrlClassifierUpdateObserver* aTarget)
+    : mTarget(aTarget)
+  { }
+
+  NS_DECL_ISUPPORTS
+  NS_DECL_NSIURLCLASSIFIERUPDATEOBSERVER
+
+  class UpdateUrlRequestedRunnable : public nsRunnable
+  {
+  public:
+    UpdateUrlRequestedRunnable(nsIUrlClassifierUpdateObserver* aTarget,
+                               const nsACString& aURL,
+                               const nsACString& aTable,
+                               const nsACString& aServerMAC)
+      : mTarget(aTarget)
+      , mURL(aURL)
+      , mTable(aTable)
+      , mServerMAC(aServerMAC)
+    { }
+
+    NS_DECL_NSIRUNNABLE
+
+  private:
+    nsCOMPtr<nsIUrlClassifierUpdateObserver> mTarget;
+    nsCString mURL, mTable, mServerMAC;
+  };
+
+  class StreamFinishedRunnable : public nsRunnable
+  {
+  public:
+    StreamFinishedRunnable(nsIUrlClassifierUpdateObserver* aTarget,
+                           nsresult aStatus, PRUint32 aDelay)
+      : mTarget(aTarget)
+      , mStatus(aStatus)
+      , mDelay(aDelay)
+    { }
+
+    NS_DECL_NSIRUNNABLE
+
+  private:
+    nsCOMPtr<nsIUrlClassifierUpdateObserver> mTarget;
+    nsresult mStatus;
+    PRUint32 mDelay;
+  };
+
+  class UpdateErrorRunnable : public nsRunnable
+  {
+  public:
+    UpdateErrorRunnable(nsIUrlClassifierUpdateObserver* aTarget,
+                        nsresult aError)
+      : mTarget(aTarget)
+      , mError(aError)
+    { }
+
+    NS_DECL_NSIRUNNABLE
+
+  private:
+    nsCOMPtr<nsIUrlClassifierUpdateObserver> mTarget;
+    nsresult mError;
+  };
+
+  class UpdateSuccessRunnable : public nsRunnable
+  {
+  public:
+    UpdateSuccessRunnable(nsIUrlClassifierUpdateObserver* aTarget,
+                          PRUint32 aRequestedTimeout)
+      : mTarget(aTarget)
+      , mRequestedTimeout(aRequestedTimeout)
+    { }
+
+    NS_DECL_NSIRUNNABLE
+
+  private:
+    nsCOMPtr<nsIUrlClassifierUpdateObserver> mTarget;
+    PRUint32 mRequestedTimeout;
+  };
+
+private:
+  nsCOMPtr<nsIUrlClassifierUpdateObserver> mTarget;
+};
+
+#endif // nsUrlClassifierProxies_h