chromium/content/public/test/mock_render_thread.cc

// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/public/test/mock_render_thread.h"

#include <memory>
#include <tuple>

#include "base/logging.h"
#include "base/task/single_thread_task_runner.h"
#include "base/unguessable_token.h"
#include "build/build_config.h"
#include "content/common/associated_interfaces.mojom.h"
#include "content/common/frame.mojom.h"
#include "content/public/renderer/render_thread_observer.h"
#include "content/renderer/render_thread_impl.h"
#include "content/test/test_render_frame.h"
#include "ipc/ipc_message_utils.h"
#include "ipc/ipc_sync_message.h"
#include "ipc/message_filter.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/dom_storage/session_storage_namespace_id.h"
#include "third_party/blink/public/common/user_agent/user_agent_metadata.h"
#include "third_party/blink/public/mojom/page/widget.mojom.h"
#include "third_party/blink/public/mojom/widget/platform_widget.mojom.h"

namespace content {

namespace {

// Some tests hard-code small numbers for routing IDs, so make reasonably sure
// that the IDs generated by MockRenderThread will not clash.
constexpr int32_t kFirstGeneratedRoutingId =;

static const blink::UserAgentMetadata kUserAgentMetadata;

}  // namespace

MockRenderThread::MockRenderThread()
    :{}

MockRenderThread::~MockRenderThread() {}

#if BUILDFLAG(CONTENT_ENABLE_LEGACY_IPC)
// Called by the Widget. Used to send messages to the browser.
// We short-circuit the mechanism and handle the messages right here on this
// class.
bool MockRenderThread::Send(IPC::Message* msg) {
  // We need to simulate a synchronous channel, thus we are going to receive
  // through this function messages, messages with reply and reply messages.
  // We can only handle one synchronous message at a time.
  if (msg->is_reply()) {
    if (reply_deserializer_) {
      reply_deserializer_->SerializeOutputParameters(*msg);
      reply_deserializer_.reset();
    }
  } else {
    if (msg->is_sync()) {
      // We actually need to handle deleting the reply deserializer for sync
      // messages.
      reply_deserializer_ =
          static_cast<IPC::SyncMessage*>(msg)->TakeReplyDeserializer();
    }
    if (msg->routing_id() == MSG_ROUTING_CONTROL)
      OnControlMessageReceived(*msg);
    else
      OnMessageReceived(*msg);
  }
  delete msg;
  return true;
}

IPC::SyncMessageFilter* MockRenderThread::GetSyncMessageFilter() {
  return nullptr;
}

void MockRenderThread::AddRoute(int32_t routing_id, IPC::Listener* listener) {}

void MockRenderThread::AttachTaskRunnerToRoute(
    int32_t routing_id,
    scoped_refptr<base::SingleThreadTaskRunner> task_runner) {}

void MockRenderThread::RemoveRoute(int32_t routing_id) {}

void MockRenderThread::AddFilter(IPC::MessageFilter* filter) {
  filter->OnFilterAdded(&sink());
  // Add this filter to a vector so the MockRenderThread::RemoveFilter function
  // can check if this filter is added.
  filters_.push_back(base::WrapRefCounted(filter));
}

void MockRenderThread::RemoveFilter(IPC::MessageFilter* filter) {
  // Emulate the IPC::ChannelProxy::OnRemoveFilter function.
  for (size_t i = 0; i < filters_.size(); ++i) {
    if (filters_[i].get() == filter) {
      filter->OnFilterRemoved();
      filters_.erase(filters_.begin() + i);
      return;
    }
  }
  NOTREACHED_IN_MIGRATION() << "filter to be removed not found";
}

#endif

IPC::SyncChannel* MockRenderThread::GetChannel() {}

std::string MockRenderThread::GetLocale() {}

scoped_refptr<base::SingleThreadTaskRunner>
MockRenderThread::GetIOTaskRunner() {}

void MockRenderThread::BindHostReceiver(mojo::GenericPendingReceiver receiver) {}

bool MockRenderThread::GenerateFrameRoutingID(
    int32_t& routing_id,
    blink::LocalFrameToken& frame_token,
    base::UnguessableToken& devtools_frame_token,
    blink::DocumentToken& document_token) {}

void MockRenderThread::AddObserver(RenderThreadObserver* observer) {}

void MockRenderThread::RemoveObserver(RenderThreadObserver* observer) {}

void MockRenderThread::RecordAction(const base::UserMetricsAction& action) {}

void MockRenderThread::RecordComputedAction(const std::string& action) {}

int MockRenderThread::PostTaskToAllWebWorkers(base::RepeatingClosure closure) {}

base::WaitableEvent* MockRenderThread::GetShutdownEvent() {}

int32_t MockRenderThread::GetClientId() {}

void MockRenderThread::SetRendererProcessType(
    blink::scheduler::WebRendererProcessType type) {}

blink::WebString MockRenderThread::GetUserAgent() {}

const blink::UserAgentMetadata& MockRenderThread::GetUserAgentMetadata() {}

#if BUILDFLAG(IS_WIN)
void MockRenderThread::PreCacheFont(const LOGFONT& log_font) {
}

void MockRenderThread::ReleaseCachedFonts() {
}
#endif

void MockRenderThread::SetFieldTrialGroup(const std::string& trial_name,
                                          const std::string& group_name) {}

void MockRenderThread::WriteIntoTrace(
    perfetto::TracedProto<perfetto::protos::pbzero::RenderProcessHost> proto) {}

int32_t MockRenderThread::GetNextRoutingID() {}

mojo::PendingReceiver<blink::mojom::BrowserInterfaceBroker>
MockRenderThread::TakeInitialBrowserInterfaceBrokerReceiverForFrame(
    const blink::LocalFrameToken& frame_token) {}

void MockRenderThread::OnCreateChildFrame(
    const blink::LocalFrameToken& child_frame_token,
    mojo::PendingAssociatedRemote<mojom::Frame> frame_remote,
    mojo::PendingReceiver<blink::mojom::BrowserInterfaceBroker>
        browser_interface_broker) {}

#if BUILDFLAG(CONTENT_ENABLE_LEGACY_IPC)
bool MockRenderThread::OnControlMessageReceived(const IPC::Message& msg) {
  for (auto& observer : observers_) {
    if (observer.OnControlMessageReceived(msg))
      return true;
  }
  return OnMessageReceived(msg);
}

bool MockRenderThread::OnMessageReceived(const IPC::Message& msg) {
  // Save the message in the sink.
  sink_.OnMessageReceived(msg);
  return false;
}
#endif

// The View expects to be returned a valid route_id different from its own.
void MockRenderThread::OnCreateWindow(
    const mojom::CreateNewWindowParams& params,
    mojom::CreateNewWindowReply* reply) {}

void MockRenderThread::ReleaseAllWebViews() {}

}  // namespace content