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() : m_stopped(false) |
| 20 { |
| 21 } |
| 22 |
| 23 NotificationImageLoader::~NotificationImageLoader() |
| 24 { |
| 25 } |
| 26 |
| 27 void NotificationImageLoader::start(ExecutionContext* executionContext, const KU
RL& url, PassOwnPtr<ImageCallback> imageCallback) |
| 28 { |
| 29 DCHECK(!m_stopped); |
| 30 |
| 31 m_imageCallback = imageCallback; |
| 32 |
| 33 // TODO(mvanouwerkerk): Add a timeout mechanism: crbug.com/579137. |
| 34 ThreadableLoaderOptions threadableLoaderOptions; |
| 35 threadableLoaderOptions.preflightPolicy = PreventPreflight; |
| 36 threadableLoaderOptions.crossOriginRequestPolicy = AllowCrossOriginRequests; |
| 37 |
| 38 // TODO(mvanouwerkerk): Add an entry for notifications to FetchInitiatorType
Names and use it. |
| 39 ResourceLoaderOptions resourceLoaderOptions; |
| 40 resourceLoaderOptions.allowCredentials = AllowStoredCredentials; |
| 41 if (executionContext->isWorkerGlobalScope()) |
| 42 resourceLoaderOptions.requestInitiatorContext = WorkerContext; |
| 43 |
| 44 ResourceRequest resourceRequest(url); |
| 45 resourceRequest.setRequestContext(WebURLRequest::RequestContextImage); |
| 46 resourceRequest.setPriority(ResourceLoadPriorityMedium); |
| 47 resourceRequest.setRequestorOrigin(executionContext->getSecurityOrigin()); |
| 48 |
| 49 m_threadableLoader = ThreadableLoader::create(*executionContext, this, threa
dableLoaderOptions, resourceLoaderOptions); |
| 50 m_threadableLoader->start(resourceRequest); |
| 51 } |
| 52 |
| 53 void NotificationImageLoader::stop() |
| 54 { |
| 55 if (m_stopped) |
| 56 return; |
| 57 |
| 58 m_stopped = true; |
| 59 if (m_threadableLoader) { |
| 60 m_threadableLoader->cancel(); |
| 61 // WorkerThreadableLoader keeps a Persistent<WorkerGlobalScope> to the |
| 62 // ExecutionContext it received in |create|. Kill it to prevent |
| 63 // reference cycles involving a mix of GC and non-GC types that fail to |
| 64 // clear in ThreadState::cleanup. |
| 65 m_threadableLoader.clear(); |
| 66 } |
| 67 } |
| 68 |
| 69 void NotificationImageLoader::didReceiveData(const char* data, unsigned length) |
| 70 { |
| 71 if (!m_data) |
| 72 m_data = SharedBuffer::create(); |
| 73 m_data->append(data, length); |
| 74 } |
| 75 |
| 76 void NotificationImageLoader::didFinishLoading(unsigned long resourceIdentifier,
double finishTime) |
| 77 { |
| 78 // If this has been stopped it is not desirable to trigger further work, |
| 79 // there is a shutdown of some sort in progress. |
| 80 if (m_stopped) |
| 81 return; |
| 82 |
| 83 if (m_data) { |
| 84 OwnPtr<ImageDecoder> decoder = ImageDecoder::create(*m_data.get(), Image
Decoder::AlphaPremultiplied, ImageDecoder::GammaAndColorProfileApplied); |
| 85 if (decoder) { |
| 86 decoder->setData(m_data.get(), true /* allDataReceived */); |
| 87 // The |ImageFrame*| is owned by the decoder. |
| 88 ImageFrame* imageFrame = decoder->frameBufferAtIndex(0); |
| 89 if (imageFrame) { |
| 90 (*m_imageCallback)(imageFrame->bitmap()); |
| 91 return; |
| 92 } |
| 93 } |
| 94 } |
| 95 runCallbackWithEmptyBitmap(); |
| 96 } |
| 97 |
| 98 void NotificationImageLoader::didFail(const ResourceError& error) |
| 99 { |
| 100 runCallbackWithEmptyBitmap(); |
| 101 } |
| 102 |
| 103 void NotificationImageLoader::didFailRedirectCheck() |
| 104 { |
| 105 runCallbackWithEmptyBitmap(); |
| 106 } |
| 107 |
| 108 void NotificationImageLoader::runCallbackWithEmptyBitmap() |
| 109 { |
| 110 // If this has been stopped it is not desirable to trigger further work, |
| 111 // there is a shutdown of some sort in progress. |
| 112 if (m_stopped) |
| 113 return; |
| 114 |
| 115 (*m_imageCallback)(SkBitmap()); |
| 116 } |
| 117 |
| 118 } // namespace blink |
OLD | NEW |