Home | History | Annotate | Download | only in imagebitmap
      1 /*
      2  * Copyright (c) 2013, Google Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions are
      6  * met:
      7  *
      8  *     * Redistributions of source code must retain the above copyright
      9  * notice, this list of conditions and the following disclaimer.
     10  *     * Redistributions in binary form must reproduce the above
     11  * copyright notice, this list of conditions and the following disclaimer
     12  * in the documentation and/or other materials provided with the
     13  * distribution.
     14  *     * Neither the name of Google Inc. nor the names of its
     15  * contributors may be used to endorse or promote products derived from
     16  * this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #ifndef ImageBitmapFactories_h
     32 #define ImageBitmapFactories_h
     33 
     34 #include "bindings/v8/ScriptPromise.h"
     35 #include "bindings/v8/ScriptPromiseResolverWithContext.h"
     36 #include "bindings/v8/ScriptState.h"
     37 #include "core/fileapi/FileReaderLoader.h"
     38 #include "core/fileapi/FileReaderLoaderClient.h"
     39 #include "platform/Supplementable.h"
     40 #include "platform/geometry/IntRect.h"
     41 #include "wtf/Forward.h"
     42 #include "wtf/HashSet.h"
     43 
     44 namespace WebCore {
     45 
     46 class Blob;
     47 class CanvasRenderingContext2D;
     48 class EventTarget;
     49 class ExceptionState;
     50 class HTMLCanvasElement;
     51 class HTMLImageElement;
     52 class HTMLVideoElement;
     53 class ImageBitmap;
     54 class ImageData;
     55 class ExecutionContext;
     56 
     57 class ImageBitmapFactories FINAL : public NoBaseWillBeGarbageCollectedFinalized<ImageBitmapFactories>, public WillBeHeapSupplement<LocalDOMWindow>, public WillBeHeapSupplement<WorkerGlobalScope> {
     58     WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(ImageBitmapFactories);
     59 
     60 public:
     61     static ScriptPromise createImageBitmap(ScriptState*, EventTarget&, HTMLImageElement*, ExceptionState&);
     62     static ScriptPromise createImageBitmap(ScriptState*, EventTarget&, HTMLImageElement*, int sx, int sy, int sw, int sh, ExceptionState&);
     63     static ScriptPromise createImageBitmap(ScriptState*, EventTarget&, HTMLVideoElement*, ExceptionState&);
     64     static ScriptPromise createImageBitmap(ScriptState*, EventTarget&, HTMLVideoElement*, int sx, int sy, int sw, int sh, ExceptionState&);
     65     static ScriptPromise createImageBitmap(ScriptState*, EventTarget&, CanvasRenderingContext2D*, ExceptionState&);
     66     static ScriptPromise createImageBitmap(ScriptState*, EventTarget&, CanvasRenderingContext2D*, int sx, int sy, int sw, int sh, ExceptionState&);
     67     static ScriptPromise createImageBitmap(ScriptState*, EventTarget&, HTMLCanvasElement*, ExceptionState&);
     68     static ScriptPromise createImageBitmap(ScriptState*, EventTarget&, HTMLCanvasElement*, int sx, int sy, int sw, int sh, ExceptionState&);
     69     static ScriptPromise createImageBitmap(ScriptState*, EventTarget&, Blob*, ExceptionState&);
     70     static ScriptPromise createImageBitmap(ScriptState*, EventTarget&, Blob*, int sx, int sy, int sw, int sh, ExceptionState&);
     71     static ScriptPromise createImageBitmap(ScriptState*, EventTarget&, ImageData*, ExceptionState&);
     72     static ScriptPromise createImageBitmap(ScriptState*, EventTarget&, ImageData*, int sx, int sy, int sw, int sh, ExceptionState&);
     73     static ScriptPromise createImageBitmap(ScriptState*, EventTarget&, ImageBitmap*, ExceptionState&);
     74     static ScriptPromise createImageBitmap(ScriptState*, EventTarget&, ImageBitmap*, int sx, int sy, int sw, int sh, ExceptionState&);
     75 
     76     virtual ~ImageBitmapFactories() { }
     77 
     78     void trace(Visitor*);
     79 
     80 protected:
     81     static const char* supplementName();
     82 
     83 private:
     84     class ImageBitmapLoader FINAL : public GarbageCollectedFinalized<ImageBitmapLoader>, public FileReaderLoaderClient {
     85     public:
     86         static ImageBitmapLoader* create(ImageBitmapFactories& factory, const IntRect& cropRect, ScriptState* scriptState)
     87         {
     88             return new ImageBitmapLoader(factory, cropRect, scriptState);
     89         }
     90 
     91         void loadBlobAsync(ExecutionContext*, Blob*);
     92         ScriptPromise promise() { return m_resolver->promise(); }
     93 
     94         void trace(Visitor*);
     95 
     96         virtual ~ImageBitmapLoader() { }
     97 
     98     private:
     99         ImageBitmapLoader(ImageBitmapFactories&, const IntRect&, ScriptState*);
    100 
    101         void rejectPromise();
    102 
    103         // FileReaderLoaderClient
    104         virtual void didStartLoading() OVERRIDE { }
    105         virtual void didReceiveData() OVERRIDE { }
    106         virtual void didFinishLoading() OVERRIDE;
    107         virtual void didFail(FileError::ErrorCode) OVERRIDE;
    108 
    109         FileReaderLoader m_loader;
    110         RawPtrWillBeMember<ImageBitmapFactories> m_factory;
    111         RefPtr<ScriptPromiseResolverWithContext> m_resolver;
    112         IntRect m_cropRect;
    113     };
    114 
    115     static ImageBitmapFactories& from(EventTarget&);
    116 
    117     template<class GlobalObject>
    118     static ImageBitmapFactories& fromInternal(GlobalObject&);
    119 
    120     void addLoader(ImageBitmapLoader*);
    121     void didFinishLoading(ImageBitmapLoader*);
    122 
    123     PersistentHeapHashSetWillBeHeapHashSet<Member<ImageBitmapLoader> > m_pendingLoaders;
    124 };
    125 
    126 } // namespace WebCore
    127 
    128 #endif // ImageBitmapFactories_h
    129