Blink roll 174933:174969
[chromium-blink-merge.git] / tools / ipc_fuzzer / replay / replay_process.cc
blobb141cd0029e25310c2e728a01a32cac16e9a078a
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "tools/ipc_fuzzer/replay/replay_process.h"
7 #include <limits.h>
8 #include <string>
9 #include "base/bind.h"
10 #include "base/command_line.h"
11 #include "base/files/file_path.h"
12 #include "base/logging.h"
13 #include "base/posix/global_descriptors.h"
14 #include "chrome/common/chrome_switches.h"
15 #include "ipc/ipc_descriptors.h"
16 #include "ipc/ipc_switches.h"
18 namespace ipc_fuzzer {
20 ReplayProcess::ReplayProcess()
21 : io_thread_("Chrome_ChildIOThread"),
22 shutdown_event_(true, false),
23 message_index_(0) {
26 ReplayProcess::~ReplayProcess() {
27 channel_.reset();
30 bool ReplayProcess::Initialize(int argc, const char** argv) {
31 CommandLine::Init(argc, argv);
33 if (!CommandLine::ForCurrentProcess()->HasSwitch(
34 switches::kIpcFuzzerTestcase)) {
35 LOG(ERROR) << "This binary shouldn't be executed directly, "
36 << "please use tools/ipc_fuzzer/play_testcase.py";
37 return false;
40 // Log to default destination.
41 logging::SetMinLogLevel(logging::LOG_ERROR);
42 logging::InitLogging(logging::LoggingSettings());
44 io_thread_.StartWithOptions(
45 base::Thread::Options(base::MessageLoop::TYPE_IO, 0));
47 base::GlobalDescriptors* g_fds = base::GlobalDescriptors::GetInstance();
48 g_fds->Set(kPrimaryIPCChannel,
49 kPrimaryIPCChannel + base::GlobalDescriptors::kBaseDescriptor);
50 return true;
53 void ReplayProcess::OpenChannel() {
54 std::string channel_name =
55 CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
56 switches::kProcessChannelID);
58 channel_.reset(
59 new IPC::ChannelProxy(channel_name,
60 IPC::Channel::MODE_CLIENT,
61 this,
62 io_thread_.message_loop_proxy()));
65 bool ReplayProcess::OpenTestcase() {
66 base::FilePath path = CommandLine::ForCurrentProcess()->GetSwitchValuePath(
67 switches::kIpcFuzzerTestcase);
68 return MessageFile::Read(path, &messages_);
71 void ReplayProcess::SendNextMessage() {
72 if (message_index_ >= messages_.size()) {
73 base::MessageLoop::current()->Quit();
74 return;
77 // Take next message and release it from vector.
78 IPC::Message* message = messages_[message_index_];
79 messages_[message_index_++] = NULL;
81 if (!channel_->Send(message)) {
82 LOG(ERROR) << "ChannelProxy::Send() failed after "
83 << message_index_ << " messages";
84 base::MessageLoop::current()->Quit();
88 void ReplayProcess::Run() {
89 timer_.reset(new base::Timer(false, true));
90 timer_->Start(FROM_HERE,
91 base::TimeDelta::FromMilliseconds(1),
92 base::Bind(&ReplayProcess::SendNextMessage,
93 base::Unretained(this)));
94 base::MessageLoop::current()->Run();
97 bool ReplayProcess::OnMessageReceived(const IPC::Message& msg) {
98 return true;
101 void ReplayProcess::OnChannelError() {
102 LOG(ERROR) << "Channel error, quitting after "
103 << message_index_ << " messages";
104 base::MessageLoop::current()->Quit();
107 } // namespace ipc_fuzzer