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