1 // Copyright 2018 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.
4
5 #include "mojo/public/cpp/bindings/message_dumper.h"
6
7 #include "base/files/file.h"
8 #include "base/files/file_path.h"
9 #include "base/files/file_util.h"
10 #include "base/logging.h"
11 #include "base/no_destructor.h"
12 #include "base/process/process.h"
13 #include "base/rand_util.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/task_scheduler/post_task.h"
16 #include "mojo/public/cpp/bindings/message.h"
17
18 namespace {
19
DumpDirectory()20 base::FilePath& DumpDirectory() {
21 static base::NoDestructor<base::FilePath> dump_directory;
22 return *dump_directory;
23 }
24
25 // void WriteMessage(uint32_t identifier,
26 // const mojo::MessageDumper::MessageEntry& entry) {
27 // static uint64_t num = 0;
28
29 // if (!entry.interface_name)
30 // return;
31
32 // base::FilePath message_directory =
33 // DumpDirectory()
34 // .AppendASCII(entry.interface_name)
35 // .AppendASCII(base::NumberToString(identifier));
36
37 // if (!base::DirectoryExists(message_directory) &&
38 // !base::CreateDirectory(message_directory)) {
39 // LOG(ERROR) << "Failed to create" << message_directory.value();
40 // return;
41 // }
42
43 // std::string filename =
44 // base::NumberToString(num++) + "." + entry.method_name + ".mojomsg";
45 // base::FilePath path = message_directory.AppendASCII(filename);
46 // base::File file(path,
47 // base::File::FLAG_WRITE | base::File::FLAG_CREATE_ALWAYS);
48
49 // file.WriteAtCurrentPos(reinterpret_cast<const char*>(entry.data_bytes.data()),
50 // static_cast<int>(entry.data_bytes.size()));
51 // }
52
53 } // namespace
54
55 namespace mojo {
56
MessageEntry(const uint8_t * data,uint32_t data_size,const char * interface_name,const char * method_name)57 MessageDumper::MessageEntry::MessageEntry(const uint8_t* data,
58 uint32_t data_size,
59 const char* interface_name,
60 const char* method_name)
61 : interface_name(interface_name),
62 method_name(method_name),
63 data_bytes(data, data + data_size) {}
64
65 MessageDumper::MessageEntry::MessageEntry(const MessageEntry& entry) = default;
66
~MessageEntry()67 MessageDumper::MessageEntry::~MessageEntry() {}
68
MessageDumper()69 MessageDumper::MessageDumper() : identifier_(base::RandUint64()) {}
70
~MessageDumper()71 MessageDumper::~MessageDumper() {}
72
Accept(mojo::Message * message)73 bool MessageDumper::Accept(mojo::Message* message) {
74 // MessageEntry entry(message->data(), message->data_num_bytes(),
75 // "unknown interface", "unknown name");
76
77 // static base::NoDestructor<scoped_refptr<base::TaskRunner>> task_runner(
78 // base::CreateSequencedTaskRunnerWithTraits(
79 // {base::MayBlock(), base::TaskPriority::USER_BLOCKING,
80 // base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN}));
81
82 // (*task_runner)
83 // ->PostTask(FROM_HERE,
84 // base::BindOnce(&WriteMessage, identifier_, std::move(entry)));
85 return true;
86 }
87
SetMessageDumpDirectory(const base::FilePath & directory)88 void MessageDumper::SetMessageDumpDirectory(const base::FilePath& directory) {
89 DumpDirectory() = directory;
90 }
91
GetMessageDumpDirectory()92 const base::FilePath& MessageDumper::GetMessageDumpDirectory() {
93 return DumpDirectory();
94 }
95
96 } // namespace mojo
97