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 #ifndef THIRD_PARTY_BLINK_RENDERER_CORE_RESIZE_OBSERVER_RESIZE_OBSERVER_H_ 6 #define THIRD_PARTY_BLINK_RENDERER_CORE_RESIZE_OBSERVER_RESIZE_OBSERVER_H_ 7 8 #include "third_party/blink/renderer/bindings/core/v8/active_script_wrappable.h" 9 #include "third_party/blink/renderer/core/core_export.h" 10 #include "third_party/blink/renderer/core/execution_context/execution_context_lifecycle_observer.h" 11 #include "third_party/blink/renderer/core/resize_observer/resize_observer_box_options.h" 12 #include "third_party/blink/renderer/platform/bindings/script_wrappable.h" 13 #include "third_party/blink/renderer/platform/heap/handle.h" 14 15 namespace blink { 16 17 class Element; 18 class LocalDOMWindow; 19 class ResizeObserverController; 20 class ResizeObserverEntry; 21 class ResizeObservation; 22 class ResizeObserverOptions; 23 class ScriptState; 24 class V8ResizeObserverCallback; 25 26 // ResizeObserver represents ResizeObserver javascript api: 27 // https://github.com/WICG/ResizeObserver/ 28 class CORE_EXPORT ResizeObserver final 29 : public ScriptWrappable, 30 public ActiveScriptWrappable<ResizeObserver>, 31 public ExecutionContextClient { 32 DEFINE_WRAPPERTYPEINFO(); 33 34 public: 35 // This delegate is an internal (non-web-exposed) version of ResizeCallback. 36 class Delegate : public GarbageCollected<Delegate> { 37 public: 38 virtual ~Delegate() = default; 39 virtual void OnResize( 40 const HeapVector<Member<ResizeObserverEntry>>& entries) = 0; Trace(Visitor * visitor)41 virtual void Trace(Visitor* visitor) const {} 42 }; 43 44 static ResizeObserver* Create(ScriptState*, V8ResizeObserverCallback*); 45 static ResizeObserver* Create(LocalDOMWindow*, Delegate*); 46 47 ResizeObserver(V8ResizeObserverCallback*, LocalDOMWindow*); 48 ResizeObserver(Delegate*, LocalDOMWindow*); 49 ~ResizeObserver() override = default; 50 51 // API methods 52 void observe(Element*, const ResizeObserverOptions* options); 53 void observe(Element*); 54 void unobserve(Element*); 55 void disconnect(); 56 57 // Returns depth of shallowest observed node, kDepthLimit if none. 58 size_t GatherObservations(size_t deeper_than); SkippedObservations()59 bool SkippedObservations() { return skipped_observations_; } 60 void DeliverObservations(); 61 void ClearObservations(); 62 63 ResizeObserverBoxOptions ParseBoxOptions(const String& box_options); 64 65 // ScriptWrappable override: 66 bool HasPendingActivity() const override; 67 68 void Trace(Visitor*) const override; 69 70 private: 71 void observeInternal(Element* target, ResizeObserverBoxOptions box_option); 72 73 using ObservationList = HeapLinkedHashSet<WeakMember<ResizeObservation>>; 74 75 // Either of |callback_| and |delegate_| should be non-null. 76 const Member<V8ResizeObserverCallback> callback_; 77 const Member<Delegate> delegate_; 78 79 // List of Elements we are observing. These Elements make the ResizeObserver 80 // and most-importantly |callback_| alive. If |observations_| is empty, no one 81 // is performing wrapper-tracing and |callback_| might already be gone. 82 ObservationList observations_; 83 // List of elements that have changes 84 HeapVector<Member<ResizeObservation>> active_observations_; 85 // True if observations were skipped gatherObservations 86 bool skipped_observations_; 87 88 WeakMember<ResizeObserverController> controller_; 89 }; 90 91 } // namespace blink 92 93 #endif // THIRD_PARTY_BLINK_RENDERER_CORE_RESIZE_OBSERVER_RESIZE_OBSERVER_H_ 94