OLD | NEW |
---|---|
(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_isStopped(false) | |
21 , m_data(SharedBuffer::create()) | |
22 { | |
23 } | |
24 | |
25 NotificationImageLoader::~NotificationImageLoader() | |
26 { | |
27 } | |
28 | |
29 void NotificationImageLoader::start( | |
30 ExecutionContext* executionContext, | |
31 const KURL& url, PassOwnPtr<ImageCallback> imageCallback) | |
32 { | |
33 if (m_isStopped) | |
Peter Beverloo
2016/04/13 18:32:27
I don't think we'll ever hit this case unless some
Michael van Ouwerkerk
2016/04/14 13:42:11
Done.
| |
34 return; | |
35 | |
36 m_imageCallback = imageCallback; | |
37 | |
38 // TODO(mvanouwerkerk): Add a timeout mechanism: crbug.com/579137. | |
Peter Beverloo
2016/04/13 18:32:27
Should we have a TODO on adding a FetchInitiatorTy
Peter Beverloo
2016/04/13 18:32:27
Note: While I'm sure you're aware, ThreadableLoade
Michael van Ouwerkerk
2016/04/14 13:42:11
Sure... what is it used for?
Michael van Ouwerkerk
2016/04/14 13:42:11
Yeah that's why it's here. I'm not sure we have en
Peter Beverloo
2016/04/15 11:07:29
I assume for DevTools' Initiator column in the net
| |
39 ThreadableLoaderOptions threadableLoaderOptions; | |
40 threadableLoaderOptions.preflightPolicy = PreventPreflight; | |
Peter Beverloo
2016/04/13 18:32:27
To confirm: We prevent pre-flight requests since w
Michael van Ouwerkerk
2016/04/14 13:42:11
Acknowledged.
| |
41 threadableLoaderOptions.crossOriginRequestPolicy = AllowCrossOriginRequests; | |
42 | |
43 ResourceLoaderOptions resourceLoaderOptions; | |
44 resourceLoaderOptions.allowCredentials = AllowStoredCredentials; | |
45 if (executionContext->isWorkerGlobalScope()) | |
46 resourceLoaderOptions.requestInitiatorContext = WorkerContext; | |
47 | |
48 ResourceRequest resourceRequest(url); | |
49 resourceRequest.setRequestContext(WebURLRequest::RequestContextImage); | |
50 resourceRequest.setPriority(ResourceLoadPriorityMedium); | |
51 resourceRequest.setRequestorOrigin(executionContext->getSecurityOrigin()); | |
52 | |
53 m_threadableLoader = ThreadableLoader::create(*executionContext, this, | |
Peter Beverloo
2016/04/13 18:32:27
micro nit: nowrap
Michael van Ouwerkerk
2016/04/14 13:42:11
Done.
| |
54 threadableLoaderOptions, resourceLoaderOptions); | |
55 m_threadableLoader->start(resourceRequest); | |
56 } | |
57 | |
58 void NotificationImageLoader::stop() | |
59 { | |
60 if (m_isStopped) | |
61 return; | |
62 | |
63 m_isStopped = true; | |
64 if (m_threadableLoader) { | |
65 m_threadableLoader->cancel(); | |
66 // WorkerThreadableLoader keeps a Persistent<WorkerGlobalScope> to the | |
67 // ExecutionContext it received in |create|. Kill it to prevent | |
68 // reference cycles involving a mix of GC and non-GC types that fail to | |
69 // clear in ThreadState::cleanup. | |
70 m_threadableLoader.clear(); | |
71 } | |
72 } | |
73 | |
74 void NotificationImageLoader::didReceiveData(const char* data, unsigned length) | |
75 { | |
76 m_data->append(data, length); | |
77 } | |
78 | |
79 void NotificationImageLoader::didFinishLoading(unsigned long resourceIdentifier, | |
Peter Beverloo
2016/04/13 18:32:27
micro nit: nowrap
Michael van Ouwerkerk
2016/04/14 13:42:11
Done.
| |
80 double finishTime) | |
81 { | |
82 // If this has been stopped it is not desirable to trigger further work, | |
83 // there is a shutdown of some sort in progress. | |
84 if (m_isStopped) | |
85 return; | |
86 | |
87 OwnPtr<ImageDecoder> decoder = ImageDecoder::create(*m_data.get(), | |
88 ImageDecoder::AlphaPremultiplied, | |
89 ImageDecoder::GammaAndColorProfileApplied); | |
90 if (decoder) { | |
91 decoder->setData(m_data.get(), true /* allDataReceived */); | |
92 // The |ImageFrame*| is owned by the decoder. | |
93 ImageFrame* imageFrame = decoder->frameBufferAtIndex(0); | |
94 if (imageFrame) { | |
95 (*m_imageCallback)(imageFrame->bitmap()); | |
96 return; | |
97 } | |
98 } | |
99 runCallbackWithEmptyBitmap(); | |
100 } | |
101 | |
102 void NotificationImageLoader::didFail(const ResourceError& error) | |
103 { | |
104 runCallbackWithEmptyBitmap(); | |
105 } | |
106 | |
107 void NotificationImageLoader::didFailRedirectCheck() | |
108 { | |
109 runCallbackWithEmptyBitmap(); | |
110 } | |
111 | |
112 void NotificationImageLoader::runCallbackWithEmptyBitmap() | |
113 { | |
114 // If this has been stopped it is not desirable to trigger further work, | |
115 // there is a shutdown of some sort in progress. | |
116 if (m_isStopped) | |
117 return; | |
118 | |
119 (*m_imageCallback)(SkBitmap()); | |
120 } | |
121 | |
122 } // namespace blink | |
OLD | NEW |