OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "config.h" | 5 #include "config.h" |
6 #include "core/html/shadow/PluginPlaceholderElement.h" | 6 #include "core/html/shadow/PluginPlaceholderElement.h" |
7 | 7 |
8 #include "bindings/core/v8/V8PluginPlaceholderElement.h" | 8 #include "bindings/core/v8/V8PluginPlaceholderElement.h" |
| 9 #include "core/CSSPropertyNames.h" |
| 10 #include "core/CSSValueKeywords.h" |
| 11 #include "core/EventTypeNames.h" |
9 #include "core/dom/Document.h" | 12 #include "core/dom/Document.h" |
| 13 #include "core/events/Event.h" |
| 14 #include "core/events/EventListener.h" |
| 15 #include "core/html/HTMLStyleElement.h" |
| 16 #include "platform/heap/Visitor.h" |
10 #include "wtf/Assertions.h" | 17 #include "wtf/Assertions.h" |
| 18 #include "wtf/Functional.h" |
| 19 #include "wtf/StdLibExtras.h" |
| 20 #include "wtf/text/AtomicString.h" |
11 | 21 |
12 namespace blink { | 22 namespace blink { |
| 23 namespace { |
| 24 |
| 25 // FIXME: Move this into a resource and @import it. |
| 26 const char kStyleSource[] = |
| 27 "#plugin-placeholder {" |
| 28 " all: initial;" |
| 29 " width: 100%;" |
| 30 " height: 100%;" |
| 31 " overflow: hidden;" |
| 32 " display: flex;" |
| 33 " align-items: center;" |
| 34 " background: gray;" |
| 35 " font: 12px -webkit-control;" |
| 36 "}" |
| 37 "#plugin-placeholder-content {" |
| 38 " text-align: center;" |
| 39 " margin: auto;" |
| 40 "}"; |
| 41 |
| 42 // Invokes a closure when an event occurs. |
| 43 // Take care to use a WeakPtr where necessary to avoid reference cycles. |
| 44 class SimpleEventListener : public EventListener { |
| 45 public: |
| 46 static PassRefPtr<SimpleEventListener> create(PassOwnPtr<Closure> closure) |
| 47 { |
| 48 return adoptRef(new SimpleEventListener(closure)); |
| 49 } |
| 50 |
| 51 ~SimpleEventListener() override { } |
| 52 |
| 53 private: |
| 54 SimpleEventListener(PassOwnPtr<Closure> closure) |
| 55 : EventListener(CPPEventListenerType) |
| 56 , m_closure(closure) |
| 57 { |
| 58 } |
| 59 |
| 60 bool operator==(const EventListener& other) override { return this == &other
; } |
| 61 |
| 62 void handleEvent(ExecutionContext*, Event*) override |
| 63 { |
| 64 (*m_closure)(); |
| 65 } |
| 66 |
| 67 OwnPtr<Closure> m_closure; |
| 68 }; |
| 69 |
| 70 } // namespace |
13 | 71 |
14 PluginPlaceholderElement::PluginPlaceholderElement(Document& document) | 72 PluginPlaceholderElement::PluginPlaceholderElement(Document& document) |
15 : HTMLDivElement(document) | 73 : HTMLDivElement(document) |
| 74 , m_weakPtrFactory(this) |
16 { | 75 { |
17 } | 76 } |
18 | 77 |
19 PassRefPtrWillBeRawPtr<PluginPlaceholderElement> PluginPlaceholderElement::creat
e(Document& document) | 78 PluginPlaceholderElement::~PluginPlaceholderElement() |
20 { | 79 { |
21 RefPtrWillBeRawPtr<PluginPlaceholderElement> element = adoptRefWillBeNoop(ne
w PluginPlaceholderElement(document)); | 80 } |
22 bool success = V8PluginPlaceholderElement::PrivateScript::createdCallbackMet
hod(document.frame(), element.get()); | 81 |
23 ASSERT_UNUSED(success, success); | 82 void PluginPlaceholderElement::trace(Visitor* visitor) |
24 return element.release(); | 83 { |
| 84 visitor->trace(m_messageElement); |
| 85 visitor->trace(m_closeButton); |
| 86 HTMLDivElement::trace(visitor); |
25 } | 87 } |
26 | 88 |
27 void PluginPlaceholderElement::setMessage(const String& message) | 89 void PluginPlaceholderElement::setMessage(const String& message) |
28 { | 90 { |
29 bool success = V8PluginPlaceholderElement::PrivateScript::messageAttributeSe
tter(document().frame(), this, message); | 91 m_messageElement->setTextContent(message); |
30 ASSERT_UNUSED(success, success); | |
31 } | 92 } |
32 | 93 |
33 void PluginPlaceholderElement::setIsCloseable(bool closeable) | 94 void PluginPlaceholderElement::setIsCloseable(bool closeable) |
34 { | 95 { |
35 bool success = V8PluginPlaceholderElement::PrivateScript::closeableAttribute
Setter(document().frame(), this, closeable); | 96 if (closeable) |
| 97 m_closeButton->removeInlineStyleProperty(CSSPropertyDisplay); |
| 98 else |
| 99 m_closeButton->setInlineStyleProperty(CSSPropertyDisplay, CSSValueNone); |
| 100 } |
| 101 |
| 102 void PluginPlaceholderElement::initialize() |
| 103 { |
| 104 DEFINE_STATIC_LOCAL(AtomicString, elementId, ("plugin-placeholder", AtomicSt
ring::ConstructFromLiteral)); |
| 105 setIdAttribute(elementId); |
| 106 |
| 107 RefPtrWillBeRawPtr<HTMLStyleElement> styleElement = HTMLStyleElement::create
(document(), false); |
| 108 styleElement->setTextContent(kStyleSource); |
| 109 |
| 110 DEFINE_STATIC_LOCAL(AtomicString, contentElementId, ("plugin-placeholder-con
tent", AtomicString::ConstructFromLiteral)); |
| 111 RefPtrWillBeRawPtr<HTMLDivElement> contentElement = HTMLDivElement::create(d
ocument()); |
| 112 contentElement->setIdAttribute(contentElementId); |
| 113 |
| 114 DEFINE_STATIC_LOCAL(AtomicString, messageElementId, ("plugin-placeholder-mes
sage", AtomicString::ConstructFromLiteral)); |
| 115 RefPtrWillBeRawPtr<HTMLDivElement> messageElement = HTMLDivElement::create(d
ocument()); |
| 116 messageElement->setIdAttribute(messageElementId); |
| 117 |
| 118 // FIXME: UI polish, l10n, etc. for the close button. |
| 119 DEFINE_STATIC_LOCAL(AtomicString, closeButtonId, ("plugin-placeholder-close-
button", AtomicString::ConstructFromLiteral)); |
| 120 RefPtrWillBeRawPtr<HTMLButtonElement> closeButton = HTMLButtonElement::creat
e(document(), nullptr); |
| 121 closeButton->setIdAttribute(closeButtonId); |
| 122 closeButton->setTextContent("Close"); |
| 123 closeButton->setInlineStyleProperty(CSSPropertyDisplay, CSSValueNone); |
| 124 closeButton->addEventListener(EventTypeNames::click, createSimpleEventListen
er(&PluginPlaceholderElement::onCloseButtonClick)); |
| 125 |
| 126 contentElement->appendChild(messageElement); |
| 127 contentElement->appendChild(closeButton); |
| 128 appendChild(styleElement); |
| 129 appendChild(contentElement); |
| 130 |
| 131 m_messageElement = messageElement.release(); |
| 132 m_closeButton = closeButton.release(); |
| 133 } |
| 134 |
| 135 PassRefPtr<EventListener> PluginPlaceholderElement::createSimpleEventListener(vo
id(PluginPlaceholderElement::*method)()) |
| 136 { |
| 137 return SimpleEventListener::create(bind(method, m_weakPtrFactory.createWeakP
tr())); |
| 138 } |
| 139 |
| 140 void PluginPlaceholderElement::onCloseButtonClick() |
| 141 { |
| 142 // FIXME: Record UMA Plugin_Hide_Click or an equivalent UseCounter. |
| 143 bool success = V8PluginPlaceholderElement::PrivateScript::hideMethod(documen
t().frame(), this); |
36 ASSERT_UNUSED(success, success); | 144 ASSERT_UNUSED(success, success); |
37 } | 145 } |
38 | 146 |
39 } // namespace blink | 147 } // namespace blink |
OLD | NEW |