LCOV - app.info - content/xbl/src/nsXBLResourceLoader.cpp

LCOV - code coverage report
| Current view: | directory - content/xbl/src - nsXBLResourceLoader.cpp (source / functions) |  | Found | Hit | Coverage |
| Test: | app.info | Lines: | 118 | 2 | 1.7 %
| Date: | 2012-04-21 | Functions: | 17 | 2 | 11.8 %

           1                 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
           2                 : /* ***** BEGIN LICENSE BLOCK *****
           3                 :  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
           4                 :  *
           5                 :  * The contents of this file are subject to the Mozilla Public License Version
           6                 :  * 1.1 (the "License"); you may not use this file except in compliance with
           7                 :  * the License. You may obtain a copy of the License at
           8                 :  * http://www.mozilla.org/MPL/
           9                 :  *
          10                 :  * Software distributed under the License is distributed on an "AS IS" basis,
          11                 :  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
          12                 :  * for the specific language governing rights and limitations under the
          13                 :  * License.
          14                 :  *
          15                 :  * The Original Code is Mozilla Communicator client code.
          16                 :  *
          17                 :  * The Initial Developer of the Original Code is
          18                 :  * Netscape Communications Corporation.
          19                 :  * Portions created by the Initial Developer are Copyright (C) 1998
          20                 :  * the Initial Developer. All Rights Reserved.
          21                 :  *
          22                 :  * Contributor(s):
          23                 :  *   Original Author: David W. Hyatt (hyatt@netscape.com)
          24                 :  *
          25                 :  * Alternatively, the contents of this file may be used under the terms of
          26                 :  * either of the GNU General Public License Version 2 or later (the "GPL"),
          27                 :  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
          28                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
          29                 :  * of those above. If you wish to allow use of your version of this file only
          30                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
          31                 :  * use your version of this file under the terms of the MPL, indicate your
          32                 :  * decision by deleting the provisions above and replace them with the notice
          33                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
          34                 :  * the provisions above, a recipient may use your version of this file under
          35                 :  * the terms of any one of the MPL, the GPL or the LGPL.
          36                 :  *
          37                 :  * ***** END LICENSE BLOCK ***** */
          38                 :
          39                 : #include "nsTArray.h"
          40                 : #include "nsString.h"
          41                 : #include "nsCSSStyleSheet.h"
          42                 : #include "nsIStyleRuleProcessor.h"
          43                 : #include "nsIDocument.h"
          44                 : #include "nsIContent.h"
          45                 : #include "nsIPresShell.h"
          46                 : #include "nsIXBLService.h"
          47                 : #include "nsIServiceManager.h"
          48                 : #include "nsXBLResourceLoader.h"
          49                 : #include "nsXBLPrototypeResources.h"
          50                 : #include "nsIDocumentObserver.h"
          51                 : #include "imgILoader.h"
          52                 : #include "imgIRequest.h"
          53                 : #include "mozilla/css/Loader.h"
          54                 : #include "nsIURI.h"
          55                 : #include "nsNetUtil.h"
          56                 : #include "nsGkAtoms.h"
          57                 : #include "nsFrameManager.h"
          58                 : #include "nsStyleContext.h"
          59                 : #include "nsXBLPrototypeBinding.h"
          60                 : #include "nsCSSRuleProcessor.h"
          61                 : #include "nsContentUtils.h"
          62                 : #include "nsStyleSet.h"
          63                 : #include "nsIScriptSecurityManager.h"
          64                 :
          65            1396 : NS_IMPL_CYCLE_COLLECTION_CLASS(nsXBLResourceLoader)
          66               0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsXBLResourceLoader)
          67               0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMARRAY(mBoundElements)
          68               0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_END
          69               0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsXBLResourceLoader)
          70               0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMARRAY(mBoundElements)
          71               0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
          72                 :
          73               0 : NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsXBLResourceLoader)
          74               0 :   NS_INTERFACE_MAP_ENTRY(nsICSSLoaderObserver)
          75               0 :   NS_INTERFACE_MAP_ENTRY(nsISupports)
          76               0 : NS_INTERFACE_MAP_END
          77                 :
          78               0 : NS_IMPL_CYCLE_COLLECTING_ADDREF(nsXBLResourceLoader)
          79               0 : NS_IMPL_CYCLE_COLLECTING_RELEASE(nsXBLResourceLoader)
          80                 :
          81               0 : nsXBLResourceLoader::nsXBLResourceLoader(nsXBLPrototypeBinding* aBinding,
          82                 :                                          nsXBLPrototypeResources* aResources)
          83                 : :mBinding(aBinding),
          84                 :  mResources(aResources),
          85                 :  mResourceList(nsnull),
          86                 :  mLastResource(nsnull),
          87                 :  mLoadingResources(false),
          88                 :  mInLoadResourcesFunc(false),
          89               0 :  mPendingSheets(0)
          90                 : {
          91               0 : }
          92                 :
          93               0 : nsXBLResourceLoader::~nsXBLResourceLoader()
          94                 : {
          95               0 :   delete mResourceList;
          96               0 : }
          97                 :
          98                 : void
          99               0 : nsXBLResourceLoader::LoadResources(bool* aResult)
         100                 : {
         101               0 :   mInLoadResourcesFunc = true;
         102                 :
         103               0 :   if (mLoadingResources) {
         104               0 :     *aResult = (mPendingSheets == 0);
         105               0 :     mInLoadResourcesFunc = false;
         106               0 :     return;
         107                 :   }
         108                 :
         109               0 :   mLoadingResources = true;
         110               0 :   *aResult = true;
         111                 :
         112                 :   // Declare our loaders.
         113               0 :   nsCOMPtr<nsIDocument> doc = mBinding->XBLDocumentInfo()->GetDocument();
         114                 :
         115               0 :   mozilla::css::Loader* cssLoader = doc->CSSLoader();
         116               0 :   nsIURI *docURL = doc->GetDocumentURI();
         117               0 :   nsIPrincipal* docPrincipal = doc->NodePrincipal();
         118                 :
         119               0 :   nsCOMPtr<nsIURI> url;
         120                 :
         121               0 :   for (nsXBLResource* curr = mResourceList; curr; curr = curr->mNext) {
         122               0 :     if (curr->mSrc.IsEmpty())
         123               0 :       continue;
         124                 :
         125               0 :     if (NS_FAILED(NS_NewURI(getter_AddRefs(url), curr->mSrc,
         126                 :                             doc->GetDocumentCharacterSet().get(), docURL)))
         127               0 :       continue;
         128                 :
         129               0 :     if (curr->mType == nsGkAtoms::image) {
         130               0 :       if (!nsContentUtils::CanLoadImage(url, doc, doc, docPrincipal)) {
         131                 :         // We're not permitted to load this image, move on...
         132               0 :         continue;
         133                 :       }
         134                 :
         135                 :       // Now kick off the image load...
         136                 :       // Passing NULL for pretty much everything -- cause we don't care!
         137                 :       // XXX: initialDocumentURI is NULL!
         138               0 :       nsCOMPtr<imgIRequest> req;
         139                 :       nsContentUtils::LoadImage(url, doc, docPrincipal, docURL, nsnull,
         140                 :                                 nsIRequest::LOAD_BACKGROUND,
         141               0 :                                 getter_AddRefs(req));
         142                 :     }
         143               0 :     else if (curr->mType == nsGkAtoms::stylesheet) {
         144                 :       // Kick off the load of the stylesheet.
         145                 :
         146                 :       // Always load chrome synchronously
         147                 :       // XXXbz should that still do a content policy check?
         148                 :       bool chrome;
         149                 :       nsresult rv;
         150               0 :       if (NS_SUCCEEDED(url->SchemeIs("chrome", &chrome)) && chrome)
         151                 :       {
         152               0 :         rv = nsContentUtils::GetSecurityManager()->
         153                 :           CheckLoadURIWithPrincipal(docPrincipal, url,
         154               0 :                                     nsIScriptSecurityManager::ALLOW_CHROME);
         155               0 :         if (NS_SUCCEEDED(rv)) {
         156               0 :           nsRefPtr<nsCSSStyleSheet> sheet;
         157               0 :           rv = cssLoader->LoadSheetSync(url, getter_AddRefs(sheet));
         158               0 :           NS_ASSERTION(NS_SUCCEEDED(rv), "Load failed!!!");
         159               0 :           if (NS_SUCCEEDED(rv))
         160                 :           {
         161               0 :             rv = StyleSheetLoaded(sheet, false, NS_OK);
         162               0 :             NS_ASSERTION(NS_SUCCEEDED(rv), "Processing the style sheet failed!!!");
         163                 :           }
         164                 :         }
         165                 :       }
         166                 :       else
         167                 :       {
         168               0 :         rv = cssLoader->LoadSheet(url, docPrincipal, EmptyCString(), this);
         169               0 :         if (NS_SUCCEEDED(rv))
         170               0 :           ++mPendingSheets;
         171                 :       }
         172                 :     }
         173                 :   }
         174                 :
         175               0 :   *aResult = (mPendingSheets == 0);
         176               0 :   mInLoadResourcesFunc = false;
         177                 :
         178                 :   // Destroy our resource list.
         179               0 :   delete mResourceList;
         180               0 :   mResourceList = nsnull;
         181                 : }
         182                 :
         183                 : // nsICSSLoaderObserver
         184                 : NS_IMETHODIMP
         185               0 : nsXBLResourceLoader::StyleSheetLoaded(nsCSSStyleSheet* aSheet,
         186                 :                                       bool aWasAlternate,
         187                 :                                       nsresult aStatus)
         188                 : {
         189               0 :   if (!mResources) {
         190                 :     // Our resources got destroyed -- just bail out
         191               0 :     return NS_OK;
         192                 :   }
         193                 :
         194               0 :   mResources->mStyleSheetList.AppendElement(aSheet);
         195                 :
         196               0 :   if (!mInLoadResourcesFunc)
         197               0 :     mPendingSheets--;
         198                 :
         199               0 :   if (mPendingSheets == 0) {
         200                 :     // All stylesheets are loaded.
         201                 :     mResources->mRuleProcessor =
         202                 :       new nsCSSRuleProcessor(mResources->mStyleSheetList,
         203               0 :                              nsStyleSet::eDocSheet);
         204                 :
         205                 :     // XXX Check for mPendingScripts when scripts also come online.
         206               0 :     if (!mInLoadResourcesFunc)
         207               0 :       NotifyBoundElements();
         208                 :   }
         209               0 :   return NS_OK;
         210                 : }
         211                 :
         212                 : void
         213               0 : nsXBLResourceLoader::AddResource(nsIAtom* aResourceType, const nsAString& aSrc)
         214                 : {
         215               0 :   nsXBLResource* res = new nsXBLResource(aResourceType, aSrc);
         216               0 :   if (!res)
         217               0 :     return;
         218                 :
         219               0 :   if (!mResourceList)
         220               0 :     mResourceList = res;
         221                 :   else
         222               0 :     mLastResource->mNext = res;
         223                 :
         224               0 :   mLastResource = res;
         225                 : }
         226                 :
         227                 : void
         228               0 : nsXBLResourceLoader::AddResourceListener(nsIContent* aBoundElement)
         229                 : {
         230               0 :   if (aBoundElement) {
         231               0 :     mBoundElements.AppendObject(aBoundElement);
         232                 :   }
         233               0 : }
         234                 :
         235                 : void
         236               0 : nsXBLResourceLoader::NotifyBoundElements()
         237                 : {
         238               0 :   nsCOMPtr<nsIXBLService> xblService(do_GetService("@mozilla.org/xbl;1"));
         239               0 :   nsIURI* bindingURI = mBinding->BindingURI();
         240                 :
         241               0 :   PRUint32 eltCount = mBoundElements.Count();
         242               0 :   for (PRUint32 j = 0; j < eltCount; j++) {
         243               0 :     nsCOMPtr<nsIContent> content = mBoundElements.ObjectAt(j);
         244                 :
         245               0 :     bool ready = false;
         246               0 :     xblService->BindingReady(content, bindingURI, &ready);
         247                 :
         248               0 :     if (ready) {
         249                 :       // We need the document to flush out frame construction and
         250                 :       // such, so we want to use the current document.
         251               0 :       nsIDocument* doc = content->GetCurrentDoc();
         252                 :
         253               0 :       if (doc) {
         254                 :         // Flush first to make sure we can get the frame for content
         255               0 :         doc->FlushPendingNotifications(Flush_Frames);
         256                 :
         257                 :         // If |content| is (in addition to having binding |mBinding|)
         258                 :         // also a descendant of another element with binding |mBinding|,
         259                 :         // then we might have just constructed it due to the
         260                 :         // notification of its parent.  (We can know about both if the
         261                 :         // binding loads were triggered from the DOM rather than frame
         262                 :         // construction.)  So we have to check both whether the element
         263                 :         // has a primary frame and whether it's in the undisplayed map
         264                 :         // before sending a ContentInserted notification, or bad things
         265                 :         // will happen.
         266               0 :         nsIPresShell *shell = doc->GetShell();
         267               0 :         if (shell) {
         268               0 :           nsIFrame* childFrame = content->GetPrimaryFrame();
         269               0 :           if (!childFrame) {
         270                 :             // Check to see if it's in the undisplayed content map.
         271                 :             nsStyleContext* sc =
         272               0 :               shell->FrameManager()->GetUndisplayedContent(content);
         273                 :
         274               0 :             if (!sc) {
         275               0 :               shell->RecreateFramesFor(content);
         276                 :             }
         277                 :           }
         278                 :         }
         279                 :
         280                 :         // Flush again
         281                 :         // XXXbz why is this needed?
         282               0 :         doc->FlushPendingNotifications(Flush_ContentAndNotify);
         283                 :       }
         284                 :     }
         285                 :   }
         286                 :
         287                 :   // Clear out the whole array.
         288               0 :   mBoundElements.Clear();
         289                 :
         290                 :   // Delete ourselves.
         291               0 :   NS_RELEASE(mResources->mLoader);
         292               0 : }
         293                 :
         294                 : nsresult
         295               0 : nsXBLResourceLoader::Write(nsIObjectOutputStream* aStream)
         296                 : {
         297                 :   nsresult rv;
         298                 :
         299               0 :   for (nsXBLResource* curr = mResourceList; curr; curr = curr->mNext) {
         300               0 :     if (curr->mType == nsGkAtoms::image)
         301               0 :       rv = aStream->Write8(XBLBinding_Serialize_Image);
         302               0 :     else if (curr->mType == nsGkAtoms::stylesheet)
         303               0 :       rv = aStream->Write8(XBLBinding_Serialize_Stylesheet);
         304                 :     else
         305               0 :       continue;
         306                 :
         307               0 :     NS_ENSURE_SUCCESS(rv, rv);
         308                 :
         309               0 :     rv = aStream->WriteWStringZ(curr->mSrc.get());
         310               0 :     NS_ENSURE_SUCCESS(rv, rv);
         311                 :   }
         312                 :
         313               0 :   return NS_OK;
         314            4188 : }

Generated by: LCOV version 1.7