| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/api/messaging/message_service.h" | 5 #include "chrome/browser/extensions/api/messaging/message_service.h" |
| 6 | 6 |
| 7 #include "base/atomic_sequence_num.h" | 7 #include "base/atomic_sequence_num.h" |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
| 11 #include "base/lazy_instance.h" |
| 11 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
| 12 #include "base/values.h" | 13 #include "base/values.h" |
| 13 #include "chrome/browser/extensions/api/messaging/extension_message_port.h" | 14 #include "chrome/browser/extensions/api/messaging/extension_message_port.h" |
| 14 #include "chrome/browser/extensions/api/messaging/native_message_port.h" | 15 #include "chrome/browser/extensions/api/messaging/native_message_port.h" |
| 15 #include "chrome/browser/extensions/extension_host.h" | 16 #include "chrome/browser/extensions/extension_host.h" |
| 16 #include "chrome/browser/extensions/extension_process_manager.h" | 17 #include "chrome/browser/extensions/extension_process_manager.h" |
| 17 #include "chrome/browser/extensions/extension_service.h" | 18 #include "chrome/browser/extensions/extension_service.h" |
| 18 #include "chrome/browser/extensions/extension_system.h" | 19 #include "chrome/browser/extensions/extension_system.h" |
| 19 #include "chrome/browser/extensions/extension_tab_util.h" | 20 #include "chrome/browser/extensions/extension_tab_util.h" |
| 20 #include "chrome/browser/extensions/lazy_background_task_queue.h" | 21 #include "chrome/browser/extensions/lazy_background_task_queue.h" |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 DCHECK(GET_OPPOSITE_PORT_ID(port2_id) == port1_id); | 136 DCHECK(GET_OPPOSITE_PORT_ID(port2_id) == port1_id); |
| 136 DCHECK(GET_CHANNEL_ID(port1_id) == GET_CHANNEL_ID(port2_id)); | 137 DCHECK(GET_CHANNEL_ID(port1_id) == GET_CHANNEL_ID(port2_id)); |
| 137 DCHECK(GET_CHANNEL_ID(port1_id) == channel_id); | 138 DCHECK(GET_CHANNEL_ID(port1_id) == channel_id); |
| 138 DCHECK(GET_CHANNEL_OPENER_ID(channel_id) == port1_id); | 139 DCHECK(GET_CHANNEL_OPENER_ID(channel_id) == port1_id); |
| 139 DCHECK(GET_CHANNEL_RECEIVERS_ID(channel_id) == port2_id); | 140 DCHECK(GET_CHANNEL_RECEIVERS_ID(channel_id) == port2_id); |
| 140 | 141 |
| 141 *port1 = port1_id; | 142 *port1 = port1_id; |
| 142 *port2 = port2_id; | 143 *port2 = port2_id; |
| 143 } | 144 } |
| 144 | 145 |
| 145 MessageService::MessageService( | 146 MessageService::MessageService(Profile* profile) |
| 146 LazyBackgroundTaskQueue* queue) | 147 : lazy_background_task_queue_( |
| 147 : lazy_background_task_queue_(queue), | 148 ExtensionSystem::Get(profile)->lazy_background_task_queue()), |
| 148 weak_factory_(this) { | 149 weak_factory_(this) { |
| 149 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, | 150 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
| 150 content::NotificationService::AllBrowserContextsAndSources()); | 151 content::NotificationService::AllBrowserContextsAndSources()); |
| 151 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, | 152 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, |
| 152 content::NotificationService::AllBrowserContextsAndSources()); | 153 content::NotificationService::AllBrowserContextsAndSources()); |
| 153 } | 154 } |
| 154 | 155 |
| 155 MessageService::~MessageService() { | 156 MessageService::~MessageService() { |
| 156 STLDeleteContainerPairSecondPointers(channels_.begin(), channels_.end()); | 157 STLDeleteContainerPairSecondPointers(channels_.begin(), channels_.end()); |
| 157 channels_.clear(); | 158 channels_.clear(); |
| 158 } | 159 } |
| 159 | 160 |
| 161 static base::LazyInstance<ProfileKeyedAPIFactory<MessageService> > |
| 162 g_factory = LAZY_INSTANCE_INITIALIZER; |
| 163 |
| 164 // static |
| 165 ProfileKeyedAPIFactory<MessageService>* MessageService::GetFactoryInstance() { |
| 166 return &g_factory.Get(); |
| 167 } |
| 168 |
| 169 // static |
| 170 MessageService* MessageService::Get(Profile* profile) { |
| 171 return ProfileKeyedAPIFactory<MessageService>::GetForProfile(profile); |
| 172 } |
| 173 |
| 160 void MessageService::OpenChannelToExtension( | 174 void MessageService::OpenChannelToExtension( |
| 161 int source_process_id, int source_routing_id, int receiver_port_id, | 175 int source_process_id, int source_routing_id, int receiver_port_id, |
| 162 const std::string& source_extension_id, | 176 const std::string& source_extension_id, |
| 163 const std::string& target_extension_id, | 177 const std::string& target_extension_id, |
| 164 const GURL& source_url, | 178 const GURL& source_url, |
| 165 const std::string& channel_name) { | 179 const std::string& channel_name) { |
| 166 content::RenderProcessHost* source = | 180 content::RenderProcessHost* source = |
| 167 content::RenderProcessHost::FromID(source_process_id); | 181 content::RenderProcessHost::FromID(source_process_id); |
| 168 if (!source) | 182 if (!source) |
| 169 return; | 183 return; |
| (...skipping 356 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 526 return; | 540 return; |
| 527 | 541 |
| 528 params->source = source; | 542 params->source = source; |
| 529 params->receiver.reset(new ExtensionMessagePort(host->render_process_host(), | 543 params->receiver.reset(new ExtensionMessagePort(host->render_process_host(), |
| 530 MSG_ROUTING_CONTROL, | 544 MSG_ROUTING_CONTROL, |
| 531 params->target_extension_id)); | 545 params->target_extension_id)); |
| 532 OpenChannelImpl(params.Pass()); | 546 OpenChannelImpl(params.Pass()); |
| 533 } | 547 } |
| 534 | 548 |
| 535 } // namespace extensions | 549 } // namespace extensions |
| OLD | NEW |