Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(28)

Side by Side Diff: third_party/WebKit/Source/modules/notifications/NotificationImageLoader.cpp

Issue 1847863002: Move notification resource loading from content/child to blink (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Call stop when all fetches have finished. Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "modules/notifications/NotificationImageLoader.h"
6
7 #include "core/dom/ExecutionContext.h"
8 #include "core/fetch/ResourceLoaderOptions.h"
9 #include "platform/image-decoders/ImageDecoder.h"
10 #include "platform/image-decoders/ImageFrame.h"
11 #include "platform/network/ResourceError.h"
12 #include "platform/network/ResourceLoadPriority.h"
13 #include "platform/network/ResourceRequest.h"
14 #include "platform/weborigin/KURL.h"
15 #include "public/platform/WebURLRequest.h"
16
17 namespace blink {
18
19 NotificationImageLoader::NotificationImageLoader()
20 : m_stopped(false)
21 , m_data(SharedBuffer::create())
22 {
23 LOG(WARNING) << __FUNCTION__;
24 }
25
26 NotificationImageLoader::~NotificationImageLoader()
27 {
28 LOG(WARNING) << __FUNCTION__;
29 }
30
31 void NotificationImageLoader::start(
32 ExecutionContext* executionContext,
33 const KURL& url, PassOwnPtr<ImageCallback> imageCallback)
34 {
35 LOG(WARNING) << __FUNCTION__;
36 DCHECK(!m_stopped);
37
38 m_imageCallback = imageCallback;
39
40 // TODO(mvanouwerkerk): Add a timeout mechanism: crbug.com/579137.
41 ThreadableLoaderOptions threadableLoaderOptions;
42 threadableLoaderOptions.preflightPolicy = PreventPreflight;
43 threadableLoaderOptions.crossOriginRequestPolicy = AllowCrossOriginRequests;
44
45 // TODO(mvanouwerkerk): Add an entry for notifications to FetchInitiatorType Names and use it.
46 ResourceLoaderOptions resourceLoaderOptions;
47 resourceLoaderOptions.allowCredentials = AllowStoredCredentials;
48 if (executionContext->isWorkerGlobalScope())
49 resourceLoaderOptions.requestInitiatorContext = WorkerContext;
50
51 ResourceRequest resourceRequest(url);
52 resourceRequest.setRequestContext(WebURLRequest::RequestContextImage);
53 resourceRequest.setPriority(ResourceLoadPriorityMedium);
54 resourceRequest.setRequestorOrigin(executionContext->getSecurityOrigin());
55
56 m_threadableLoader = ThreadableLoader::create(*executionContext, this, threa dableLoaderOptions, resourceLoaderOptions);
57 m_threadableLoader->start(resourceRequest);
58 }
59
60 void NotificationImageLoader::stop()
61 {
62 LOG(WARNING) << __FUNCTION__;
63 if (m_stopped)
64 return;
65
66 m_stopped = true;
67 if (m_threadableLoader) {
68 m_threadableLoader->cancel();
69 // WorkerThreadableLoader keeps a Persistent<WorkerGlobalScope> to the
70 // ExecutionContext it received in |create|. Kill it to prevent
71 // reference cycles involving a mix of GC and non-GC types that fail to
72 // clear in ThreadState::cleanup.
73 m_threadableLoader.clear();
74 }
75 }
76
77 void NotificationImageLoader::didReceiveData(const char* data, unsigned length)
78 {
79 m_data->append(data, length);
80 }
81
82 void NotificationImageLoader::didFinishLoading(unsigned long resourceIdentifier, double finishTime)
83 {
84 // If this has been stopped it is not desirable to trigger further work,
85 // there is a shutdown of some sort in progress.
86 if (m_stopped)
87 return;
88
89 OwnPtr<ImageDecoder> decoder = ImageDecoder::create(*m_data.get(),
90 ImageDecoder::AlphaPremultiplied,
91 ImageDecoder::GammaAndColorProfileApplied);
92 if (decoder) {
93 decoder->setData(m_data.get(), true /* allDataReceived */);
94 // The |ImageFrame*| is owned by the decoder.
95 ImageFrame* imageFrame = decoder->frameBufferAtIndex(0);
96 if (imageFrame) {
97 (*m_imageCallback)(imageFrame->bitmap());
98 return;
99 }
100 }
101 runCallbackWithEmptyBitmap();
102 }
103
104 void NotificationImageLoader::didFail(const ResourceError& error)
105 {
106 runCallbackWithEmptyBitmap();
107 }
108
109 void NotificationImageLoader::didFailRedirectCheck()
110 {
111 runCallbackWithEmptyBitmap();
112 }
113
114 void NotificationImageLoader::runCallbackWithEmptyBitmap()
115 {
116 // If this has been stopped it is not desirable to trigger further work,
117 // there is a shutdown of some sort in progress.
118 if (m_stopped)
119 return;
120
121 (*m_imageCallback)(SkBitmap());
122 }
123
124 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698