OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 "chromeos/binder/test_service.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/guid.h" |
| 9 #include "base/run_loop.h" |
| 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "chromeos/binder/local_object.h" |
| 12 #include "chromeos/binder/service_manager_proxy.h" |
| 13 #include "chromeos/binder/transaction_data.h" |
| 14 #include "chromeos/binder/transaction_data_reader.h" |
| 15 #include "chromeos/binder/writable_transaction_data.h" |
| 16 |
| 17 namespace binder { |
| 18 |
| 19 class TestService::TestObject : public LocalObject::TransactionHandler { |
| 20 public: |
| 21 TestObject() { VLOG(1) << "Object created: " << this; } |
| 22 |
| 23 ~TestObject() override { VLOG(1) << "Object destroyed: " << this; } |
| 24 |
| 25 scoped_ptr<binder::TransactionData> OnTransact( |
| 26 binder::CommandBroker* command_broker, |
| 27 const binder::TransactionData& data) { |
| 28 VLOG(1) << "Transact code = " << data.GetCode(); |
| 29 binder::TransactionDataReader reader(data); |
| 30 switch (data.GetCode()) { |
| 31 case INCREMENT_INT_TRANSACTION: { |
| 32 int32_t arg = 0; |
| 33 reader.ReadInt32(&arg); |
| 34 scoped_ptr<binder::WritableTransactionData> reply( |
| 35 new binder::WritableTransactionData()); |
| 36 reply->WriteInt32(arg + 1); |
| 37 return std::move(reply); |
| 38 } |
| 39 } |
| 40 return scoped_ptr<TransactionData>(); |
| 41 } |
| 42 |
| 43 private: |
| 44 DISALLOW_COPY_AND_ASSIGN(TestObject); |
| 45 }; |
| 46 |
| 47 TestService::TestService() |
| 48 : service_name_(base::ASCIIToUTF16("org.chromium.TestService-" + |
| 49 base::GenerateGUID())) {} |
| 50 |
| 51 TestService::~TestService() {} |
| 52 |
| 53 bool TestService::StartAndWait() { |
| 54 if (!thread_.Start() || !thread_.WaitUntilThreadStarted() || |
| 55 !thread_.initialized()) { |
| 56 LOG(ERROR) << "Failed to start the thread."; |
| 57 return false; |
| 58 } |
| 59 bool result = false; |
| 60 base::RunLoop run_loop; |
| 61 thread_.task_runner()->PostTaskAndReply( |
| 62 FROM_HERE, |
| 63 base::Bind(&TestService::Initialize, base::Unretained(this), &result), |
| 64 run_loop.QuitClosure()); |
| 65 run_loop.Run(); |
| 66 return result; |
| 67 } |
| 68 |
| 69 void TestService::Stop() { |
| 70 thread_.Stop(); |
| 71 } |
| 72 |
| 73 void TestService::Initialize(bool* result) { |
| 74 // Add service. |
| 75 scoped_refptr<LocalObject> object( |
| 76 new LocalObject(make_scoped_ptr(new TestObject))); |
| 77 *result = ServiceManagerProxy::AddService(thread_.command_broker(), |
| 78 service_name_, object, 0); |
| 79 } |
| 80 |
| 81 } // namespace binder |
OLD | NEW |