Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(288)

Side by Side Diff: Source/bindings/dart/DartCustomElement.cpp

Issue 24294002: Dartium changes for custom element lifecycle events. (Closed) Base URL: svn://svn.chromium.org/multivm/trunk/webkit
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2013, Google Inc.
2 // 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 #include "config.h"
31 #include "DartCustomElement.h"
32
33 namespace WebCore {
34
35 Dart_Handle DartCustomElement::createWrapper(Element* impl, CreateWrapperFunctio n createWrapper)
36 {
37 if (!impl)
38 return Dart_Null();
39
40 HTMLElement* element = reinterpret_cast<HTMLElement*>(impl);
41
42 ASSERT(!DartDOMWrapper::lookupWrapper(DartHTMLElement::isNode, element));
43
44 // FIXME: Streamline the common path. If an element is already upgraded prop erly,
45 // we shouldn't need all this checking.
46
47 // Look up custom Dart type.
48 DartDOMData* domData = DartDOMData::current();
49 String name = element->localName();
50 DartCustomElementMap* map = domData->customElementMap();
51 Dart_PersistentHandle persistent = map->get(name);
52 if (!persistent) {
53 // Not a custom tag. Check for type extension.
54 if (element->hasAttribute("is")) {
55 name = element->getAttribute("is");
56 persistent = map->get(name);
57 }
58
59 // No custom type. Fall back onto builtin wrapper type.
60 if (!persistent) {
61 return createWrapper.invoke(element);
62 }
63 }
64
65 Dart_Handle type = Dart_HandleFromPersistent(persistent);
66 ASSERT(!Dart_IsError(type));
67
68 DartCustomElementMap* nativeMap = domData->customBaseElementMap();
69 Dart_Handle expectedNativeType = Dart_HandleFromPersistent(nativeMap->get(na me));
70 ASSERT(!Dart_IsError(expectedNativeType));
71
72 // FIXME: Avoid allocating the fallback wrapper until we know we need it.
73 Dart_Handle fallback = createWrapper.invoke(element);
74 Dart_Handle actualNativeType = Dart_InstanceGetType(fallback);
75 ASSERT(!Dart_IsError(actualNativeType));
76
77 if (!Dart_IdentityEquals(actualNativeType, expectedNativeType)) {
78 // The registered custom type does not match. Just fallback.
79 return fallback;
80 }
81 DartDOMWrapper::disassociateWrapper<DartHTMLElement>(element, fallback);
82
83 Dart_Handle newInstance = Dart_Allocate(type);
84 ASSERT(!Dart_IsError(newInstance));
85
86 Dart_Handle result = Dart_SetNativeInstanceField(newInstance, 0, reinterpret _cast<intptr_t>(element));
87 UNUSED_PARAM(result);
88 ASSERT(!Dart_IsError(result));
89
90 DartDOMWrapper::associateWrapper<DartHTMLElement>(element, newInstance);
91 Dart_Invoke(newInstance, Dart_NewStringFromCString("created"), 0, 0);
92 return newInstance;
93 }
94
95 Dart_WeakPersistentHandle DartCustomElement::lookupWrapper(
96 DartDOMData* domData, Dart_WeakPersistentHandle wrapper, Element* impl)
97 {
98 ASSERT(impl);
99 ASSERT(wrapper);
100
101 HTMLElement* element = reinterpret_cast<HTMLElement*>(impl);
102
103 // FIXME: Streamline the common path. If an element is already upgraded prop erly,
104 // we shouldn't need all this checking.
105
106 // Look up custom Dart type.
107 String name = element->localName();
108 DartCustomElementMap* map = domData->customElementMap();
109 Dart_PersistentHandle persistent = map->get(name);
110 if (!persistent) {
111 // Not a custom tag. Check for type extension.
112 if (element->hasAttribute("is")) {
113 name = element->getAttribute("is");
114 persistent = map->get(name);
115 }
116
117 // No custom type. Fall back onto builtin wrapper type.
118 if (!persistent) {
119 return wrapper;
120 }
121 }
122 // Previous wrapper exists. Test for upgrade.
123 Dart_Handle type = Dart_HandleFromPersistent(persistent);
124 Dart_Handle oldInstance = Dart_HandleFromWeakPersistent(wrapper);
125 Dart_Handle oldType = Dart_InstanceGetType(oldInstance);
126 ASSERT(!Dart_IsError(oldType));
127 if (Dart_IdentityEquals(oldType, type))
128 return wrapper;
129
130 DartCustomElementMap* nativeMap = domData->customBaseElementMap();
131 Dart_Handle expectedNativeType = Dart_HandleFromPersistent(nativeMap->get(na me));
132 ASSERT(!Dart_IsError(expectedNativeType));
133
134 if (!Dart_IdentityEquals(oldType, expectedNativeType)) {
135 // The registered custom type does not match. Just fallback.
136 return wrapper;
137 }
138
139 return 0;
140 }
141
142 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698