| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 "core/dom/IntersectionObserver.h" | 5 #include "core/dom/IntersectionObserver.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/ExceptionState.h" | 7 #include "bindings/core/v8/ExceptionState.h" |
| 8 #include "core/css/parser/CSSParserTokenRange.h" | 8 #include "core/css/parser/CSSParserTokenRange.h" |
| 9 #include "core/css/parser/CSSTokenizer.h" | 9 #include "core/css/parser/CSSTokenizer.h" |
| 10 #include "core/dom/Element.h" | 10 #include "core/dom/Element.h" |
| (...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 if (rootIsImplicit()) { | 223 if (rootIsImplicit()) { |
| 224 DCHECK(m_callback->getExecutionContext()); | 224 DCHECK(m_callback->getExecutionContext()); |
| 225 return *toDocument(m_callback->getExecutionContext()); | 225 return *toDocument(m_callback->getExecutionContext()); |
| 226 } | 226 } |
| 227 DCHECK(root()); | 227 DCHECK(root()); |
| 228 return root()->document(); | 228 return root()->document(); |
| 229 } | 229 } |
| 230 | 230 |
| 231 void IntersectionObserver::observe(Element* target, | 231 void IntersectionObserver::observe(Element* target, |
| 232 ExceptionState& exceptionState) { | 232 ExceptionState& exceptionState) { |
| 233 if (!rootIsValid()) { | 233 if (!rootIsValid()) |
| 234 exceptionState.throwDOMException( | |
| 235 InvalidStateError, | |
| 236 "observe() called on an IntersectionObserver with an invalid root."); | |
| 237 return; | 234 return; |
| 238 } | |
| 239 | 235 |
| 240 if (!target || root() == target) | 236 if (!target || root() == target) |
| 241 return; | 237 return; |
| 242 | 238 |
| 243 LocalFrame* targetFrame = target->document().frame(); | 239 LocalFrame* targetFrame = target->document().frame(); |
| 244 if (!targetFrame) | 240 if (!targetFrame) |
| 245 return; | 241 return; |
| 246 | 242 |
| 247 if (target->ensureIntersectionObserverData().getObservationFor(*this)) | 243 if (target->ensureIntersectionObserverData().getObservationFor(*this)) |
| 248 return; | 244 return; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 280 for (auto& observation : m_observations) | 276 for (auto& observation : m_observations) |
| 281 observation->setLastThresholdIndex(std::numeric_limits<unsigned>::max()); | 277 observation->setLastThresholdIndex(std::numeric_limits<unsigned>::max()); |
| 282 } | 278 } |
| 283 | 279 |
| 284 if (FrameView* frameView = targetFrame->view()) | 280 if (FrameView* frameView = targetFrame->view()) |
| 285 frameView->scheduleAnimation(); | 281 frameView->scheduleAnimation(); |
| 286 } | 282 } |
| 287 | 283 |
| 288 void IntersectionObserver::unobserve(Element* target, | 284 void IntersectionObserver::unobserve(Element* target, |
| 289 ExceptionState& exceptionState) { | 285 ExceptionState& exceptionState) { |
| 290 if (!rootIsValid()) { | |
| 291 exceptionState.throwDOMException( | |
| 292 InvalidStateError, | |
| 293 "unobserve() called on an IntersectionObserver with an invalid root."); | |
| 294 return; | |
| 295 } | |
| 296 | |
| 297 if (!target || !target->intersectionObserverData()) | 286 if (!target || !target->intersectionObserverData()) |
| 298 return; | 287 return; |
| 299 | 288 |
| 300 if (IntersectionObservation* observation = | 289 if (IntersectionObservation* observation = |
| 301 target->intersectionObserverData()->getObservationFor(*this)) | 290 target->intersectionObserverData()->getObservationFor(*this)) { |
| 302 observation->disconnect(); | 291 observation->disconnect(); |
| 292 m_observations.remove(observation); |
| 293 } |
| 303 } | 294 } |
| 304 | 295 |
| 305 void IntersectionObserver::computeIntersectionObservations() { | 296 void IntersectionObserver::computeIntersectionObservations() { |
| 306 if (!rootIsValid()) | 297 if (!rootIsValid()) |
| 307 return; | 298 return; |
| 308 Document* callbackDocument = toDocument(m_callback->getExecutionContext()); | 299 Document* callbackDocument = toDocument(m_callback->getExecutionContext()); |
| 309 if (!callbackDocument) | 300 if (!callbackDocument) |
| 310 return; | 301 return; |
| 311 LocalDOMWindow* callbackDOMWindow = callbackDocument->domWindow(); | 302 LocalDOMWindow* callbackDOMWindow = callbackDocument->domWindow(); |
| 312 if (!callbackDOMWindow) | 303 if (!callbackDOMWindow) |
| 313 return; | 304 return; |
| 314 DOMHighResTimeStamp timestamp = | 305 DOMHighResTimeStamp timestamp = |
| 315 DOMWindowPerformance::performance(*callbackDOMWindow)->now(); | 306 DOMWindowPerformance::performance(*callbackDOMWindow)->now(); |
| 316 for (auto& observation : m_observations) | 307 for (auto& observation : m_observations) |
| 317 observation->computeIntersectionObservations(timestamp); | 308 observation->computeIntersectionObservations(timestamp); |
| 318 } | 309 } |
| 319 | 310 |
| 320 void IntersectionObserver::disconnect(ExceptionState& exceptionState) { | 311 void IntersectionObserver::disconnect(ExceptionState& exceptionState) { |
| 321 if (!rootIsValid()) { | |
| 322 exceptionState.throwDOMException( | |
| 323 InvalidStateError, | |
| 324 "disconnect() called on an IntersectionObserver with an invalid root."); | |
| 325 return; | |
| 326 } | |
| 327 | |
| 328 for (auto& observation : m_observations) | 312 for (auto& observation : m_observations) |
| 329 observation->clearRootAndRemoveFromTarget(); | 313 observation->disconnect(); |
| 330 m_observations.clear(); | 314 m_observations.clear(); |
| 331 } | 315 } |
| 332 | 316 |
| 333 void IntersectionObserver::removeObservation( | |
| 334 IntersectionObservation& observation) { | |
| 335 m_observations.remove(&observation); | |
| 336 } | |
| 337 | |
| 338 void IntersectionObserver::setInitialState(InitialState initialState) { | 317 void IntersectionObserver::setInitialState(InitialState initialState) { |
| 339 DCHECK(m_observations.isEmpty()); | 318 DCHECK(m_observations.isEmpty()); |
| 340 m_initialState = initialState; | 319 m_initialState = initialState; |
| 341 } | 320 } |
| 342 | 321 |
| 343 HeapVector<Member<IntersectionObserverEntry>> IntersectionObserver::takeRecords( | 322 HeapVector<Member<IntersectionObserverEntry>> IntersectionObserver::takeRecords( |
| 344 ExceptionState& exceptionState) { | 323 ExceptionState& exceptionState) { |
| 345 HeapVector<Member<IntersectionObserverEntry>> entries; | 324 HeapVector<Member<IntersectionObserverEntry>> entries; |
| 346 | 325 entries.swap(m_entries); |
| 347 if (!rootIsValid()) { | |
| 348 exceptionState.throwDOMException(InvalidStateError, | |
| 349 "takeRecords() called on an " | |
| 350 "IntersectionObserver with an invalid " | |
| 351 "root."); | |
| 352 } else { | |
| 353 entries.swap(m_entries); | |
| 354 } | |
| 355 | |
| 356 return entries; | 326 return entries; |
| 357 } | 327 } |
| 358 | 328 |
| 359 static void appendLength(StringBuilder& stringBuilder, const Length& length) { | 329 static void appendLength(StringBuilder& stringBuilder, const Length& length) { |
| 360 stringBuilder.appendNumber(length.intValue()); | 330 stringBuilder.appendNumber(length.intValue()); |
| 361 if (length.type() == Percent) | 331 if (length.type() == Percent) |
| 362 stringBuilder.append('%'); | 332 stringBuilder.append('%'); |
| 363 else | 333 else |
| 364 stringBuilder.append("px", 2); | 334 stringBuilder.append("px", 2); |
| 365 } | 335 } |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 402 | 372 |
| 403 DEFINE_TRACE(IntersectionObserver) { | 373 DEFINE_TRACE(IntersectionObserver) { |
| 404 visitor->template registerWeakMembers< | 374 visitor->template registerWeakMembers< |
| 405 IntersectionObserver, &IntersectionObserver::clearWeakMembers>(this); | 375 IntersectionObserver, &IntersectionObserver::clearWeakMembers>(this); |
| 406 visitor->trace(m_callback); | 376 visitor->trace(m_callback); |
| 407 visitor->trace(m_observations); | 377 visitor->trace(m_observations); |
| 408 visitor->trace(m_entries); | 378 visitor->trace(m_entries); |
| 409 } | 379 } |
| 410 | 380 |
| 411 } // namespace blink | 381 } // namespace blink |
| OLD | NEW |