Back to index

lightning-sunbird  0.9+nobinonly
PrefObserver.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "StdAfx.h"
00039 
00040 #include <Wininet.h>
00041 
00042 #include "npapi.h"
00043 #include "nsServiceManagerUtils.h"
00044 #include "nsISupportsUtils.h"
00045 #include "nsIPrefService.h"
00046 #include "nsIPrefBranch2.h"
00047 #include "nsWeakReference.h"
00048 #include "nsIObserver.h"
00049 #include "nsCRT.h"
00050 #include "nsString.h"
00051 
00052 #include "XPConnect.h"
00053 
00054 // These are the default hosting flags in the absence of a pref.
00055 
00056 const PRUint32 kDefaultHostingFlags =
00057 #ifdef XPC_IDISPATCH_SUPPORT
00058     nsIActiveXSecurityPolicy::HOSTING_FLAGS_HOST_NOTHING;
00059 #else
00060     nsIActiveXSecurityPolicy::HOSTING_FLAGS_HOST_SAFE_OBJECTS |
00061     nsIActiveXSecurityPolicy::HOSTING_FLAGS_DOWNLOAD_CONTROLS |
00062     nsIActiveXSecurityPolicy::HOSTING_FLAGS_SCRIPT_SAFE_OBJECTS;
00063 #endif
00064 
00065 class PrefObserver :
00066     public nsSupportsWeakReference,
00067     public nsIObserver
00068 {
00069 public:
00070     PrefObserver();
00071 
00072 protected:
00073     virtual ~PrefObserver();
00074 
00075     void Sync(nsIPrefBranch *aPrefBranch);
00076     
00077     PRUint32 mHostingFlags;
00078     nsCOMPtr<nsIPrefBranch2> mPrefBranch;
00079 public:
00080     NS_DECL_ISUPPORTS
00081     NS_DECL_NSIOBSERVER
00082 
00083     static PrefObserver *sPrefObserver;
00084 
00085     nsresult Subscribe();
00086     nsresult Unsubscribe();
00087     PRUint32 GetHostingFlags() const;
00088 };
00089 
00090 const char *kActiveXHostingFlags = "security.xpconnect.activex.";
00091 const char *kUserAgentPref = "general.useragent.";
00092 const char *kProxyPref = "network.http.";
00093 
00094 PrefObserver *PrefObserver::sPrefObserver = nsnull;
00095 
00096 PrefObserver::PrefObserver() :
00097     mHostingFlags(kDefaultHostingFlags)
00098 {
00099     nsresult rv = NS_OK;
00100     mPrefBranch = do_GetService(NS_PREFSERVICE_CONTRACTID, &rv);
00101     NS_ASSERTION(mPrefBranch, "where is the pref service?");
00102 }
00103 
00104 PrefObserver::~PrefObserver()
00105 {
00106 }
00107 
00108 NS_IMPL_ADDREF(PrefObserver)
00109 NS_IMPL_RELEASE(PrefObserver)
00110 
00111 NS_INTERFACE_MAP_BEGIN(PrefObserver)
00112     NS_INTERFACE_MAP_ENTRY(nsIObserver)
00113     NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
00114 NS_INTERFACE_MAP_END
00115 
00116 /* void observe (in nsISupports aSubject, in string aTopic, in wstring aData); */
00117 NS_IMETHODIMP PrefObserver::Observe(nsISupports *aSubject, const char *aTopic, const PRUnichar *aData)
00118 {
00119     if (nsCRT::strcmp(NS_PREFBRANCH_PREFCHANGE_TOPIC_ID, aTopic) != 0)
00120     {
00121         return S_OK;
00122     }
00123 
00124     nsresult rv;
00125     nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(aSubject, &rv);
00126     if (NS_FAILED(rv))
00127         return rv;
00128 
00129     NS_ConvertUTF16toUTF8 pref(aData);
00130     if (nsCRT::strcmp(kActiveXHostingFlags, pref.get()) == 0 ||
00131         nsCRT::strcmp(kUserAgentPref, pref.get()) == 0 ||
00132         nsCRT::strcmp(kProxyPref, pref.get()) == 0)
00133     {
00134         Sync(prefBranch);
00135     }
00136     return NS_OK;
00137 }
00138 
00139 void PrefObserver::Sync(nsIPrefBranch *aPrefBranch)
00140 {
00141     NS_ASSERTION(aPrefBranch, "no pref branch");
00142     if (!aPrefBranch)
00143     {
00144         return;
00145     }
00146 
00147     // TODO
00148     // const char *userAgent = NPN_UserAgent(mData->pPluginInstance);
00149        // ::UrlMkSetSessionOption(URLMON_OPTION_USERAGENT, userAgent, strlen(userAgent), 0);      
00150 
00151     // TODO
00152     // INTERNET_PROXY_INFO ipi;
00153     // ::UrlMkSetSessionOption(INTERNET_OPTION_PROXY, ....);
00154 
00155     nsCOMPtr<nsIDispatchSupport> dispSupport = do_GetService(NS_IDISPATCH_SUPPORT_CONTRACTID);
00156     if (!dispSupport)
00157         mHostingFlags = kDefaultHostingFlags;
00158     else
00159         dispSupport->GetHostingFlags(nsnull, &mHostingFlags);
00160 }
00161 
00162 nsresult
00163 PrefObserver::Subscribe()
00164 {
00165     NS_ENSURE_TRUE(mPrefBranch, NS_ERROR_FAILURE);
00166 
00167     mPrefBranch->AddObserver(kProxyPref, this, PR_TRUE);
00168     mPrefBranch->AddObserver(kUserAgentPref, this, PR_TRUE);
00169     mPrefBranch->AddObserver(kActiveXHostingFlags, this, PR_TRUE);
00170 
00171     Sync(mPrefBranch);
00172 
00173     return S_OK;
00174 }
00175 
00176 nsresult
00177 PrefObserver::Unsubscribe()
00178 {
00179     NS_ENSURE_TRUE(mPrefBranch, NS_ERROR_FAILURE);
00180 
00181     mPrefBranch->RemoveObserver(kProxyPref, this);
00182     mPrefBranch->RemoveObserver(kUserAgentPref, this);
00183     mPrefBranch->RemoveObserver(kActiveXHostingFlags, this);
00184 
00185     return NS_OK;
00186 }
00187 
00188 PRUint32 PrefObserver::GetHostingFlags() const
00189 {
00190     return mHostingFlags;
00191 }
00192 
00194 
00195 PRUint32 MozAxPlugin::PrefGetHostingFlags()
00196 {
00197     if (!PrefObserver::sPrefObserver)
00198     {
00199         PrefObserver::sPrefObserver = new PrefObserver();
00200         if (!PrefObserver::sPrefObserver)
00201         {
00202             return nsIActiveXSecurityPolicy::HOSTING_FLAGS_HOST_NOTHING;
00203         }
00204         PrefObserver::sPrefObserver->AddRef();
00205         PrefObserver::sPrefObserver->Subscribe();
00206     }
00207     return PrefObserver::sPrefObserver->GetHostingFlags();
00208 }
00209 
00210 void MozAxPlugin::ReleasePrefObserver()
00211 {
00212     if (PrefObserver::sPrefObserver)
00213     {
00214         PrefObserver::sPrefObserver->Unsubscribe();
00215         PrefObserver::sPrefObserver->Release();
00216         PrefObserver::sPrefObserver = nsnull;
00217     }
00218 }
00219