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 // Common base class for funnels of Chrome Extension events that originate | 5 // Common base class for funnels of Chrome Extension events that originate |
6 // from the BHO and are sent to the Broker. | 6 // from the BHO and are sent to the Broker. |
7 | 7 |
8 #include "ceee/ie/plugin/bho/events_funnel.h" | 8 #include "ceee/ie/plugin/bho/events_funnel.h" |
9 | 9 |
| 10 #include <atlbase.h> |
10 #include "base/json/json_writer.h" | 11 #include "base/json/json_writer.h" |
11 #include "base/logging.h" | 12 #include "base/logging.h" |
12 #include "base/values.h" | 13 #include "base/values.h" |
| 14 #include "ceee/ie/broker/broker_rpc_client.h" |
13 #include "ceee/ie/common/ceee_module_util.h" | 15 #include "ceee/ie/common/ceee_module_util.h" |
14 | 16 |
15 | 17 |
16 EventsFunnel::EventsFunnel(bool keep_broker_alive) | 18 EventsFunnel::EventsFunnel() { |
17 : keep_broker_alive_(keep_broker_alive) { | |
18 if (keep_broker_alive_) | |
19 ceee_module_util::AddRefModuleWorkerThread(); | |
20 } | 19 } |
21 | 20 |
22 EventsFunnel::~EventsFunnel() { | 21 EventsFunnel::~EventsFunnel() { |
23 if (keep_broker_alive_) | |
24 ceee_module_util::ReleaseModuleWorkerThread(); | |
25 } | 22 } |
26 | 23 |
27 HRESULT EventsFunnel::SendEvent(const char* event_name, | 24 HRESULT EventsFunnel::SendEvent(const char* event_name, |
28 const Value& event_args) { | 25 const Value& event_args) { |
29 // Event arguments for FireEventToBroker always need to be stored in a list. | 26 // Event arguments for FireEventToBroker always need to be stored in a list. |
30 std::string event_args_str; | 27 std::string event_args_str; |
31 if (event_args.IsType(Value::TYPE_LIST)) { | 28 if (event_args.IsType(Value::TYPE_LIST)) { |
32 base::JSONWriter::Write(&event_args, false, &event_args_str); | 29 base::JSONWriter::Write(&event_args, false, &event_args_str); |
33 } else { | 30 } else { |
34 ListValue list; | 31 ListValue list; |
35 list.Append(event_args.DeepCopy()); | 32 list.Append(event_args.DeepCopy()); |
36 base::JSONWriter::Write(&list, false, &event_args_str); | 33 base::JSONWriter::Write(&list, false, &event_args_str); |
37 } | 34 } |
38 | 35 |
39 EventsFunnel thread_locker(!keep_broker_alive_); | 36 return SendEventToBroker(event_name, event_args_str.c_str()); |
40 ceee_module_util::FireEventToBroker(event_name, event_args_str); | |
41 return S_OK; | |
42 } | 37 } |
| 38 |
| 39 HRESULT EventsFunnel::SendEventToBroker(const char* event_name, |
| 40 const char* event_args) { |
| 41 if (!broker_rpc_client_.get()) { |
| 42 broker_rpc_client_.reset(new BrokerRpcClient()); |
| 43 if (!broker_rpc_client_.get()) |
| 44 return E_OUTOFMEMORY; |
| 45 HRESULT hr = BrokerRpcClient::StartServer(); |
| 46 if (FAILED(hr)) |
| 47 return hr; |
| 48 hr = broker_rpc_client_->Connect(); |
| 49 // Don't reset broker_rpc_client_. See comment in *h file. |
| 50 if (FAILED(hr)) |
| 51 return hr; |
| 52 } |
| 53 return broker_rpc_client_->FireEvent(event_name, event_args); |
| 54 } |
OLD | NEW |