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

Side by Side Diff: third_party/WebKit/Source/modules/webaudio/BaseAudioContext.h

Issue 2913303002: Avoid unsafe heap access from audio thread. (Closed)
Patch Set: improve method documentation Created 3 years, 6 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
« no previous file with comments | « no previous file | third_party/WebKit/Source/modules/webaudio/BaseAudioContext.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2010, Google Inc. All rights reserved. 2 * Copyright (C) 2010, Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after
266 // When a source node has no more processing to do (has finished playing), 266 // When a source node has no more processing to do (has finished playing),
267 // this method tells the context to release the corresponding node. 267 // this method tells the context to release the corresponding node.
268 void NotifySourceNodeFinishedProcessing(AudioHandler*); 268 void NotifySourceNodeFinishedProcessing(AudioHandler*);
269 269
270 // Called at the start of each render quantum. 270 // Called at the start of each render quantum.
271 void HandlePreRenderTasks(const AudioIOPosition& output_position); 271 void HandlePreRenderTasks(const AudioIOPosition& output_position);
272 272
273 // Called at the end of each render quantum. 273 // Called at the end of each render quantum.
274 void HandlePostRenderTasks(); 274 void HandlePostRenderTasks();
275 275
276 // Called periodically at the end of each render quantum to release
277 // finished source nodes. Updates m_finishedSourceNodes with nodes
278 // to be deleted. Returns true if any node needs deletion. Must be
279 // run from the audio thread.
280 bool ReleaseFinishedSourceNodes();
281
282 // The finished source nodes found by |releaseFinishedSourceNodes|
283 // will be removed on the main thread, which is done here.
284 void RemoveFinishedSourceNodes(bool needs_removal);
285
286 // Keeps track of the number of connections made. 276 // Keeps track of the number of connections made.
287 void IncrementConnectionCount() { 277 void IncrementConnectionCount() {
288 DCHECK(IsMainThread()); 278 DCHECK(IsMainThread());
289 connection_count_++; 279 connection_count_++;
290 } 280 }
291 281
292 unsigned ConnectionCount() const { return connection_count_; } 282 unsigned ConnectionCount() const { return connection_count_; }
293 283
294 DeferredTaskHandler& GetDeferredTaskHandler() const { 284 DeferredTaskHandler& GetDeferredTaskHandler() const {
295 return *deferred_task_handler_; 285 return *deferred_task_handler_;
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
406 kAutoplayStatusCount 396 kAutoplayStatusCount
407 }; 397 };
408 398
409 bool is_cleared_; 399 bool is_cleared_;
410 void Clear(); 400 void Clear();
411 401
412 // When the context goes away, there might still be some sources which 402 // When the context goes away, there might still be some sources which
413 // haven't finished playing. Make sure to release them here. 403 // haven't finished playing. Make sure to release them here.
414 void ReleaseActiveSourceNodes(); 404 void ReleaseActiveSourceNodes();
415 405
416 // Actually remove the nodes noted for deletion by
417 // releaseFinishedSourceNodes. Must be run from the main thread,
418 // and must not be run with the context lock.
419 void RemoveFinishedSourceNodesOnMainThread();
420
421 // Returns the Document wich wich the instance is associated. 406 // Returns the Document wich wich the instance is associated.
422 Document* GetDocument() const; 407 Document* GetDocument() const;
423 408
424 // Returns the AutoplayPolicy currently applying to this instance. 409 // Returns the AutoplayPolicy currently applying to this instance.
425 AutoplayPolicy::Type GetAutoplayPolicy() const; 410 AutoplayPolicy::Type GetAutoplayPolicy() const;
426 411
427 // Returns whether the autoplay requirements are fulfilled. 412 // Returns whether the autoplay requirements are fulfilled.
428 bool AreAutoplayRequirementsFulfilled() const; 413 bool AreAutoplayRequirementsFulfilled() const;
429 414
430 // Listener for the PannerNodes 415 // Listener for the PannerNodes
431 Member<AudioListener> listener_; 416 Member<AudioListener> listener_;
432 417
433 // Only accessed in the audio thread. 418 // Accessed by audio thread and main thread, coordinated using
419 // the associated mutex.
420 //
434 // These raw pointers are safe because AudioSourceNodes in 421 // These raw pointers are safe because AudioSourceNodes in
435 // m_activeSourceNodes own them. 422 // active_source_nodes_ own them.
423 Mutex finished_source_handlers_mutex_;
436 Vector<AudioHandler*> finished_source_handlers_; 424 Vector<AudioHandler*> finished_source_handlers_;
437 425
438 // List of source nodes. This is either accessed when the graph lock is 426 // List of source nodes. This is either accessed when the graph lock is
439 // held, or on the main thread when the audio thread has finished. 427 // held, or on the main thread when the audio thread has finished.
440 // Oilpan: This Vector holds connection references. We must call 428 // Oilpan: This Vector holds connection references. We must call
441 // AudioHandler::makeConnection when we add an AudioNode to this, and must 429 // AudioHandler::makeConnection when we add an AudioNode to this, and must
442 // call AudioHandler::breakConnection() when we remove an AudioNode from 430 // call AudioHandler::breakConnection() when we remove an AudioNode from
443 // this. 431 // this.
444 HeapVector<Member<AudioNode>> active_source_nodes_; 432 HeapVector<Member<AudioNode>> active_source_nodes_;
445 433
446 // The main thread controls m_activeSourceNodes, all updates and additions 434 // Called by the audio thread to handle Promises for resume() and suspend(),
447 // are performed by it. When the audio thread marks a source node as finished, 435 // posting a main thread task to perform the actual resolving, if needed.
448 // the nodes are added to |m_finishedSourceNodes| and scheduled for removal 436 //
449 // from |m_activeSourceNodes| by the main thread. 437 // TODO(dominicc): Move to AudioContext because only it creates
450 HashSet<UntracedMember<AudioNode>> finished_source_nodes_; 438 // these Promises.
439 void ResolvePromisesForResume();
451 440
452 // FIXME(dominicc): Move these to AudioContext because only 441 // The audio thread relies on the main thread to perform some operations
453 // it creates these Promises. 442 // over the objects that it owns and controls; |ScheduleMainThreadCleanup()|
454 // Handle Promises for resume() and suspend() 443 // posts the task to initiate those.
455 void ResolvePromisesForResume(); 444 //
456 void ResolvePromisesForResumeOnMainThread(); 445 // That is, we combine all those sub-tasks into one task action for
446 // convenience and performance, |PerformCleanupOnMainThread()|. It handles
447 // promise resolving, stopping and finishing up of audio source nodes etc.
448 // Actions that should happen, but can happen asynchronously to the
449 // audio thread making rendering progress.
450 void ScheduleMainThreadCleanup();
451 void PerformCleanupOnMainThread();
457 452
458 // When the context is going away, reject any pending script promise 453 // When the context is going away, reject any pending script promise
459 // resolvers. 454 // resolvers.
460 virtual void RejectPendingResolvers(); 455 virtual void RejectPendingResolvers();
461 456
462 // Record the current autoplay status and clear it. 457 // Record the current autoplay status and clear it.
463 void RecordAutoplayStatus(); 458 void RecordAutoplayStatus();
464 459
465 // True if we're in the process of resolving promises for resume(). Resolving 460 // True if we're in the process of resolving promises for resume(). Resolving
466 // can take some time and the audio context process loop is very fast, so we 461 // can take some time and the audio context process loop is very fast, so we
467 // don't want to call resolve an excessive number of times. 462 // don't want to call resolve an excessive number of times.
468 bool is_resolving_resume_promises_; 463 bool is_resolving_resume_promises_;
469 464
465 // Set to |true| by the audio thread when it posts a main-thread task to
466 // perform delayed state sync'ing updates that needs to be done on the main
467 // thread. Cleared by the main thread task once it has run.
468 bool has_posted_cleanup_task_;
469
470 // Whether a user gesture is required to start this AudioContext. 470 // Whether a user gesture is required to start this AudioContext.
471 bool user_gesture_required_; 471 bool user_gesture_required_;
472 472
473 unsigned connection_count_; 473 unsigned connection_count_;
474 474
475 // Graph locking. 475 // Graph locking.
476 RefPtr<DeferredTaskHandler> deferred_task_handler_; 476 RefPtr<DeferredTaskHandler> deferred_task_handler_;
477 477
478 // The state of the BaseAudioContext. 478 // The state of the BaseAudioContext.
479 AudioContextState context_state_; 479 AudioContextState context_state_;
(...skipping 28 matching lines...) Expand all
508 // It is somewhat arbitrary and could be increased if necessary. 508 // It is somewhat arbitrary and could be increased if necessary.
509 enum { kMaxNumberOfChannels = 32 }; 509 enum { kMaxNumberOfChannels = 32 };
510 510
511 Optional<AutoplayStatus> autoplay_status_; 511 Optional<AutoplayStatus> autoplay_status_;
512 AudioIOPosition output_position_; 512 AudioIOPosition output_position_;
513 }; 513 };
514 514
515 } // namespace blink 515 } // namespace blink
516 516
517 #endif // BaseAudioContext_h 517 #endif // BaseAudioContext_h
OLDNEW
« no previous file with comments | « no previous file | third_party/WebKit/Source/modules/webaudio/BaseAudioContext.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698