Chromium Code Reviews| 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/CSSPaintDefinition.h" | 5 #include "modules/csspaint/CSSPaintDefinition.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/ScriptState.h" | 7 #include "bindings/core/v8/ScriptState.h" |
| 8 #include "bindings/core/v8/V8Binding.h" | |
| 9 #include "bindings/core/v8/V8BindingMacros.h" | |
| 10 #include "core/dom/ExecutionContext.h" | |
| 11 #include "core/frame/ConsoleTypes.h" | |
| 12 #include "core/inspector/ConsoleMessage.h" | |
| 13 #include "modules/csspaint/Geometry.h" | |
| 14 #include "modules/csspaint/PaintRenderingContext2D.h" | |
| 15 #include "platform/ScriptForbiddenScope.h" | |
| 16 #include "platform/graphics/ImageBuffer.h" | |
| 17 #include "platform/graphics/PaintGeneratedImage.h" | |
| 18 #include "platform/graphics/RecordingImageBufferSurface.h" | |
| 19 #include "platform/graphics/UnacceleratedImageBufferSurface.h" | |
| 8 | 20 |
| 9 namespace blink { | 21 namespace blink { |
| 10 | 22 |
| 23 namespace { | |
| 24 | |
| 25 // TODO(ikilpatrick): Modify RecordingImageBufferSurface so that it cannot | |
| 26 // fallback to a UnacceleratedImageBufferSurface. | |
| 27 class UnacceleratedSurfaceFactory : public RecordingImageBufferFallbackSurfaceFa ctory { | |
| 28 public: | |
| 29 virtual PassOwnPtr<ImageBufferSurface> createSurface(const IntSize& size, Op acityMode opacityMode) | |
| 30 { | |
| 31 return adoptPtr(new UnacceleratedImageBufferSurface(size, opacityMode)); | |
| 32 } | |
| 33 | |
| 34 virtual ~UnacceleratedSurfaceFactory() { } | |
| 35 }; | |
| 36 | |
| 37 } // namespace | |
| 38 | |
| 11 CSSPaintDefinition* CSSPaintDefinition::create(ScriptState* scriptState, v8::Loc al<v8::Function> constructor, v8::Local<v8::Function> paint) | 39 CSSPaintDefinition* CSSPaintDefinition::create(ScriptState* scriptState, v8::Loc al<v8::Function> constructor, v8::Local<v8::Function> paint) |
| 12 { | 40 { |
| 13 return new CSSPaintDefinition(scriptState, constructor, paint); | 41 return new CSSPaintDefinition(scriptState, constructor, paint); |
| 14 } | 42 } |
| 15 | 43 |
| 16 CSSPaintDefinition::CSSPaintDefinition(ScriptState* scriptState, v8::Local<v8::F unction> constructor, v8::Local<v8::Function> paint) | 44 CSSPaintDefinition::CSSPaintDefinition(ScriptState* scriptState, v8::Local<v8::F unction> constructor, v8::Local<v8::Function> paint) |
| 17 : m_scriptState(scriptState) | 45 : m_scriptState(scriptState) |
| 18 , m_constructor(scriptState->isolate(), constructor) | 46 , m_constructor(scriptState->isolate(), constructor) |
| 19 , m_paint(scriptState->isolate(), paint) | 47 , m_paint(scriptState->isolate(), paint) |
| 20 { | 48 { |
| 21 } | 49 } |
| 22 | 50 |
| 23 CSSPaintDefinition::~CSSPaintDefinition() | 51 CSSPaintDefinition::~CSSPaintDefinition() |
| 24 { | 52 { |
| 25 } | 53 } |
| 26 | 54 |
| 55 PassRefPtr<Image> CSSPaintDefinition::paint(const IntSize& size) | |
| 56 { | |
| 57 v8::Isolate* isolate = m_scriptState->isolate(); | |
| 58 | |
| 59 ScriptState::Scope scope(m_scriptState.get()); | |
| 60 v8::MicrotasksScope microtasksScope(isolate, v8::MicrotasksScope::kRunMicrot asks); | |
|
haraken
2016/04/11 02:18:14
v8::MicrotasksScope is handled by V8ScriptRunner::
ikilpatrick
2016/04/11 22:34:24
Done.
| |
| 61 | |
| 62 maybeCreatePaintInstance(); | |
| 63 v8::Local<v8::Object> instance = m_instance.newLocal(isolate); | |
| 64 | |
| 65 // We may have failed to create an instance class, in which case produce an | |
| 66 // invalid image. | |
| 67 if (isUndefinedOrNull(instance)) | |
| 68 return nullptr; | |
| 69 | |
| 70 PaintRenderingContext2D* renderingContext = PaintRenderingContext2D::create( | |
| 71 ImageBuffer::create(adoptPtr(new RecordingImageBufferSurface(size, adopt Ptr(new UnacceleratedSurfaceFactory()))))); | |
| 72 Geometry* geom = Geometry::create(size); | |
|
haraken
2016/04/11 02:18:14
geom => geometry
ikilpatrick
2016/04/11 22:34:24
Done.
| |
| 73 | |
| 74 v8::Local<v8::Value> argv[] = { | |
| 75 toV8(renderingContext, m_scriptState->context()->Global(), isolate), | |
| 76 toV8(geom, m_scriptState->context()->Global(), isolate) | |
| 77 }; | |
| 78 | |
| 79 v8::Local<v8::Function> paint = m_paint.newLocal(isolate); | |
| 80 | |
| 81 ScriptForbiddenScope::AllowUserAgentScript allowScripting; | |
|
haraken
2016/04/11 02:18:14
Why do you need ScriptForbiddenScope::AllowUserAge
ikilpatrick
2016/04/11 22:34:24
Done.
| |
| 82 v8::TryCatch block(isolate); | |
|
haraken
2016/04/11 02:18:14
You won't need this TryCatch.
| |
| 83 | |
| 84 V8ScriptRunner::callFunction(paint, m_scriptState->getExecutionContext(), in stance, 2, argv, isolate); | |
| 85 | |
| 86 // The paint function may have produced an error, in which case produce an | |
| 87 // invalid image. | |
| 88 if (block.HasCaught()) { | |
|
haraken
2016/04/11 02:18:14
What happens if the paint function didn't throw a
ikilpatrick
2016/04/11 22:34:23
Sorry didn't get "but didn't wrong with painting?"
haraken
2016/04/12 00:44:20
Thanks, makes sense.
| |
| 89 // TODO(ikilpatrick): messageHandlerInMainThread doesn't currently know | |
| 90 // about main-thread worklets. | |
| 91 m_scriptState->getExecutionContext()->addConsoleMessage(ConsoleMessage:: create(JSMessageSource, ErrorMessageLevel, "Failed to invoke the paint method.") ); | |
| 92 return nullptr; | |
| 93 } | |
| 94 | |
| 95 return PaintGeneratedImage::create(renderingContext->imageBuffer()->getPictu re(), size); | |
| 96 } | |
| 97 | |
| 98 void CSSPaintDefinition::maybeCreatePaintInstance() | |
| 99 { | |
| 100 ASSERT(m_invalidConstructor || m_instance.isEmpty()); | |
| 101 | |
| 102 if (m_invalidConstructor) | |
| 103 return; | |
| 104 | |
| 105 if (!m_instance.isEmpty()) | |
| 106 return; | |
| 107 | |
| 108 v8::Isolate* isolate = m_scriptState->isolate(); | |
| 109 v8::Local<v8::Function> constructor = m_constructor.newLocal(isolate); | |
| 110 ASSERT(!isUndefinedOrNull(constructor)); | |
| 111 | |
| 112 v8::Local<v8::Object> paintInstance; | |
| 113 v8::TryCatch block(isolate); | |
|
haraken
2016/04/11 02:18:14
You won't need this v8::TryCatch.
ikilpatrick
2016/04/11 22:34:24
Done.
| |
| 114 | |
| 115 if (!v8Call(constructor->NewInstance(m_scriptState->context()), paintInstanc e, block)) { | |
|
haraken
2016/04/11 02:18:14
Can we use V8ObjectConstructor::newInstance? Then
ikilpatrick
2016/04/11 22:34:23
Done.
| |
| 116 m_invalidConstructor = true; | |
| 117 // TODO(ikilpatrick): messageHandlerInMainThread doesn't currently | |
|
haraken
2016/04/11 02:18:14
Or maybe you can use v8CallOrCrash until you find
ikilpatrick
2016/04/11 22:34:24
Just removed the log at the moment. Filed crbug.co
| |
| 118 // know about main-thread worklets. | |
| 119 m_scriptState->getExecutionContext()->addConsoleMessage(ConsoleMessage:: create(JSMessageSource, ErrorMessageLevel, "Failed to create instance of class." )); | |
| 120 } else { | |
| 121 m_instance.set(isolate, paintInstance); | |
| 122 } | |
| 123 } | |
| 124 | |
| 27 } // namespace blink | 125 } // namespace blink |
| OLD | NEW |