forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_service.cc
137 lines (122 loc) · 4.56 KB
/
test_service.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright 2015 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 "chromeos/binder/test_service.h"
#include "base/bind.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/guid.h"
#include "base/memory/ptr_util.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "base/synchronization/waitable_event.h"
#include "chromeos/binder/local_object.h"
#include "chromeos/binder/service_manager_proxy.h"
#include "chromeos/binder/transaction_data.h"
#include "chromeos/binder/transaction_data_reader.h"
#include "chromeos/binder/writable_transaction_data.h"
namespace binder {
class TestService::TestObject : public LocalObject::TransactionHandler {
public:
TestObject()
: event_(false /* manual_reset */, false /* initially_signaled */) {
VLOG(1) << "Object created: " << this;
}
~TestObject() override { VLOG(1) << "Object destroyed: " << this; }
std::unique_ptr<binder::TransactionData> OnTransact(
binder::CommandBroker* command_broker,
const binder::TransactionData& data) {
VLOG(1) << "Transact code = " << data.GetCode();
binder::TransactionDataReader reader(data);
switch (data.GetCode()) {
case INCREMENT_INT_TRANSACTION: {
int32_t arg = 0;
reader.ReadInt32(&arg);
std::unique_ptr<binder::WritableTransactionData> reply(
new binder::WritableTransactionData());
reply->WriteInt32(arg + 1);
return std::move(reply);
}
case GET_FD_TRANSACTION: {
// Prepare a file.
std::string data = GetFileContents();
base::ScopedTempDir temp_dir;
base::FilePath path;
if (!temp_dir.CreateUniqueTempDir() ||
!base::CreateTemporaryFileInDir(temp_dir.path(), &path) ||
!base::WriteFile(path, data.data(), data.size())) {
LOG(ERROR) << "Failed to create a file";
return std::unique_ptr<TransactionData>();
}
// Open the file.
base::File file(path, base::File::FLAG_OPEN | base::File::FLAG_READ);
if (!file.IsValid()) {
LOG(ERROR) << "Failed to open the file.";
return std::unique_ptr<TransactionData>();
}
// Return the FD.
// The file will be deleted by |temp_dir|, but the FD remains valid
// until the receiving process closes it.
std::unique_ptr<binder::WritableTransactionData> reply(
new binder::WritableTransactionData());
reply->WriteFileDescriptor(base::ScopedFD(file.TakePlatformFile()));
return std::move(reply);
}
case WAIT_TRANSACTION: {
event_.Wait();
std::unique_ptr<binder::WritableTransactionData> reply(
new binder::WritableTransactionData());
reply->WriteUint32(WAIT_TRANSACTION);
return std::move(reply);
}
case SIGNAL_TRANSACTION: {
event_.Signal();
std::unique_ptr<binder::WritableTransactionData> reply(
new binder::WritableTransactionData());
reply->WriteUint32(SIGNAL_TRANSACTION);
return std::move(reply);
}
}
return std::unique_ptr<TransactionData>();
}
private:
base::WaitableEvent event_;
DISALLOW_COPY_AND_ASSIGN(TestObject);
};
TestService::TestService()
: service_name_(base::ASCIIToUTF16("org.chromium.TestService-" +
base::GenerateGUID())),
sub_thread_(&main_thread_) {}
TestService::~TestService() {}
bool TestService::StartAndWait() {
if (!main_thread_.Start() || !sub_thread_.Start() ||
!main_thread_.WaitUntilThreadStarted() || !main_thread_.initialized() ||
!sub_thread_.WaitUntilThreadStarted() || !sub_thread_.initialized()) {
LOG(ERROR) << "Failed to start the threads.";
return false;
}
bool result = false;
base::RunLoop run_loop;
main_thread_.task_runner()->PostTaskAndReply(
FROM_HERE,
base::Bind(&TestService::Initialize, base::Unretained(this), &result),
run_loop.QuitClosure());
run_loop.Run();
return result;
}
void TestService::Stop() {
sub_thread_.Stop();
main_thread_.Stop();
}
// static
std::string TestService::GetFileContents() {
return "Test data";
}
void TestService::Initialize(bool* result) {
// Add service.
scoped_refptr<LocalObject> object(
new LocalObject(base::WrapUnique(new TestObject)));
*result = ServiceManagerProxy::AddService(main_thread_.command_broker(),
service_name_, object, 0);
}
} // namespace binder