Back to index

lightning-sunbird  0.9+nobinonly
nsDOMSerializer.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 "nsDOMSerializer.h"
00039 #include "nsIDOMNode.h"
00040 #include "nsIDOMClassInfo.h"
00041 #include "nsIOutputStream.h"
00042 #include "nsIContent.h"
00043 #include "nsIDocument.h"
00044 #include "nsIDOMDocument.h"
00045 #include "nsIDocumentEncoder.h"
00046 #include "nsIComponentManager.h"
00047 #include "nsIContentSerializer.h"
00048 #include "nsString.h"
00049 #include "nsReadableUtils.h"
00050 
00051 #include "nsIJSContextStack.h"
00052 #include "nsIScriptSecurityManager.h"
00053 #include "nsIURI.h"
00054 #include "nsContentUtils.h"
00055 
00056 nsDOMSerializer::nsDOMSerializer()
00057 {
00058 }
00059 
00060 nsDOMSerializer::~nsDOMSerializer()
00061 {
00062 }
00063 
00064 
00065 // QueryInterface implementation for nsDOMSerializer
00066 NS_INTERFACE_MAP_BEGIN(nsDOMSerializer)
00067   NS_INTERFACE_MAP_ENTRY(nsISupports)
00068   NS_INTERFACE_MAP_ENTRY(nsIDOMSerializer)
00069   NS_INTERFACE_MAP_ENTRY_CONTENT_CLASSINFO(XMLSerializer)
00070 NS_INTERFACE_MAP_END
00071 
00072 
00073 NS_IMPL_ADDREF(nsDOMSerializer)
00074 NS_IMPL_RELEASE(nsDOMSerializer)
00075 
00076 
00077 static nsresult
00078 SetUpEncoder(nsIDOMNode *aRoot, const nsACString& aCharset,
00079              nsIDocumentEncoder **aEncoder)
00080 {
00081   *aEncoder = nsnull;
00082    
00083   nsresult rv;
00084   nsCOMPtr<nsIDocumentEncoder> encoder =
00085     do_CreateInstance(NS_DOC_ENCODER_CONTRACTID_BASE "text/xml", &rv);
00086   if (NS_FAILED(rv))
00087     return rv;
00088 
00089   PRBool entireDocument = PR_TRUE;
00090   nsCOMPtr<nsIDocument> document(do_QueryInterface(aRoot));
00091   if (!document) {
00092     entireDocument = PR_FALSE;
00093     nsCOMPtr<nsIDOMDocument> domDoc;
00094     rv = aRoot->GetOwnerDocument(getter_AddRefs(domDoc));
00095     if (NS_FAILED(rv))
00096       return rv;
00097     document = do_QueryInterface(domDoc);
00098   }
00099 
00100   // This method will fail if no document
00101   rv = encoder->Init(document, NS_LITERAL_STRING("text/xml"),
00102                      nsIDocumentEncoder::OutputEncodeBasicEntities);
00103   if (NS_FAILED(rv))
00104     return rv;
00105 
00106   nsCAutoString charset(aCharset);
00107   if (charset.IsEmpty()) {
00108     charset = document->GetDocumentCharacterSet();
00109   }
00110   rv = encoder->SetCharset(charset);
00111   if (NS_FAILED(rv))
00112     return rv;
00113 
00114   // If we are working on the entire document we do not need to
00115   // specify which part to serialize
00116   if (!entireDocument) {
00117     rv = encoder->SetNode(aRoot);
00118   }
00119 
00120   if (NS_SUCCEEDED(rv)) {
00121     *aEncoder = encoder.get();
00122     NS_ADDREF(*aEncoder);
00123   }
00124 
00125   return rv;
00126 }
00127 
00128 static nsresult
00129 CheckSameOrigin(nsIDOMNode *aRoot)
00130 {
00131   // Make sure that the caller has permission to access the root
00132 
00133   // Be sure to QI to either nsIContent or nsIDocument to make sure
00134   // we're passed a naitve object.
00135 
00136   nsCOMPtr<nsIContent> content(do_QueryInterface(aRoot));
00137   nsCOMPtr<nsIDocument> doc;
00138 
00139   if (content) {
00140     doc = content->GetOwnerDoc();
00141 
00142     if (!doc) {
00143       // Orphan node, permit access.
00144 
00145       return NS_OK;
00146     }
00147   } else {
00148     doc = do_QueryInterface(aRoot);
00149 
00150     if (!doc) {
00151       // We got a non-native object.
00152 
00153       return NS_ERROR_INVALID_POINTER;
00154     }
00155   }
00156 
00157   nsIPrincipal *principal = doc->GetPrincipal();
00158 
00159   if (principal) {
00160     nsresult rv;
00161     nsCOMPtr<nsIScriptSecurityManager> secMan = 
00162       do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID, &rv);
00163     NS_ENSURE_SUCCESS(rv, rv);
00164 
00165     PRBool ubrEnabled = PR_FALSE;
00166     rv = secMan->IsCapabilityEnabled("UniversalBrowserRead", &ubrEnabled);
00167     NS_ENSURE_SUCCESS(rv, rv);
00168 
00169     if (ubrEnabled) {
00170       // UniversalBrowserRead is enabled (or we're not called from
00171       // script), permit access.
00172       return NS_OK;
00173     }
00174 
00175     nsCOMPtr<nsIPrincipal> subject;
00176     rv = secMan->GetSubjectPrincipal(getter_AddRefs(subject));
00177     NS_ENSURE_SUCCESS(rv, rv);
00178 
00179     // Since UBR is not enabled, we better have a subject principal,
00180     // but just in case...
00181     if (subject) {
00182       // Check if the caller is from the same origin that the root is from.
00183       return secMan->CheckSameOriginPrincipal(subject, principal);
00184     }
00185   }
00186 
00187   return NS_OK;
00188 }
00189 
00190 NS_IMETHODIMP
00191 nsDOMSerializer::SerializeToString(nsIDOMNode *aRoot, nsAString& _retval)
00192 {
00193   NS_ENSURE_ARG_POINTER(aRoot);
00194   
00195   _retval.Truncate();
00196 
00197   nsresult rv = CheckSameOrigin(aRoot);
00198   if (NS_FAILED(rv))
00199     return rv;
00200 
00201   nsCOMPtr<nsIDocumentEncoder> encoder;
00202   rv = SetUpEncoder(aRoot, EmptyCString(), getter_AddRefs(encoder));
00203   if (NS_FAILED(rv))
00204     return rv;
00205 
00206   return encoder->EncodeToString(_retval);
00207 }
00208 
00209 NS_IMETHODIMP
00210 nsDOMSerializer::SerializeToStream(nsIDOMNode *aRoot, 
00211                                    nsIOutputStream *aStream, 
00212                                    const nsACString& aCharset)
00213 {
00214   NS_ENSURE_ARG_POINTER(aRoot);
00215   NS_ENSURE_ARG_POINTER(aStream);
00216   // The charset arg can be null, in which case we get the document's
00217   // charset and use that when serializing.
00218 
00219   nsresult rv = CheckSameOrigin(aRoot);
00220   if (NS_FAILED(rv))
00221     return rv;
00222 
00223   nsCOMPtr<nsIDocumentEncoder> encoder;
00224   rv = SetUpEncoder(aRoot, aCharset, getter_AddRefs(encoder));
00225   if (NS_FAILED(rv))
00226     return rv;
00227 
00228   return encoder->EncodeToStream(aStream);
00229 }