Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/layout/style/PreloadedStyleSheet.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
/* a CSS style sheet returned from nsIStyleSheetService.preloadSheet */
8
9
#include "PreloadedStyleSheet.h"
10
11
#include "mozilla/css/Loader.h"
12
#include "mozilla/dom/Promise.h"
13
#include "nsICSSLoaderObserver.h"
14
#include "nsLayoutUtils.h"
15
16
namespace mozilla {
17
18
PreloadedStyleSheet::PreloadedStyleSheet(nsIURI* aURI,
19
                                         css::SheetParsingMode aParsingMode)
20
  : mLoaded(false)
21
  , mURI(aURI)
22
  , mParsingMode(aParsingMode)
23
0
{
24
0
}
25
26
/* static */ nsresult
27
PreloadedStyleSheet::Create(nsIURI* aURI,
28
                            css::SheetParsingMode aParsingMode,
29
                            PreloadedStyleSheet** aResult)
30
0
{
31
0
  *aResult = nullptr;
32
0
33
0
  RefPtr<PreloadedStyleSheet> preloadedSheet =
34
0
    new PreloadedStyleSheet(aURI, aParsingMode);
35
0
36
0
  preloadedSheet.forget(aResult);
37
0
  return NS_OK;
38
0
}
39
40
0
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(PreloadedStyleSheet)
41
0
  NS_INTERFACE_MAP_ENTRY(nsIPreloadedStyleSheet)
42
0
  NS_INTERFACE_MAP_ENTRY(nsISupports)
43
0
NS_INTERFACE_MAP_END
44
45
NS_IMPL_CYCLE_COLLECTING_ADDREF(PreloadedStyleSheet)
46
NS_IMPL_CYCLE_COLLECTING_RELEASE(PreloadedStyleSheet)
47
48
NS_IMPL_CYCLE_COLLECTION(PreloadedStyleSheet, mSheet)
49
50
nsresult
51
PreloadedStyleSheet::GetSheet(StyleSheet** aResult)
52
0
{
53
0
  *aResult = nullptr;
54
0
55
0
  MOZ_DIAGNOSTIC_ASSERT(mLoaded);
56
0
57
0
  if (!mSheet) {
58
0
    RefPtr<css::Loader> loader = new css::Loader;
59
0
    nsresult rv = loader->LoadSheetSync(mURI, mParsingMode, true, &mSheet);
60
0
    NS_ENSURE_SUCCESS(rv, rv);
61
0
    MOZ_ASSERT(mSheet);
62
0
  }
63
0
64
0
  *aResult = mSheet;
65
0
  return NS_OK;
66
0
}
67
68
nsresult
69
PreloadedStyleSheet::Preload()
70
0
{
71
0
  MOZ_DIAGNOSTIC_ASSERT(!mLoaded);
72
0
73
0
  // The nsIStyleSheetService.preloadSheet API doesn't tell us which backend
74
0
  // the sheet will be used with, and it seems wasteful to eagerly create
75
0
  // both a CSSStyleSheet and a ServoStyleSheet.  So instead, we guess that
76
0
  // the sheet type we will want matches the current value of the stylo pref,
77
0
  // and preload a sheet of that type.
78
0
  //
79
0
  // If we guess wrong, we will re-load the sheet later with the requested type,
80
0
  // and we won't really have front loaded the loading time as the name
81
0
  // "preload" might suggest.  Also, in theory we could get different data from
82
0
  // fetching the URL again, but for the usage patterns of this API this is
83
0
  // unlikely, and it doesn't seem worth trying to store the contents of the URL
84
0
  // and duplicating a bunch of css::Loader's logic.
85
0
  mLoaded = true;
86
0
87
0
  StyleSheet* sheet;
88
0
  return GetSheet(&sheet);
89
0
}
90
91
NS_IMPL_ISUPPORTS(PreloadedStyleSheet::StylesheetPreloadObserver,
92
                  nsICSSLoaderObserver)
93
94
NS_IMETHODIMP
95
PreloadedStyleSheet::StylesheetPreloadObserver::StyleSheetLoaded(
96
  StyleSheet* aSheet, bool aWasDeferred, nsresult aStatus)
97
0
{
98
0
  MOZ_DIAGNOSTIC_ASSERT(!mPreloadedSheet->mLoaded);
99
0
  mPreloadedSheet->mLoaded = true;
100
0
101
0
  if (NS_FAILED(aStatus)) {
102
0
    mPromise->MaybeReject(aStatus);
103
0
  } else {
104
0
    mPromise->MaybeResolve(mPreloadedSheet);
105
0
  }
106
0
107
0
  return NS_OK;
108
0
}
109
110
// Note: After calling this method, the preloaded sheet *must not* be used
111
// until the observer is notified that the sheet has finished loading.
112
nsresult
113
PreloadedStyleSheet::PreloadAsync(NotNull<dom::Promise*> aPromise)
114
0
{
115
0
  MOZ_DIAGNOSTIC_ASSERT(!mLoaded);
116
0
117
0
  RefPtr<css::Loader> loader = new css::Loader;
118
0
119
0
  RefPtr<StylesheetPreloadObserver> obs =
120
0
    new StylesheetPreloadObserver(aPromise, this);
121
0
122
0
  return loader->LoadSheet(mURI, mParsingMode, false, obs, &mSheet);
123
0
}
124
125
} // namespace mozilla