OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #include "chrome/browser/extensions/extension_message_service.h" | 5 #include "chrome/browser/extensions/extension_message_service.h" |
6 | 6 |
7 #include "base/atomic_sequence_num.h" | 7 #include "base/atomic_sequence_num.h" |
8 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 | 43 |
44 struct ExtensionMessageService::MessageChannel { | 44 struct ExtensionMessageService::MessageChannel { |
45 ExtensionMessageService::MessagePort opener; | 45 ExtensionMessageService::MessagePort opener; |
46 ExtensionMessageService::MessagePort receiver; | 46 ExtensionMessageService::MessagePort receiver; |
47 }; | 47 }; |
48 | 48 |
49 const char ExtensionMessageService::kDispatchOnConnect[] = | 49 const char ExtensionMessageService::kDispatchOnConnect[] = |
50 "Port.dispatchOnConnect"; | 50 "Port.dispatchOnConnect"; |
51 const char ExtensionMessageService::kDispatchOnDisconnect[] = | 51 const char ExtensionMessageService::kDispatchOnDisconnect[] = |
52 "Port.dispatchOnDisconnect"; | 52 "Port.dispatchOnDisconnect"; |
53 const char ExtensionMessageService::kDispatchOnMessage[] = | |
54 "Port.dispatchOnMessage"; | |
55 | 53 |
56 namespace { | 54 namespace { |
57 | 55 |
58 static base::AtomicSequenceNumber g_next_channel_id(base::LINKER_INITIALIZED); | 56 static base::AtomicSequenceNumber g_next_channel_id(base::LINKER_INITIALIZED); |
59 | 57 |
60 static void DispatchOnConnect(const ExtensionMessageService::MessagePort& port, | 58 static void DispatchOnConnect(const ExtensionMessageService::MessagePort& port, |
61 int dest_port_id, | 59 int dest_port_id, |
62 const std::string& channel_name, | 60 const std::string& channel_name, |
63 const std::string& tab_json, | 61 const std::string& tab_json, |
64 const std::string& source_extension_id, | 62 const std::string& source_extension_id, |
(...skipping 16 matching lines...) Expand all Loading... |
81 const ExtensionMessageService::MessagePort& port, int source_port_id, | 79 const ExtensionMessageService::MessagePort& port, int source_port_id, |
82 bool connection_error) { | 80 bool connection_error) { |
83 ListValue args; | 81 ListValue args; |
84 args.Set(0, Value::CreateIntegerValue(source_port_id)); | 82 args.Set(0, Value::CreateIntegerValue(source_port_id)); |
85 args.Set(1, Value::CreateBooleanValue(connection_error)); | 83 args.Set(1, Value::CreateBooleanValue(connection_error)); |
86 port.sender->Send(new ExtensionMsg_MessageInvoke(port.routing_id, | 84 port.sender->Send(new ExtensionMsg_MessageInvoke(port.routing_id, |
87 "", ExtensionMessageService::kDispatchOnDisconnect, args, GURL())); | 85 "", ExtensionMessageService::kDispatchOnDisconnect, args, GURL())); |
88 } | 86 } |
89 | 87 |
90 static void DispatchOnMessage(const ExtensionMessageService::MessagePort& port, | 88 static void DispatchOnMessage(const ExtensionMessageService::MessagePort& port, |
91 const std::string& message, int source_port_id) { | 89 const std::string& message, int target_port_id) { |
92 ListValue args; | 90 port.sender->Send( |
93 args.Set(0, Value::CreateStringValue(message)); | 91 new ExtensionMsg_DeliverMessage( |
94 args.Set(1, Value::CreateIntegerValue(source_port_id)); | 92 port.routing_id, target_port_id, message)); |
95 port.sender->Send(new ExtensionMsg_MessageInvoke(port.routing_id, | |
96 "", ExtensionMessageService::kDispatchOnMessage, args, GURL())); | |
97 } | 93 } |
98 | 94 |
99 } // namespace | 95 } // namespace |
100 | 96 |
101 // static | 97 // static |
102 void ExtensionMessageService::AllocatePortIdPair(int* port1, int* port2) { | 98 void ExtensionMessageService::AllocatePortIdPair(int* port1, int* port2) { |
103 int channel_id = g_next_channel_id.GetNext(); | 99 int channel_id = g_next_channel_id.GetNext(); |
104 int port1_id = channel_id * 2; | 100 int port1_id = channel_id * 2; |
105 int port2_id = channel_id * 2 + 1; | 101 int port2_id = channel_id * 2 + 1; |
106 | 102 |
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
361 | 357 |
362 if (current->second->opener.sender == sender) { | 358 if (current->second->opener.sender == sender) { |
363 CloseChannelImpl(current, GET_CHANNEL_OPENER_ID(current->first), | 359 CloseChannelImpl(current, GET_CHANNEL_OPENER_ID(current->first), |
364 notify_other_port); | 360 notify_other_port); |
365 } else if (current->second->receiver.sender == sender) { | 361 } else if (current->second->receiver.sender == sender) { |
366 CloseChannelImpl(current, GET_CHANNEL_RECEIVERS_ID(current->first), | 362 CloseChannelImpl(current, GET_CHANNEL_RECEIVERS_ID(current->first), |
367 notify_other_port); | 363 notify_other_port); |
368 } | 364 } |
369 } | 365 } |
370 } | 366 } |
OLD | NEW |