Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "core/observer/ResizeObserver.h" | |
| 6 | |
| 7 #include "core/dom/Element.h" | |
| 8 #include "core/frame/FrameView.h" | |
| 9 #include "core/frame/LocalFrame.h" | |
| 10 #include "core/observer/ResizeObservation.h" | |
| 11 #include "core/observer/ResizeObserverCallback.h" | |
| 12 #include "core/observer/ResizeObserverController.h" | |
| 13 #include "core/observer/ResizeObserverEntry.h" | |
| 14 | |
| 15 namespace blink { | |
| 16 | |
| 17 ResizeObserver* ResizeObserver::create(Document& document, ResizeObserverCallbac k* callback) | |
| 18 { | |
| 19 auto observer = new ResizeObserver(callback); | |
| 20 document.ensureResizeObserverController().addObserver(*observer); | |
| 21 return observer; | |
| 22 } | |
| 23 | |
| 24 ResizeObserver::ResizeObserver(ResizeObserverCallback* callback) | |
| 25 : m_callback(callback) | |
| 26 { | |
| 27 } | |
| 28 | |
| 29 void ResizeObserver::observe(Element* target) | |
| 30 { | |
| 31 auto observerMap = target ? target->resizeObserverData() : nullptr; | |
| 32 if (observerMap && observerMap->contains(this)) | |
| 33 return; // Already registered. | |
| 34 m_observations.add(new ResizeObservation(target, this)); | |
| 35 | |
| 36 LocalFrame* frame = target->document().frame(); | |
|
szager1
2016/07/11 18:33:47
nit:
if (FrameView* frameView = document().view()
atotic1
2016/07/11 23:18:58
Done.
| |
| 37 if (frame) { | |
| 38 if (FrameView* frameView = frame->view()) | |
| 39 frameView->scheduleAnimation(); | |
| 40 } | |
| 41 } | |
| 42 | |
| 43 void ResizeObserver::unobserve(Element* target) | |
| 44 { | |
| 45 auto observerMap = target ? target->resizeObserverData() : nullptr; | |
| 46 if (!observerMap) | |
| 47 return; | |
| 48 auto observation = observerMap->find(this); | |
| 49 if (observation != observerMap->end()) { | |
| 50 m_observations.remove((*observation).value); | |
| 51 observerMap->remove(observation); | |
| 52 } | |
| 53 } | |
| 54 | |
| 55 void ResizeObserver::disconnect() | |
| 56 { | |
| 57 ObservationList observations; | |
| 58 for (auto it = m_observations.begin(); it != m_observations.end(); ++it) | |
|
szager1
2016/07/11 18:33:47
nit:
m_observations.swap(observations);
atotic1
2016/07/11 23:18:58
Done.
| |
| 59 observations.add(*it); | |
| 60 | |
| 61 m_observations.clear(); | |
|
szager1
2016/07/11 18:33:47
If you use swap(), this line is unnecessary.
atotic1
2016/07/11 23:18:58
Done.
| |
| 62 for (auto observation : observations) { | |
| 63 Element* target = (*observation).target(); | |
| 64 if (target) { | |
| 65 auto& observerMap = target->ensureResizeObserverData(); | |
|
szager1
2016/07/11 18:33:47
Can't you just:
target->ensureResizeObserverData(
atotic1
2016/07/11 23:18:58
Wow, you can. Still rusty with my C++ collections.
| |
| 66 observerMap.remove(observerMap.find(this)); | |
| 67 } | |
| 68 } | |
| 69 m_activeObservations.clear(); | |
| 70 } | |
| 71 | |
| 72 bool ResizeObserver::gatherObservations() | |
| 73 { | |
| 74 DCHECK(m_activeObservations.isEmpty()); | |
| 75 | |
| 76 for (auto& observation : m_observations) { | |
| 77 if (observation->hasResized()) | |
| 78 m_activeObservations.append(*observation); | |
| 79 } | |
| 80 | |
| 81 return !m_activeObservations.isEmpty(); | |
| 82 } | |
| 83 | |
| 84 void ResizeObserver::deliverObservations() | |
| 85 { | |
| 86 if (m_activeObservations.size() == 0) | |
| 87 return; | |
| 88 | |
| 89 HeapVector<Member<ResizeObserverEntry>> entries; | |
| 90 | |
| 91 for (auto& observation : m_activeObservations) { | |
| 92 auto entry = new ResizeObserverEntry(observation->target()); | |
| 93 entries.append(entry); | |
| 94 observation->setBroadcastSize(entry->contentSize()); | |
| 95 } | |
| 96 m_callback->handleEvent(entries, this); | |
| 97 m_activeObservations.clear(); | |
| 98 } | |
| 99 | |
| 100 void ResizeObserver::clearObservations() | |
| 101 { | |
| 102 m_activeObservations.clear(); | |
| 103 } | |
| 104 | |
| 105 DEFINE_TRACE(ResizeObserver) | |
| 106 { | |
| 107 visitor->trace(m_callback); | |
| 108 visitor->trace(m_observations); | |
| 109 visitor->trace(m_activeObservations); | |
| 110 } | |
| 111 | |
| 112 } // namespace blink | |
| OLD | NEW |