summaryrefslogtreecommitdiffstats
path: root/chromeos/binder/writable_transaction_data.cc
blob: 5e5626829e31b00f3662185e86eac6a8116564a9 (plain)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// 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/writable_transaction_data.h"

#include "chromeos/binder/binder_driver_api.h"
#include "chromeos/binder/constants.h"
#include "chromeos/binder/local_object.h"
#include "chromeos/binder/object.h"
#include "chromeos/binder/remote_object.h"

namespace binder {

WritableTransactionData::WritableTransactionData() {}

WritableTransactionData::~WritableTransactionData() {}

uintptr_t WritableTransactionData::GetCookie() const {
  return 0;
}

uint32_t WritableTransactionData::GetCode() const {
  return code_;
}

pid_t WritableTransactionData::GetSenderPID() const {
  return 0;
}

uid_t WritableTransactionData::GetSenderEUID() const {
  return 0;
}

bool WritableTransactionData::IsOneWay() const {
  return is_one_way_;
}

bool WritableTransactionData::HasStatus() const {
  return false;
}

Status WritableTransactionData::GetStatus() const {
  return Status::OK;
}

const void* WritableTransactionData::GetData() const {
  return data_.data();
}

size_t WritableTransactionData::GetDataSize() const {
  return data_.size();
}

const uintptr_t* WritableTransactionData::GetObjectOffsets() const {
  return object_offsets_.data();
}

size_t WritableTransactionData::GetNumObjectOffsets() const {
  return object_offsets_.size();
}

void WritableTransactionData::Reserve(size_t n) {
  data_.reserve(n);
}

void WritableTransactionData::WriteData(const void* data, size_t n) {
  data_.insert(data_.end(), static_cast<const char*>(data),
               static_cast<const char*>(data) + n);
  if (n % 4 != 0) {  // Add padding.
    data_.resize(data_.size() + 4 - (n % 4));
  }
}

void WritableTransactionData::WriteInt32(int32_t value) {
  WriteData(&value, sizeof(value));
}

void WritableTransactionData::WriteUint32(uint32_t value) {
  WriteData(&value, sizeof(value));
}

void WritableTransactionData::WriteInt64(int64_t value) {
  WriteData(&value, sizeof(value));
}

void WritableTransactionData::WriteUint64(uint64_t value) {
  WriteData(&value, sizeof(value));
}

void WritableTransactionData::WriteFloat(float value) {
  WriteData(&value, sizeof(value));
}

void WritableTransactionData::WriteDouble(double value) {
  WriteData(&value, sizeof(value));
}

void WritableTransactionData::WriteCString(const char* value) {
  WriteData(value, strlen(value) + 1);
}

void WritableTransactionData::WriteString(const std::string& value) {
  WriteInt32(value.size());
  if (value.size() > 0) {
    // Write only when the string is not empty.
    // This is different from WriteString16().
    //
    // Despite having the length info, null-terminate the data to be consistent
    // with libbinder.
    WriteData(value.c_str(), value.size() + 1);
  }
}

void WritableTransactionData::WriteString16(const base::string16& value) {
  WriteInt32(value.size());
  // Despite having the length info, null-terminate the data to be consistent
  // with libbinder.
  WriteData(value.c_str(), (value.size() + 1) * sizeof(base::char16));
}

void WritableTransactionData::WriteInterfaceToken(
    const base::string16& interface,
    int32_t strict_mode_policy) {
  WriteInt32(kStrictModePenaltyGather | strict_mode_policy);
  WriteString16(interface);
}

void WritableTransactionData::WriteObject(scoped_refptr<Object> object) {
  objects_.push_back(object);  // Hold reference.

  flat_binder_object flat = {};
  flat.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;

  switch (object->GetType()) {
    case Object::TYPE_LOCAL: {
      auto* local = static_cast<LocalObject*>(object.get());
      flat.type = BINDER_TYPE_BINDER;
      flat.cookie = reinterpret_cast<uintptr_t>(local);
      // flat.binder is unused, but the driver requires it to be a non-zero
      // unique value.
      flat.binder = reinterpret_cast<uintptr_t>(local);
      break;
    }
    case Object::TYPE_REMOTE: {
      auto* remote = static_cast<RemoteObject*>(object.get());
      flat.type = BINDER_TYPE_HANDLE;
      flat.handle = remote->GetHandle();
      break;
    }
  }
  object_offsets_.push_back(data_.size());
  WriteData(&flat, sizeof(flat));
}

void WritableTransactionData::WriteFileDescriptor(base::ScopedFD fd) {
  files_.push_back(std::move(fd));

  flat_binder_object flat = {};
  flat.type = BINDER_TYPE_FD;
  flat.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
  flat.handle = files_.back().get();
  object_offsets_.push_back(data_.size());
  WriteData(&flat, sizeof(flat));
}

}  // namespace binder