Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/image/ImageFactory.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2
 *
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
#include "ImageFactory.h"
8
9
#include <algorithm>
10
11
#include "mozilla/Likely.h"
12
13
#include "nsIHttpChannel.h"
14
#include "nsIFileChannel.h"
15
#include "nsIFile.h"
16
#include "nsMimeTypes.h"
17
#include "nsIRequest.h"
18
19
#include "MultipartImage.h"
20
#include "RasterImage.h"
21
#include "VectorImage.h"
22
#include "Image.h"
23
#include "nsMediaFragmentURIParser.h"
24
#include "nsContentUtils.h"
25
#include "nsIScriptSecurityManager.h"
26
27
#include "gfxPrefs.h"
28
29
namespace mozilla {
30
namespace image {
31
32
/*static*/ void
33
ImageFactory::Initialize()
34
0
{ }
35
36
static uint32_t
37
ComputeImageFlags(nsIURI* uri, const nsCString& aMimeType, bool isMultiPart)
38
0
{
39
0
  nsresult rv;
40
0
41
0
  // We default to the static globals.
42
0
  bool isDiscardable = gfxPrefs::ImageMemDiscardable();
43
0
  bool doDecodeImmediately = gfxPrefs::ImageDecodeImmediatelyEnabled();
44
0
45
0
  // We want UI to be as snappy as possible and not to flicker. Disable
46
0
  // discarding for chrome URLS.
47
0
  bool isChrome = false;
48
0
  rv = uri->SchemeIs("chrome", &isChrome);
49
0
  if (NS_SUCCEEDED(rv) && isChrome) {
50
0
    isDiscardable = false;
51
0
  }
52
0
53
0
  // We don't want resources like the "loading" icon to be discardable either.
54
0
  bool isResource = false;
55
0
  rv = uri->SchemeIs("resource", &isResource);
56
0
  if (NS_SUCCEEDED(rv) && isResource) {
57
0
    isDiscardable = false;
58
0
  }
59
0
60
0
  // For multipart/x-mixed-replace, we basically want a direct channel to the
61
0
  // decoder. Disable everything for this case.
62
0
  if (isMultiPart) {
63
0
    isDiscardable = false;
64
0
  }
65
0
66
0
  // We have all the information we need.
67
0
  uint32_t imageFlags = Image::INIT_FLAG_NONE;
68
0
  if (isDiscardable) {
69
0
    imageFlags |= Image::INIT_FLAG_DISCARDABLE;
70
0
  }
71
0
  if (doDecodeImmediately) {
72
0
    imageFlags |= Image::INIT_FLAG_DECODE_IMMEDIATELY;
73
0
  }
74
0
  if (isMultiPart) {
75
0
    imageFlags |= Image::INIT_FLAG_TRANSIENT;
76
0
  }
77
0
78
0
  // Synchronously decode metadata (including size) if we have a data URI since
79
0
  // the data is immediately available.
80
0
  bool isDataURI = false;
81
0
  rv = uri->SchemeIs("data", &isDataURI);
82
0
  if (NS_SUCCEEDED(rv) && isDataURI) {
83
0
    imageFlags |= Image::INIT_FLAG_SYNC_LOAD;
84
0
  }
85
0
86
0
  return imageFlags;
87
0
}
88
89
#ifdef DEBUG
90
static void
91
NotifyImageLoading(nsIURI* aURI)
92
{
93
  if (!NS_IsMainThread()) {
94
    nsCOMPtr<nsIURI> uri(aURI);
95
    nsCOMPtr<nsIRunnable> ev =
96
      NS_NewRunnableFunction("NotifyImageLoading", [uri] () -> void {
97
        NotifyImageLoading(uri);
98
    });
99
    SystemGroup::Dispatch(TaskCategory::Other, ev.forget());
100
    return;
101
  }
102
103
  nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
104
  NS_WARNING_ASSERTION(obs, "Can't get an observer service handle");
105
  if (obs) {
106
    nsAutoCString spec;
107
    aURI->GetSpec(spec);
108
    obs->NotifyObservers(nullptr, "image-loading", NS_ConvertUTF8toUTF16(spec).get());
109
  }
110
}
111
#endif
112
113
/* static */ already_AddRefed<Image>
114
ImageFactory::CreateImage(nsIRequest* aRequest,
115
                          ProgressTracker* aProgressTracker,
116
                          const nsCString& aMimeType,
117
                          nsIURI* aURI,
118
                          bool aIsMultiPart,
119
                          uint32_t aInnerWindowId)
120
0
{
121
0
  MOZ_ASSERT(gfxPrefs::SingletonExists(),
122
0
             "Pref observers should have been initialized already");
123
0
124
0
  // Compute the image's initialization flags.
125
0
  uint32_t imageFlags = ComputeImageFlags(aURI, aMimeType, aIsMultiPart);
126
0
127
#ifdef DEBUG
128
  // Record the image load for startup performance testing.
129
  bool match = false;
130
  if ((NS_SUCCEEDED(aURI->SchemeIs("resource", &match)) && match) ||
131
      (NS_SUCCEEDED(aURI->SchemeIs("chrome", &match)) && match)) {
132
    NotifyImageLoading(aURI);
133
  }
134
#endif
135
136
0
  // Select the type of image to create based on MIME type.
137
0
  if (aMimeType.EqualsLiteral(IMAGE_SVG_XML)) {
138
0
    return CreateVectorImage(aRequest, aProgressTracker, aMimeType,
139
0
                             aURI, imageFlags, aInnerWindowId);
140
0
  } else {
141
0
    return CreateRasterImage(aRequest, aProgressTracker, aMimeType,
142
0
                             aURI, imageFlags, aInnerWindowId);
143
0
  }
144
0
}
145
146
// Marks an image as having an error before returning it.
147
template <typename T>
148
static already_AddRefed<Image>
149
BadImage(const char* aMessage, RefPtr<T>& aImage)
150
0
{
151
0
  aImage->SetHasError();
152
0
  return aImage.forget();
153
0
}
Unexecuted instantiation: Unified_cpp_image0.cpp:already_AddRefed<mozilla::image::Image> mozilla::image::BadImage<mozilla::image::RasterImage>(char const*, RefPtr<mozilla::image::RasterImage>&)
Unexecuted instantiation: Unified_cpp_image0.cpp:already_AddRefed<mozilla::image::Image> mozilla::image::BadImage<mozilla::image::VectorImage>(char const*, RefPtr<mozilla::image::VectorImage>&)
154
155
/* static */ already_AddRefed<Image>
156
ImageFactory::CreateAnonymousImage(const nsCString& aMimeType,
157
                                   uint32_t aSizeHint /* = 0 */)
158
0
{
159
0
  nsresult rv;
160
0
161
0
  RefPtr<RasterImage> newImage = new RasterImage();
162
0
163
0
  RefPtr<ProgressTracker> newTracker = new ProgressTracker();
164
0
  newTracker->SetImage(newImage);
165
0
  newImage->SetProgressTracker(newTracker);
166
0
167
0
  rv = newImage->Init(aMimeType.get(), Image::INIT_FLAG_SYNC_LOAD);
168
0
  if (NS_FAILED(rv)) {
169
0
    return BadImage("RasterImage::Init failed", newImage);
170
0
  }
171
0
172
0
  rv = newImage->SetSourceSizeHint(aSizeHint);
173
0
  if (NS_FAILED(rv)) {
174
0
    return BadImage("RasterImage::SetSourceSizeHint failed", newImage);
175
0
  }
176
0
177
0
  return newImage.forget();
178
0
}
179
180
/* static */ already_AddRefed<MultipartImage>
181
ImageFactory::CreateMultipartImage(Image* aFirstPart,
182
                                   ProgressTracker* aProgressTracker)
183
0
{
184
0
  MOZ_ASSERT(aFirstPart);
185
0
  MOZ_ASSERT(aProgressTracker);
186
0
187
0
  RefPtr<MultipartImage> newImage = new MultipartImage(aFirstPart);
188
0
  aProgressTracker->SetImage(newImage);
189
0
  newImage->SetProgressTracker(aProgressTracker);
190
0
191
0
  newImage->Init();
192
0
193
0
  return newImage.forget();
194
0
}
195
196
int32_t
197
SaturateToInt32(int64_t val)
198
0
{
199
0
  if (val > INT_MAX) {
200
0
    return INT_MAX;
201
0
  }
202
0
  if (val < INT_MIN) {
203
0
    return INT_MIN;
204
0
  }
205
0
206
0
  return static_cast<int32_t>(val);
207
0
}
208
209
uint32_t
210
GetContentSize(nsIRequest* aRequest)
211
0
{
212
0
  nsCOMPtr<nsIChannel> channel(do_QueryInterface(aRequest));
213
0
  if (channel) {
214
0
    int64_t size;
215
0
    nsresult rv = channel->GetContentLength(&size);
216
0
    if (NS_SUCCEEDED(rv)) {
217
0
      return std::max(SaturateToInt32(size), 0);
218
0
    }
219
0
  }
220
0
221
0
  // Use the file size as a size hint for file channels.
222
0
  nsCOMPtr<nsIFileChannel> fileChannel(do_QueryInterface(aRequest));
223
0
  if (fileChannel) {
224
0
    nsCOMPtr<nsIFile> file;
225
0
    nsresult rv = fileChannel->GetFile(getter_AddRefs(file));
226
0
    if (NS_SUCCEEDED(rv)) {
227
0
      int64_t filesize;
228
0
      rv = file->GetFileSize(&filesize);
229
0
      if (NS_SUCCEEDED(rv)) {
230
0
        return std::max(SaturateToInt32(filesize), 0);
231
0
      }
232
0
    }
233
0
  }
234
0
235
0
  // Fallback - neither http nor file. We'll use dynamic allocation.
236
0
  return 0;
237
0
}
238
239
/* static */ already_AddRefed<Image>
240
ImageFactory::CreateRasterImage(nsIRequest* aRequest,
241
                                ProgressTracker* aProgressTracker,
242
                                const nsCString& aMimeType,
243
                                nsIURI* aURI,
244
                                uint32_t aImageFlags,
245
                                uint32_t aInnerWindowId)
246
0
{
247
0
  MOZ_ASSERT(aProgressTracker);
248
0
249
0
  nsresult rv;
250
0
251
0
  RefPtr<RasterImage> newImage = new RasterImage(aURI);
252
0
  aProgressTracker->SetImage(newImage);
253
0
  newImage->SetProgressTracker(aProgressTracker);
254
0
255
0
  rv = newImage->Init(aMimeType.get(), aImageFlags);
256
0
  if (NS_FAILED(rv)) {
257
0
    return BadImage("RasterImage::Init failed", newImage);
258
0
  }
259
0
260
0
  newImage->SetInnerWindowID(aInnerWindowId);
261
0
262
0
  rv = newImage->SetSourceSizeHint(GetContentSize(aRequest));
263
0
  if (NS_FAILED(rv)) {
264
0
    return BadImage("RasterImage::SetSourceSizeHint failed", newImage);
265
0
  }
266
0
267
0
  return newImage.forget();
268
0
}
269
270
/* static */ already_AddRefed<Image>
271
ImageFactory::CreateVectorImage(nsIRequest* aRequest,
272
                                ProgressTracker* aProgressTracker,
273
                                const nsCString& aMimeType,
274
                                nsIURI* aURI,
275
                                uint32_t aImageFlags,
276
                                uint32_t aInnerWindowId)
277
0
{
278
0
  MOZ_ASSERT(aProgressTracker);
279
0
280
0
  nsresult rv;
281
0
282
0
  RefPtr<VectorImage> newImage = new VectorImage(aURI);
283
0
  aProgressTracker->SetImage(newImage);
284
0
  newImage->SetProgressTracker(aProgressTracker);
285
0
286
0
  rv = newImage->Init(aMimeType.get(), aImageFlags);
287
0
  if (NS_FAILED(rv)) {
288
0
    return BadImage("VectorImage::Init failed", newImage);
289
0
  }
290
0
291
0
  newImage->SetInnerWindowID(aInnerWindowId);
292
0
293
0
  rv = newImage->OnStartRequest(aRequest, nullptr);
294
0
  if (NS_FAILED(rv)) {
295
0
    return BadImage("VectorImage::OnStartRequest failed", newImage);
296
0
  }
297
0
298
0
  return newImage.forget();
299
0
}
300
301
} // namespace image
302
} // namespace mozilla