OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "modules/csspaint/PaintWorkletGlobalScope.h" | 5 #include "modules/csspaint/PaintWorkletGlobalScope.h" |
6 | 6 |
| 7 #include "bindings/core/v8/ScopedPersistent.h" |
7 #include "bindings/core/v8/WorkerOrWorkletScriptController.h" | 8 #include "bindings/core/v8/WorkerOrWorkletScriptController.h" |
| 9 #include "core/dom/ExceptionCode.h" |
8 #include "core/inspector/MainThreadDebugger.h" | 10 #include "core/inspector/MainThreadDebugger.h" |
| 11 #include "modules/csspaint/CSSPaintDefinition.h" |
| 12 #include "modules/csspaint/CSSPaintImageGeneratorImpl.h" |
| 13 #include "platform/graphics/ImageBuffer.h" |
| 14 #include "platform/graphics/ImageBufferSurface.h" |
| 15 #include "platform/graphics/RecordingImageBufferSurface.h" |
9 | 16 |
10 namespace blink { | 17 namespace blink { |
11 | 18 |
12 // static | 19 // static |
13 PassRefPtrWillBeRawPtr<PaintWorkletGlobalScope> PaintWorkletGlobalScope::create(
LocalFrame* frame, const KURL& url, const String& userAgent, PassRefPtr<Security
Origin> securityOrigin, v8::Isolate* isolate) | 20 PassRefPtrWillBeRawPtr<PaintWorkletGlobalScope> PaintWorkletGlobalScope::create(
LocalFrame* frame, const KURL& url, const String& userAgent, PassRefPtr<Security
Origin> securityOrigin, v8::Isolate* isolate) |
14 { | 21 { |
15 RefPtrWillBeRawPtr<PaintWorkletGlobalScope> paintWorkletGlobalScope = adoptR
efWillBeNoop(new PaintWorkletGlobalScope(frame, url, userAgent, securityOrigin,
isolate)); | 22 RefPtrWillBeRawPtr<PaintWorkletGlobalScope> paintWorkletGlobalScope = adoptR
efWillBeNoop(new PaintWorkletGlobalScope(frame, url, userAgent, securityOrigin,
isolate)); |
16 paintWorkletGlobalScope->scriptController()->initializeContextIfNeeded(); | 23 paintWorkletGlobalScope->scriptController()->initializeContextIfNeeded(); |
17 MainThreadDebugger::contextCreated(paintWorkletGlobalScope->scriptController
()->getScriptState(), paintWorkletGlobalScope->frame(), paintWorkletGlobalScope-
>getSecurityOrigin()); | 24 MainThreadDebugger::contextCreated(paintWorkletGlobalScope->scriptController
()->getScriptState(), paintWorkletGlobalScope->frame(), paintWorkletGlobalScope-
>getSecurityOrigin()); |
18 return paintWorkletGlobalScope.release(); | 25 return paintWorkletGlobalScope.release(); |
19 } | 26 } |
20 | 27 |
21 PaintWorkletGlobalScope::PaintWorkletGlobalScope(LocalFrame* frame, const KURL&
url, const String& userAgent, PassRefPtr<SecurityOrigin> securityOrigin, v8::Iso
late* isolate) | 28 PaintWorkletGlobalScope::PaintWorkletGlobalScope(LocalFrame* frame, const KURL&
url, const String& userAgent, PassRefPtr<SecurityOrigin> securityOrigin, v8::Iso
late* isolate) |
22 : WorkletGlobalScope(frame, url, userAgent, securityOrigin, isolate) | 29 : WorkletGlobalScope(frame, url, userAgent, securityOrigin, isolate) |
23 { | 30 { |
24 } | 31 } |
25 | 32 |
26 PaintWorkletGlobalScope::~PaintWorkletGlobalScope() | 33 PaintWorkletGlobalScope::~PaintWorkletGlobalScope() |
27 { | 34 { |
28 } | 35 } |
29 | 36 |
| 37 void PaintWorkletGlobalScope::registerPaint(const String& name, const ScriptValu
e& ctorValue, ExceptionState& exceptionState) |
| 38 { |
| 39 if (m_paintDefinitions.contains(name)) { |
| 40 exceptionState.throwDOMException(NotSupportedError, "A class with name:'
" + name + "' is already registered."); |
| 41 return; |
| 42 } |
| 43 |
| 44 if (name.isEmpty()) { |
| 45 exceptionState.throwTypeError("The empty string is not a valid name."); |
| 46 return; |
| 47 } |
| 48 |
| 49 v8::TryCatch block(isolate()); |
| 50 v8::Local<v8::Context> context = scriptController()->context(); |
| 51 |
| 52 ASSERT(ctorValue.v8Value()->IsFunction()); |
| 53 v8::Local<v8::Function> ctor = v8::Local<v8::Function>::Cast(ctorValue.v8Val
ue()); |
| 54 |
| 55 v8::Local<v8::Value> inputPropertiesValue; |
| 56 if (!ctor->Get(context, v8String(isolate(), "inputProperties")).ToLocal(&inp
utPropertiesValue)) { |
| 57 exceptionState.rethrowV8Exception(block.Exception()); |
| 58 return; |
| 59 } |
| 60 |
| 61 if (!isUndefinedOrNull(inputPropertiesValue)) { |
| 62 toImplArray<Vector<String>>(inputPropertiesValue, 0, isolate(), exceptio
nState); |
| 63 |
| 64 if (exceptionState.hadException()) |
| 65 return; |
| 66 |
| 67 // TODO(ikilpatrick): Hook up invalidation based on these inputPropertie
s. |
| 68 } |
| 69 |
| 70 v8::Local<v8::Value> prototypeValue; |
| 71 if (!ctor->Get(context, v8String(isolate(), "prototype")).ToLocal(&prototype
Value)) { |
| 72 exceptionState.rethrowV8Exception(block.Exception()); |
| 73 return; |
| 74 } |
| 75 |
| 76 if (isUndefinedOrNull(prototypeValue)) { |
| 77 exceptionState.throwTypeError("The 'prototype' object on the class does
not exist."); |
| 78 return; |
| 79 } |
| 80 |
| 81 if (!prototypeValue->IsObject()) { |
| 82 exceptionState.throwTypeError("The 'prototype' property on the class is
not an object."); |
| 83 return; |
| 84 } |
| 85 |
| 86 v8::Local<v8::Object> prototype = v8::Local<v8::Object>::Cast(prototypeValue
); |
| 87 |
| 88 v8::Local<v8::Value> paintValue; |
| 89 if (!prototype->Get(context, v8String(isolate(), "paint")).ToLocal(&paintVal
ue)) { |
| 90 exceptionState.rethrowV8Exception(block.Exception()); |
| 91 return; |
| 92 } |
| 93 |
| 94 if (isUndefinedOrNull(paintValue)) { |
| 95 exceptionState.throwTypeError("The 'paint' function on the prototype doe
s not exist."); |
| 96 return; |
| 97 } |
| 98 |
| 99 if (!paintValue->IsFunction()) { |
| 100 exceptionState.throwTypeError("The 'paint' property on the prototype is
not a function."); |
| 101 return; |
| 102 } |
| 103 |
| 104 v8::Local<v8::Function> paint = v8::Local<v8::Function>::Cast(paintValue); |
| 105 |
| 106 RefPtrWillBeRawPtr<CSSPaintDefinition> definition = CSSPaintDefinition::crea
te(scriptController()->getScriptState(), ctor, paint); |
| 107 m_paintDefinitions.set(name, definition); |
| 108 |
| 109 auto set = m_pendingGenerators.get(name); |
| 110 if (set) { |
| 111 for (const auto& generator : *set) { |
| 112 if (generator) { |
| 113 generator->setDefinition(definition); |
| 114 } |
| 115 } |
| 116 } |
| 117 |
| 118 m_pendingGenerators.remove(name); |
| 119 } |
| 120 |
| 121 CSSPaintDefinition* PaintWorkletGlobalScope::findDefinition(const String& name) |
| 122 { |
| 123 return m_paintDefinitions.get(name); |
| 124 } |
| 125 |
| 126 void PaintWorkletGlobalScope::addPendingGenerator(const String& name, PassRefPtr
WillBeRawPtr<CSSPaintImageGeneratorImpl> generator) |
| 127 { |
| 128 OwnPtrWillBeMember<GeneratorHashSet>& set = m_pendingGenerators.add(name, nu
llptr).storedValue->value; |
| 129 if (!set) |
| 130 set = adoptPtrWillBeNoop(new GeneratorHashSet); |
| 131 set->add(generator); |
| 132 } |
| 133 |
| 134 DEFINE_TRACE(PaintWorkletGlobalScope) |
| 135 { |
| 136 visitor->trace(m_paintDefinitions); |
| 137 visitor->trace(m_pendingGenerators); |
| 138 WorkletGlobalScope::trace(visitor); |
| 139 } |
| 140 |
30 } // namespace blink | 141 } // namespace blink |
OLD | NEW |