blob: e0939a99db2d04977b6e5bbb1911a1c92d232ca8 [file] [log] [blame]
// Copyright 2017 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef COBALT_DOM_MUTATION_OBSERVER_TASK_MANAGER_H_
#define COBALT_DOM_MUTATION_OBSERVER_TASK_MANAGER_H_
#include <utility>
#include "base/containers/hash_tables.h"
#include "base/threading/thread_checker.h"
#include "cobalt/script/tracer.h"
namespace cobalt {
namespace dom {
class MutationObserver;
// The Mutation observer spec describes a sequence of steps to "queue a mutation
// observer compound microtask" and "notify mutation observers". These steps
// are implemented in this helper class, which must outlive all
// MutationObserver instances.
// The spec expects an EventLoop implementation, which Cobalt does not currently
// have.
// https://www.w3.org/TR/dom/#mutation-observers
class MutationObserverTaskManager : public script::Traceable {
public:
MutationObserverTaskManager() : task_posted_(false) {}
// These should be called in the constructor/destructor of the
// MutationObserver.
void OnMutationObserverCreated(MutationObserver* observer);
void OnMutationObserverDestroyed(MutationObserver* observer);
// Post a task to notify mutation observers, if one is not already posted.
void QueueMutationObserverMicrotask();
void TraceMembers(script::Tracer* tracer) override;
private:
typedef base::hash_set<MutationObserver*> MutationObserverSet;
// Notify all mutation observers.
void NotifyMutationObservers();
THREAD_CHECKER(thread_checker_);
MutationObserverSet observers_;
bool task_posted_;
};
} // namespace dom
} // namespace cobalt
#endif // COBALT_DOM_MUTATION_OBSERVER_TASK_MANAGER_H_