| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 // | 4 // |
| 5 // Broker RPC Client implementation. | 5 // Broker RPC Client implementation. |
| 6 | 6 |
| 7 #include "ceee/ie/broker/broker_rpc_client.h" | 7 #include "ceee/ie/broker/broker_rpc_client.h" |
| 8 | 8 |
| 9 #include <atlbase.h> | 9 #include <atlbase.h> |
| 10 #include "base/lock.h" | 10 #include "base/lock.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/tuple.h" |
| 12 #include "base/win/scoped_comptr.h" | 13 #include "base/win/scoped_comptr.h" |
| 13 #include "broker_lib.h" // NOLINT | 14 #include "broker_lib.h" // NOLINT |
| 14 #include "broker_rpc_lib.h" // NOLINT | 15 #include "broker_rpc_lib.h" // NOLINT |
| 15 #include "ceee/common/com_utils.h" | 16 #include "ceee/common/com_utils.h" |
| 16 #include "ceee/ie/broker/broker_rpc_utils.h" | 17 #include "ceee/ie/broker/broker_rpc_utils.h" |
| 17 | 18 |
| 18 BrokerRpcClient::BrokerRpcClient() : context_(0), binding_handle_(NULL) { | 19 |
| 20 namespace { |
| 21 |
| 22 void LogRpcException(const char* str, unsigned int exception_code) { |
| 23 LOG(ERROR) << str << com::LogWe(exception_code); |
| 19 } | 24 } |
| 20 | 25 |
| 21 BrokerRpcClient::~BrokerRpcClient() { | 26 HRESULT BindRpc(std::wstring endpoint, RPC_BINDING_HANDLE* binding_handle) { |
| 22 Disconnect(); | 27 DCHECK(binding_handle != NULL); |
| 28 std::wstring protocol = kRpcProtocol; |
| 29 DCHECK(!protocol.empty()); |
| 30 DCHECK(!endpoint.empty()); |
| 31 if (protocol.empty() || endpoint.empty() || binding_handle == NULL) |
| 32 return E_INVALIDARG; |
| 33 |
| 34 RPC_BINDING_HANDLE tmp_binding_handle = NULL; |
| 35 |
| 36 // TODO(vitalybuka@google.com): There's no guarantee (aside from name |
| 37 // uniqueness) that it will connect to an endpoint created by the same user. |
| 38 // Hint: The missing invocation is RpcBindingSetAuthInfoEx. |
| 39 VLOG(1) << "Connecting to RPC server. Endpoint: " << endpoint; |
| 40 RPC_WSTR string_binding = NULL; |
| 41 // Create binding string for given end point. |
| 42 RPC_STATUS status = ::RpcStringBindingCompose( |
| 43 NULL, |
| 44 reinterpret_cast<RPC_WSTR>(&protocol[0]), |
| 45 NULL, |
| 46 reinterpret_cast<RPC_WSTR>(&endpoint[0]), |
| 47 NULL, |
| 48 &string_binding); |
| 49 |
| 50 if (RPC_S_OK == status) { |
| 51 // Create binding from just generated binding string. Binding handle should |
| 52 // be used for PRC calls. |
| 53 status = ::RpcBindingFromStringBinding(string_binding, &tmp_binding_handle); |
| 54 ::RpcStringFree(&string_binding); |
| 55 if (RPC_S_OK == status) { |
| 56 VLOG(1) << "RPC client is connected. Endpoint: " << endpoint; |
| 57 *binding_handle = tmp_binding_handle; |
| 58 return S_OK; |
| 59 } else { |
| 60 LogRpcException("Failed to bind. RPC_STATUS:", status); |
| 61 } |
| 62 } else { |
| 63 LogRpcException("Failed to compose binding string. RPC_STATUS:", status); |
| 64 } |
| 65 return RPC_E_FAULT; |
| 23 } | 66 } |
| 24 | 67 |
| 25 int HandleRpcException(unsigned int rpc_exception_code) { | 68 int HandleRpcException(unsigned int rpc_exception_code) { |
| 26 switch (rpc_exception_code) { | 69 switch (rpc_exception_code) { |
| 27 case STATUS_ACCESS_VIOLATION: | 70 case STATUS_ACCESS_VIOLATION: |
| 28 case STATUS_DATATYPE_MISALIGNMENT: | 71 case STATUS_DATATYPE_MISALIGNMENT: |
| 29 case STATUS_PRIVILEGED_INSTRUCTION: | 72 case STATUS_PRIVILEGED_INSTRUCTION: |
| 30 case STATUS_BREAKPOINT: | 73 case STATUS_BREAKPOINT: |
| 31 case STATUS_STACK_OVERFLOW: | 74 case STATUS_STACK_OVERFLOW: |
| 32 case STATUS_IN_PAGE_ERROR: | 75 case STATUS_IN_PAGE_ERROR: |
| 33 case STATUS_GUARD_PAGE_VIOLATION: | 76 case STATUS_GUARD_PAGE_VIOLATION: |
| 34 return EXCEPTION_CONTINUE_SEARCH; | 77 return EXCEPTION_CONTINUE_SEARCH; |
| 35 default: | 78 default: |
| 36 break; | 79 break; |
| 37 } | 80 } |
| 38 return EXCEPTION_EXECUTE_HANDLER; | 81 return EXCEPTION_EXECUTE_HANDLER; |
| 39 } | 82 } |
| 40 | 83 |
| 41 static void LogRpcException(const char* str, unsigned int exception_code) { | 84 } // namespace |
| 42 LOG(ERROR) << str << com::LogWe(exception_code); | 85 |
| 86 BrokerRpcClient::BrokerRpcClient(bool allow_restarts) |
| 87 : context_(0), |
| 88 binding_handle_(NULL), |
| 89 allow_restarts_(allow_restarts) { |
| 90 } |
| 91 |
| 92 BrokerRpcClient::~BrokerRpcClient() { |
| 93 Disconnect(); |
| 43 } | 94 } |
| 44 | 95 |
| 45 void BrokerRpcClient::LockContext() { | 96 void BrokerRpcClient::LockContext() { |
| 46 RpcTryExcept { | 97 RpcTryExcept { |
| 47 context_ = BrokerRpcClient_Connect(binding_handle_); | 98 context_ = BrokerRpcClient_Connect(binding_handle_); |
| 48 } RpcExcept(HandleRpcException(RpcExceptionCode())) { | 99 } RpcExcept(HandleRpcException(RpcExceptionCode())) { |
| 49 LogRpcException("RPC error in LockContext", RpcExceptionCode()); | 100 LogRpcException("RPC error in LockContext", RpcExceptionCode()); |
| 50 } RpcEndExcept | 101 } RpcEndExcept |
| 51 } | 102 } |
| 52 | 103 |
| 53 void BrokerRpcClient::ReleaseContext() { | 104 void BrokerRpcClient::ReleaseContext() { |
| 54 RpcTryExcept { | 105 RpcTryExcept { |
| 55 BrokerRpcClient_Disconnect(binding_handle_, &context_); | 106 BrokerRpcClient_Disconnect(binding_handle_, &context_); |
| 56 } RpcExcept(HandleRpcException(RpcExceptionCode())) { | 107 } RpcExcept(HandleRpcException(RpcExceptionCode())) { |
| 57 LogRpcException("RPC error in ReleaseContext", RpcExceptionCode()); | 108 LogRpcException("RPC error in ReleaseContext", RpcExceptionCode()); |
| 58 } RpcEndExcept | 109 } RpcEndExcept |
| 59 } | 110 } |
| 60 | 111 |
| 112 HRESULT BrokerRpcClient::StartServer(IUnknown** server) { |
| 113 base::win::ScopedComPtr<IUnknown> broker; |
| 114 // TODO(vitalybuka@google.com): Start broker without COM after the last |
| 115 // COM interface is removed. |
| 116 HRESULT hr = broker.CreateInstance(CLSID_CeeeBroker); |
| 117 LOG_IF(ERROR, FAILED(hr)) << "Failed to create broker. " << com::LogHr(hr); |
| 118 if (FAILED(hr)) |
| 119 return hr; |
| 120 *server = broker.Detach(); |
| 121 return S_OK; |
| 122 } |
| 123 |
| 61 HRESULT BrokerRpcClient::Connect(bool start_server) { | 124 HRESULT BrokerRpcClient::Connect(bool start_server) { |
| 62 if (is_connected()) | 125 if (is_connected()) |
| 63 return S_OK; | 126 return S_OK; |
| 64 | 127 |
| 65 // Keep alive until RPC is connected. | 128 // Keep alive until RPC is connected. |
| 66 base::win::ScopedComPtr<ICeeeBrokerRegistrar> broker; | 129 base::win::ScopedComPtr<IUnknown> broker; |
| 67 if (start_server) { | 130 if (start_server) { |
| 68 // TODO(vitalybuka@google.com): Start broker without COM after the last | 131 HRESULT hr = StartServer(broker.Receive()); |
| 69 // COM interface is removed. | |
| 70 HRESULT hr = broker.CreateInstance(CLSID_CeeeBroker); | |
| 71 LOG_IF(ERROR, FAILED(hr)) << "Failed to create broker. " << com::LogHr(hr); | |
| 72 if (FAILED(hr)) | 132 if (FAILED(hr)) |
| 73 return hr; | 133 return hr; |
| 74 } | 134 } |
| 75 | 135 |
| 76 std::wstring end_point = GetRpcEndPointAddress(); | 136 if (SUCCEEDED(BindRpc(GetRpcEndpointAddress(), &binding_handle_))) |
| 77 std::wstring protocol = kRpcProtocol; | 137 LockContext(); |
| 78 DCHECK(!protocol.empty()); | |
| 79 DCHECK(!end_point.empty()); | |
| 80 if (protocol.empty() || end_point.empty()) | |
| 81 return RPC_E_FAULT; | |
| 82 | 138 |
| 83 // TODO(vitalybuka@google.com): There's no guarantee (aside from name | |
| 84 // uniqueness) that it will connect to an endpoint created by the same user. | |
| 85 // Hint: The missing invocation is RpcBindingSetAuthInfoEx. | |
| 86 LOG(INFO) << "Connecting to RPC server. Endpoint: " << end_point; | |
| 87 RPC_WSTR string_binding = NULL; | |
| 88 // Create binding string with given protocol and end point. | |
| 89 RPC_STATUS status = ::RpcStringBindingCompose( | |
| 90 NULL, | |
| 91 reinterpret_cast<RPC_WSTR>(&protocol[0]), | |
| 92 NULL, | |
| 93 reinterpret_cast<RPC_WSTR>(&end_point[0]), | |
| 94 NULL, | |
| 95 &string_binding); | |
| 96 LOG_IF(ERROR, RPC_S_OK != status) << | |
| 97 "Failed to compose binding string. RPC_STATUS=0x" << com::LogWe(status); | |
| 98 | |
| 99 if (RPC_S_OK == status) { | |
| 100 // Create binding from just generated binding string. Binding handle should | |
| 101 // used for PRC calls. | |
| 102 status = ::RpcBindingFromStringBinding(string_binding, &binding_handle_); | |
| 103 LOG_IF(ERROR, RPC_S_OK != status) << | |
| 104 "Failed to bind. RPC_STATUS=0x" << com::LogWe(status); | |
| 105 ::RpcStringFree(&string_binding); | |
| 106 if (RPC_S_OK == status) { | |
| 107 LOG(INFO) << "RPC client is connected. Endpoint: " << end_point; | |
| 108 LockContext(); | |
| 109 } | |
| 110 } | |
| 111 if (!is_connected()) { | 139 if (!is_connected()) { |
| 112 Disconnect(); | 140 Disconnect(); |
| 113 return RPC_E_FAULT; | 141 return RPC_E_FAULT; |
| 114 } | 142 } |
| 115 return S_OK; | 143 return S_OK; |
| 116 } | 144 } |
| 117 | 145 |
| 118 void BrokerRpcClient::Disconnect() { | 146 void BrokerRpcClient::Disconnect() { |
| 119 if (context_ != NULL) | 147 if (context_ != NULL) |
| 120 ReleaseContext(); | 148 ReleaseContext(); |
| 121 if (binding_handle_ != NULL) { | 149 if (binding_handle_ != NULL) { |
| 122 RPC_STATUS status = ::RpcBindingFree(&binding_handle_); | 150 RPC_STATUS status = ::RpcBindingFree(&binding_handle_); |
| 123 LOG_IF(WARNING, RPC_S_OK != status) << | 151 LOG_IF(WARNING, RPC_S_OK != status) << |
| 124 "Failed to unbind. RPC_STATUS=0x" << com::LogWe(status); | 152 "Failed to unbind. RPC_STATUS=0x" << com::LogWe(status); |
| 125 } | 153 } |
| 126 } | 154 } |
| 127 | 155 |
| 156 template<class Function, class Params> |
| 157 HRESULT BrokerRpcClient::RunRpc(bool allow_restart, |
| 158 Function rpc_function, |
| 159 const Params& params) { |
| 160 DCHECK(rpc_function); |
| 161 if (!is_connected()) |
| 162 return RPC_E_FAULT; |
| 163 RpcTryExcept { |
| 164 DispatchToFunction(rpc_function, params); |
| 165 return S_OK; |
| 166 } RpcExcept(HandleRpcException(RpcExceptionCode())) { |
| 167 LogRpcException("RPC error in RunRpc", RpcExceptionCode()); |
| 168 |
| 169 if (allow_restart && |
| 170 RPC_S_OK != ::RpcMgmtIsServerListening(binding_handle_)) { |
| 171 Disconnect(); |
| 172 if (SUCCEEDED(Connect(true))) { |
| 173 return RunRpc(false, rpc_function, params); |
| 174 } |
| 175 } |
| 176 return RPC_E_FAULT; |
| 177 } RpcEndExcept |
| 178 } |
| 179 |
| 128 HRESULT BrokerRpcClient::FireEvent(const char* event_name, | 180 HRESULT BrokerRpcClient::FireEvent(const char* event_name, |
| 129 const char* event_args) { | 181 const char* event_args) { |
| 130 RpcTryExcept { | 182 return RunRpc(allow_restarts_, |
| 131 BrokerRpcClient_FireEvent(binding_handle_, context_, event_name, | 183 &BrokerRpcClient_FireEvent, |
| 132 event_args); | 184 MakeRefTuple(binding_handle_, context_, event_name, |
| 133 return S_OK; | 185 event_args)); |
| 134 } RpcExcept(HandleRpcException(RpcExceptionCode())) { | |
| 135 LogRpcException("RPC error in FireEvent", RpcExceptionCode()); | |
| 136 return RPC_E_FAULT; | |
| 137 } RpcEndExcept | |
| 138 } | 186 } |
| 139 | 187 |
| 140 HRESULT BrokerRpcClient::SendUmaHistogramTimes(const char* name, int sample) { | 188 HRESULT BrokerRpcClient::SendUmaHistogramTimes(const char* name, int sample) { |
| 141 RpcTryExcept { | 189 return RunRpc(allow_restarts_, |
| 142 BrokerRpcClient_SendUmaHistogramTimes(binding_handle_, name, sample); | 190 &BrokerRpcClient_SendUmaHistogramTimes, |
| 143 return S_OK; | 191 MakeRefTuple(binding_handle_, name, sample)); |
| 144 } RpcExcept(HandleRpcException(RpcExceptionCode())) { | |
| 145 LogRpcException("RPC error in SendUmaHistogramTimes", RpcExceptionCode()); | |
| 146 return RPC_E_FAULT; | |
| 147 } RpcEndExcept | |
| 148 } | 192 } |
| 149 | 193 |
| 150 HRESULT BrokerRpcClient::SendUmaHistogramData(const char* name, | 194 HRESULT BrokerRpcClient::SendUmaHistogramData(const char* name, |
| 151 int sample, | 195 int sample, |
| 152 int min, | 196 int min, |
| 153 int max, | 197 int max, |
| 154 int bucket_count) { | 198 int bucket_count) { |
| 155 RpcTryExcept { | 199 return RunRpc(allow_restarts_, |
| 156 BrokerRpcClient_SendUmaHistogramData( | 200 &BrokerRpcClient_SendUmaHistogramData, |
| 157 binding_handle_, name, sample, min, max, bucket_count); | 201 MakeRefTuple(binding_handle_, name, sample, min, max, |
| 158 return S_OK; | 202 bucket_count)); |
| 159 } RpcExcept(HandleRpcException(RpcExceptionCode())) { | |
| 160 LogRpcException("RPC error in SendUmaHistogramData", RpcExceptionCode()); | |
| 161 return RPC_E_FAULT; | |
| 162 } RpcEndExcept | |
| 163 } | 203 } |
| OLD | NEW |