Chromium Code Reviews| Index: third_party/WebKit/Source/modules/notifications/NotificationImageLoader.cpp |
| diff --git a/third_party/WebKit/Source/modules/notifications/NotificationImageLoader.cpp b/third_party/WebKit/Source/modules/notifications/NotificationImageLoader.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..5ff771c64f38e3d12cc13bb017f12cba76f8a065 |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/modules/notifications/NotificationImageLoader.cpp |
| @@ -0,0 +1,122 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "modules/notifications/NotificationImageLoader.h" |
| + |
| +#include "core/dom/ExecutionContext.h" |
| +#include "core/fetch/ResourceLoaderOptions.h" |
| +#include "platform/image-decoders/ImageDecoder.h" |
| +#include "platform/image-decoders/ImageFrame.h" |
| +#include "platform/network/ResourceError.h" |
| +#include "platform/network/ResourceLoadPriority.h" |
| +#include "platform/network/ResourceRequest.h" |
| +#include "platform/weborigin/KURL.h" |
| +#include "public/platform/WebURLRequest.h" |
| + |
| +namespace blink { |
| + |
| +NotificationImageLoader::NotificationImageLoader() |
| + : m_isStopped(false) |
| + , m_data(SharedBuffer::create()) |
| +{ |
| +} |
| + |
| +NotificationImageLoader::~NotificationImageLoader() |
| +{ |
| +} |
| + |
| +void NotificationImageLoader::start( |
| + ExecutionContext* executionContext, |
| + const KURL& url, PassOwnPtr<ImageCallback> imageCallback) |
| +{ |
| + 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.
|
| + return; |
| + |
| + m_imageCallback = imageCallback; |
| + |
| + // 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
|
| + ThreadableLoaderOptions threadableLoaderOptions; |
| + 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.
|
| + threadableLoaderOptions.crossOriginRequestPolicy = AllowCrossOriginRequests; |
| + |
| + ResourceLoaderOptions resourceLoaderOptions; |
| + resourceLoaderOptions.allowCredentials = AllowStoredCredentials; |
| + if (executionContext->isWorkerGlobalScope()) |
| + resourceLoaderOptions.requestInitiatorContext = WorkerContext; |
| + |
| + ResourceRequest resourceRequest(url); |
| + resourceRequest.setRequestContext(WebURLRequest::RequestContextImage); |
| + resourceRequest.setPriority(ResourceLoadPriorityMedium); |
| + resourceRequest.setRequestorOrigin(executionContext->getSecurityOrigin()); |
| + |
| + 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.
|
| + threadableLoaderOptions, resourceLoaderOptions); |
| + m_threadableLoader->start(resourceRequest); |
| +} |
| + |
| +void NotificationImageLoader::stop() |
| +{ |
| + if (m_isStopped) |
| + return; |
| + |
| + m_isStopped = true; |
| + if (m_threadableLoader) { |
| + m_threadableLoader->cancel(); |
| + // WorkerThreadableLoader keeps a Persistent<WorkerGlobalScope> to the |
| + // ExecutionContext it received in |create|. Kill it to prevent |
| + // reference cycles involving a mix of GC and non-GC types that fail to |
| + // clear in ThreadState::cleanup. |
| + m_threadableLoader.clear(); |
| + } |
| +} |
| + |
| +void NotificationImageLoader::didReceiveData(const char* data, unsigned length) |
| +{ |
| + m_data->append(data, length); |
| +} |
| + |
| +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.
|
| + double finishTime) |
| +{ |
| + // If this has been stopped it is not desirable to trigger further work, |
| + // there is a shutdown of some sort in progress. |
| + if (m_isStopped) |
| + return; |
| + |
| + OwnPtr<ImageDecoder> decoder = ImageDecoder::create(*m_data.get(), |
| + ImageDecoder::AlphaPremultiplied, |
| + ImageDecoder::GammaAndColorProfileApplied); |
| + if (decoder) { |
| + decoder->setData(m_data.get(), true /* allDataReceived */); |
| + // The |ImageFrame*| is owned by the decoder. |
| + ImageFrame* imageFrame = decoder->frameBufferAtIndex(0); |
| + if (imageFrame) { |
| + (*m_imageCallback)(imageFrame->bitmap()); |
| + return; |
| + } |
| + } |
| + runCallbackWithEmptyBitmap(); |
| +} |
| + |
| +void NotificationImageLoader::didFail(const ResourceError& error) |
| +{ |
| + runCallbackWithEmptyBitmap(); |
| +} |
| + |
| +void NotificationImageLoader::didFailRedirectCheck() |
| +{ |
| + runCallbackWithEmptyBitmap(); |
| +} |
| + |
| +void NotificationImageLoader::runCallbackWithEmptyBitmap() |
| +{ |
| + // If this has been stopped it is not desirable to trigger further work, |
| + // there is a shutdown of some sort in progress. |
| + if (m_isStopped) |
| + return; |
| + |
| + (*m_imageCallback)(SkBitmap()); |
| +} |
| + |
| +} // namespace blink |