gfx/thebes/gfxDWriteCommon.h

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

     1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
     2  * This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 #ifndef GFX_DWRITECOMMON_H
     7 #define GFX_DWRITECOMMON_H
     9 // Mozilla includes
    10 #include "nscore.h"
    11 #include "nsIServiceManager.h"
    12 #include "nsCOMPtr.h"
    13 #include "nsAutoPtr.h"
    14 #include "cairo-features.h"
    15 #include "gfxFontConstants.h"
    16 #include "nsTArray.h"
    17 #include "gfxWindowsPlatform.h"
    18 #include "nsIUUIDGenerator.h"
    20 #include <windows.h>
    21 #include <dwrite.h>
    23 static DWRITE_FONT_STRETCH
    24 DWriteFontStretchFromStretch(int16_t aStretch) 
    25 {
    26     switch (aStretch) {
    27         case NS_FONT_STRETCH_ULTRA_CONDENSED:
    28             return DWRITE_FONT_STRETCH_ULTRA_CONDENSED;
    29         case NS_FONT_STRETCH_EXTRA_CONDENSED:
    30             return DWRITE_FONT_STRETCH_EXTRA_CONDENSED;
    31         case NS_FONT_STRETCH_CONDENSED:
    32             return DWRITE_FONT_STRETCH_CONDENSED;
    33         case NS_FONT_STRETCH_SEMI_CONDENSED:
    34             return DWRITE_FONT_STRETCH_SEMI_CONDENSED;
    35         case NS_FONT_STRETCH_NORMAL:
    36             return DWRITE_FONT_STRETCH_NORMAL;
    37         case NS_FONT_STRETCH_SEMI_EXPANDED:
    38             return DWRITE_FONT_STRETCH_SEMI_EXPANDED;
    39         case NS_FONT_STRETCH_EXPANDED:
    40             return DWRITE_FONT_STRETCH_EXPANDED;
    41         case NS_FONT_STRETCH_EXTRA_EXPANDED:
    42             return DWRITE_FONT_STRETCH_EXTRA_EXPANDED;
    43         case NS_FONT_STRETCH_ULTRA_EXPANDED:
    44             return DWRITE_FONT_STRETCH_ULTRA_EXPANDED;
    45         default:
    46             return DWRITE_FONT_STRETCH_UNDEFINED;
    47     }
    48 }
    50 static int16_t
    51 FontStretchFromDWriteStretch(DWRITE_FONT_STRETCH aStretch) 
    52 {
    53     switch (aStretch) {
    54         case DWRITE_FONT_STRETCH_ULTRA_CONDENSED:
    55             return NS_FONT_STRETCH_ULTRA_CONDENSED;
    56         case DWRITE_FONT_STRETCH_EXTRA_CONDENSED:
    57             return NS_FONT_STRETCH_EXTRA_CONDENSED;
    58         case DWRITE_FONT_STRETCH_CONDENSED:
    59             return NS_FONT_STRETCH_CONDENSED;
    60         case DWRITE_FONT_STRETCH_SEMI_CONDENSED:
    61             return NS_FONT_STRETCH_SEMI_CONDENSED;
    62         case DWRITE_FONT_STRETCH_NORMAL:
    63             return NS_FONT_STRETCH_NORMAL;
    64         case DWRITE_FONT_STRETCH_SEMI_EXPANDED:
    65             return NS_FONT_STRETCH_SEMI_EXPANDED;
    66         case DWRITE_FONT_STRETCH_EXPANDED:
    67             return NS_FONT_STRETCH_EXPANDED;
    68         case DWRITE_FONT_STRETCH_EXTRA_EXPANDED:
    69             return NS_FONT_STRETCH_EXTRA_EXPANDED;
    70         case DWRITE_FONT_STRETCH_ULTRA_EXPANDED:
    71             return NS_FONT_STRETCH_ULTRA_EXPANDED;
    72         default:
    73             return NS_FONT_STRETCH_NORMAL;
    74     }
    75 }
    77 struct ffReferenceKey
    78 {
    79     FallibleTArray<uint8_t> *mArray;
    80     nsID mGUID;
    81 };
    83 class gfxDWriteFontFileLoader : public IDWriteFontFileLoader
    84 {
    85 public:
    86     gfxDWriteFontFileLoader()
    87     {
    88     }
    90     // IUnknown interface
    91     IFACEMETHOD(QueryInterface)(IID const& iid, OUT void** ppObject)
    92     {
    93         if (iid == __uuidof(IDWriteFontFileLoader)) {
    94             *ppObject = static_cast<IDWriteFontFileLoader*>(this);
    95             return S_OK;
    96         } else if (iid == __uuidof(IUnknown)) {
    97             *ppObject = static_cast<IUnknown*>(this);
    98             return S_OK;
    99         } else {
   100             return E_NOINTERFACE;
   101         }
   102     }
   104     IFACEMETHOD_(ULONG, AddRef)()
   105     {
   106         return 1;
   107     }
   109     IFACEMETHOD_(ULONG, Release)()
   110     {
   111         return 1;
   112     }
   114     // IDWriteFontFileLoader methods
   115     /**
   116      * Important! Note the key here -has- to be a pointer to an
   117      * FallibleTArray<uint8_t>.
   118      */
   119     virtual HRESULT STDMETHODCALLTYPE 
   120         CreateStreamFromKey(void const* fontFileReferenceKey,
   121                             UINT32 fontFileReferenceKeySize,
   122                             OUT IDWriteFontFileStream** fontFileStream);
   124     /**
   125      * Gets the singleton loader instance. Note that when using this font
   126      * loader, the key must be a pointer to an FallibleTArray<uint8_t>. This
   127      * array will be empty when the function returns.
   128      */
   129     static IDWriteFontFileLoader* Instance()
   130     {
   131         if (!mInstance) {
   132             mInstance = new gfxDWriteFontFileLoader();
   133             gfxWindowsPlatform::GetPlatform()->GetDWriteFactory()->
   134                 RegisterFontFileLoader(mInstance);
   135         }
   136         return mInstance;
   137     }
   139 private:
   140     static IDWriteFontFileLoader* mInstance;
   141 }; 
   143 class gfxDWriteFontFileStream MOZ_FINAL : public IDWriteFontFileStream
   144 {
   145 public:
   146     /**
   147      * Used by the FontFileLoader to create a new font stream,
   148      * this font stream is created from data in memory. The memory
   149      * passed may be released after object creation, it will be
   150      * copied internally.
   151      *
   152      * @param aData Font data
   153      */
   154     gfxDWriteFontFileStream(FallibleTArray<uint8_t> *aData);
   155     ~gfxDWriteFontFileStream();
   157     // IUnknown interface
   158     IFACEMETHOD(QueryInterface)(IID const& iid, OUT void** ppObject)
   159     {
   160         if (iid == __uuidof(IDWriteFontFileStream)) {
   161             *ppObject = static_cast<IDWriteFontFileStream*>(this);
   162             return S_OK;
   163         } else if (iid == __uuidof(IUnknown)) {
   164             *ppObject = static_cast<IUnknown*>(this);
   165             return S_OK;
   166         } else {
   167             return E_NOINTERFACE;
   168         }
   169     }
   171     IFACEMETHOD_(ULONG, AddRef)()
   172     {
   173         NS_PRECONDITION(int32_t(mRefCnt) >= 0, "illegal refcnt");
   174         ++mRefCnt;
   175         return mRefCnt;
   176     }
   178     IFACEMETHOD_(ULONG, Release)()
   179     {
   180         NS_PRECONDITION(0 != mRefCnt, "dup release");
   181         --mRefCnt;
   182         if (mRefCnt == 0) {
   183             delete this;
   184             return 0;
   185         }
   186         return mRefCnt;
   187     }
   189     // IDWriteFontFileStream methods
   190     virtual HRESULT STDMETHODCALLTYPE ReadFileFragment(void const** fragmentStart,
   191                                                        UINT64 fileOffset,
   192                                                        UINT64 fragmentSize,
   193                                                        OUT void** fragmentContext);
   195     virtual void STDMETHODCALLTYPE ReleaseFileFragment(void* fragmentContext);
   197     virtual HRESULT STDMETHODCALLTYPE GetFileSize(OUT UINT64* fileSize);
   199     virtual HRESULT STDMETHODCALLTYPE GetLastWriteTime(OUT UINT64* lastWriteTime);
   201 private:
   202     FallibleTArray<uint8_t> mData;
   203     nsAutoRefCnt mRefCnt;
   204 }; 
   206 #endif /* GFX_DWRITECOMMON_H */

mercurial