1 // Copyright 2018 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 #ifndef THIRD_PARTY_BLINK_RENDERER_BINDINGS_CORE_V8_JS_EVENT_HANDLER_FOR_CONTENT_ATTRIBUTE_H_
6 #define THIRD_PARTY_BLINK_RENDERER_BINDINGS_CORE_V8_JS_EVENT_HANDLER_FOR_CONTENT_ATTRIBUTE_H_
7 
8 #include "third_party/blink/renderer/bindings/core/v8/js_event_handler.h"
9 #include "third_party/blink/renderer/platform/wtf/text/text_position.h"
10 
11 namespace blink {
12 
13 // |JSEventHandlerForContentAttribute| supports lazy compilation for content
14 // attribute. This performs in the same way as |JSEventHandler| after it gets
15 // compiled.
16 class JSEventHandlerForContentAttribute final : public JSEventHandler {
17  public:
18   JSEventHandlerForContentAttribute(
19       v8::Isolate* isolate,
20       DOMWrapperWorld& world,
21       const AtomicString& function_name,
22       const String& script_body,
23       const String& source_url,
24       const TextPosition& position,
25       HandlerType type = HandlerType::kEventHandler)
JSEventHandler(type)26       : JSEventHandler(type),
27         did_compile_(false),
28         function_name_(function_name),
29         script_body_(script_body),
30         source_url_(source_url),
31         position_(position),
32         isolate_(isolate),
33         world_(&world) {}
34 
35   // blink::EventListener overrides:
IsEventHandlerForContentAttribute()36   bool IsEventHandlerForContentAttribute() const override { return true; }
37 
38   // blink::JSBasedEventListener overrides:
39   v8::Local<v8::Value> GetListenerObject(EventTarget&) override;
40   std::unique_ptr<SourceLocation> GetSourceLocation(EventTarget&) override;
41 
ScriptBody()42   const String& ScriptBody() const override { return script_body_; }
43 
44  protected:
45   // blink::JSBasedEventListener override:
GetIsolate()46   v8::Isolate* GetIsolate() const override { return isolate_; }
GetScriptState()47   ScriptState* GetScriptState() const override {
48     DCHECK(HasCompiledHandler());
49     return JSEventHandler::GetScriptState();
50   }
GetWorld()51   DOMWrapperWorld& GetWorld() const override { return *world_; }
52 
53  private:
54   // Implements Step 3. of "get the current value of the event handler".
55   // The compiled v8::Function is returned and |JSEventHandler::event_handler_|
56   // gets initialized with it if lazy compilation succeeds.
57   // Otherwise, v8::Null is returned.
58   // https://html.spec.whatwg.org/C/#getting-the-current-value-of-the-event-handler
59   v8::Local<v8::Value> GetCompiledHandler(EventTarget&);
60 
61   // Lazy compilation for content attribute should be tried only once, but we
62   // cannot see whether it had never tried to compile or it has already failed
63   // when |HasCompiledHandler()| returns false. |did_compile_| is used for
64   // checking that.
65   bool did_compile_;
66   const AtomicString function_name_;
67   String script_body_;
68   String source_url_;
69   TextPosition position_;
70   v8::Isolate* isolate_;
71   scoped_refptr<DOMWrapperWorld> world_;
72 };
73 
74 }  // namespace blink
75 
76 #endif  // THIRD_PARTY_BLINK_RENDERER_BINDINGS_CORE_V8_JS_EVENT_HANDLER_FOR_CONTENT_ATTRIBUTE_H_
77