blob: 1b429e2097e8dedf48ee031e89b4a5501f3ff925 [file] [log] [blame]
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <string>
#include "base/macros.h"
#include "base/values.h"
namespace content {
class WebUI;
namespace chromeos {
// A helper class to store deferred Javascript calls, shared by subclasses of
// BaseWebUIHandler.
class JSCallsContainer {
// An event is a message/JS call to or from WebUI.
struct Event {
enum class Type {
// This event was sent from C++ to JS.
// This event was sent from JS to C++.
Event(Type type,
const std::string& function_name,
std::vector<base::Value>&& arguments);
Event(const Event&) = delete;
Event& operator=(const Event&) = delete;
Type type;
std::string function_name;
std::vector<base::Value> arguments;
JSCallsContainer(const JSCallsContainer&) = delete;
JSCallsContainer& operator=(const JSCallsContainer&) = delete;
// Used to decide whether the JS call should be deferred.
bool is_initialized() const { return is_initialized_; }
// Enable event recording.
void set_record_all_events_for_test() { record_all_events_for_test_ = true; }
// If true then all JS calls should be recorded.
bool record_all_events_for_test() const {
return record_all_events_for_test_;
// Recorded events. This is mutable and can be modified.
std::vector<Event>* events() { return &events_; }
// Executes Javascript calls that were deferred while the instance was not
// initialized yet.
void ExecuteDeferredJSCalls(content::WebUI* web_ui);
// Whether the instance is initialized.
// The instance becomes initialized after the corresponding message is
// received from Javascript side.
bool is_initialized_ = false;
// Decide if incoming and outgoing JS calls should be recorded. Recording
// should only be used for tests.
bool record_all_events_for_test_ = false;
std::vector<Event> events_;
} // namespace chromeos