| 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" |
| (...skipping 354 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 365 } | 365 } |
| 366 | 366 |
| 367 if (!has_permission) { | 367 if (!has_permission) { |
| 368 DispatchOnDisconnect(source, receiver_port_id, kMissingPermissionError); | 368 DispatchOnDisconnect(source, receiver_port_id, kMissingPermissionError); |
| 369 return; | 369 return; |
| 370 } | 370 } |
| 371 | 371 |
| 372 PrefService* pref_service = profile->GetPrefs(); | 372 PrefService* pref_service = profile->GetPrefs(); |
| 373 | 373 |
| 374 // Verify that the host is not blocked by policies. | 374 // Verify that the host is not blocked by policies. |
| 375 NativeMessageProcessHost::PolicyPermission policy_permission = | 375 NativeMessageHost::PolicyPermission policy_permission = |
| 376 NativeMessageProcessHost::IsHostAllowed(pref_service, native_app_name); | 376 NativeMessageHost::IsHostAllowed(pref_service, native_app_name); |
| 377 if (policy_permission == NativeMessageProcessHost::DISALLOW) { | 377 if (policy_permission == NativeMessageHost::DISALLOW) { |
| 378 DispatchOnDisconnect(source, receiver_port_id, kProhibitedByPoliciesError); | 378 DispatchOnDisconnect(source, receiver_port_id, kProhibitedByPoliciesError); |
| 379 return; | 379 return; |
| 380 } | 380 } |
| 381 | 381 |
| 382 scoped_ptr<MessageChannel> channel(new MessageChannel()); | 382 scoped_ptr<MessageChannel> channel(new MessageChannel()); |
| 383 channel->opener.reset(new ExtensionMessagePort(source, MSG_ROUTING_CONTROL, | 383 channel->opener.reset(new ExtensionMessagePort(source, MSG_ROUTING_CONTROL, |
| 384 source_extension_id)); | 384 source_extension_id)); |
| 385 | 385 |
| 386 // Get handle of the native view and pass it to the native messaging host. | 386 // Get handle of the native view and pass it to the native messaging host. |
| 387 gfx::NativeView native_view = | 387 gfx::NativeView native_view = |
| 388 content::RenderWidgetHost::FromID(source_process_id, source_routing_id)-> | 388 content::RenderWidgetHost::FromID(source_process_id, source_routing_id)-> |
| 389 GetView()->GetNativeView(); | 389 GetView()->GetNativeView(); |
| 390 | 390 |
| 391 scoped_ptr<NativeMessageProcessHost> native_process = | 391 scoped_ptr<NativeMessageHost> native_host = NativeMessageHost::Create( |
| 392 NativeMessageProcessHost::Create( | 392 native_view, |
| 393 native_view, | 393 base::WeakPtr<NativeMessageHost::Client>(weak_factory_.GetWeakPtr()), |
| 394 base::WeakPtr<NativeMessageProcessHost::Client>( | 394 source_extension_id, |
| 395 weak_factory_.GetWeakPtr()), | 395 native_app_name, |
| 396 source_extension_id, native_app_name, receiver_port_id, | 396 receiver_port_id, |
| 397 policy_permission == NativeMessageProcessHost::ALLOW_ALL); | 397 policy_permission == NativeMessageHost::ALLOW_ALL); |
| 398 | 398 |
| 399 // Abandon the channel. | 399 // Abandon the channel. |
| 400 if (!native_process.get()) { | 400 if (!native_host.get()) { |
| 401 LOG(ERROR) << "Failed to create native process."; | 401 LOG(ERROR) << "Failed to create native process."; |
| 402 DispatchOnDisconnect( | 402 DispatchOnDisconnect( |
| 403 source, receiver_port_id, kReceivingEndDoesntExistError); | 403 source, receiver_port_id, kReceivingEndDoesntExistError); |
| 404 return; | 404 return; |
| 405 } | 405 } |
| 406 channel->receiver.reset(new NativeMessagePort(native_process.release())); | 406 channel->receiver.reset(new NativeMessagePort(native_host.release())); |
| 407 | 407 |
| 408 // Keep the opener alive until the channel is closed. | 408 // Keep the opener alive until the channel is closed. |
| 409 channel->opener->IncrementLazyKeepaliveCount(); | 409 channel->opener->IncrementLazyKeepaliveCount(); |
| 410 | 410 |
| 411 AddChannel(channel.release(), receiver_port_id); | 411 AddChannel(channel.release(), receiver_port_id); |
| 412 #else // !(defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX)) | 412 #else // !(defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX)) |
| 413 const char kNativeMessagingNotSupportedError[] = | 413 const char kNativeMessagingNotSupportedError[] = |
| 414 "Native Messaging is not supported on this platform."; | 414 "Native Messaging is not supported on this platform."; |
| 415 DispatchOnDisconnect( | 415 DispatchOnDisconnect( |
| 416 source, receiver_port_id, kNativeMessagingNotSupportedError); | 416 source, receiver_port_id, kNativeMessagingNotSupportedError); |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 556 if (iter == channels_.end()) { | 556 if (iter == channels_.end()) { |
| 557 // If this channel is pending, queue up the PostMessage to run once | 557 // If this channel is pending, queue up the PostMessage to run once |
| 558 // the channel opens. | 558 // the channel opens. |
| 559 EnqueuePendingMessage(source_port_id, channel_id, message); | 559 EnqueuePendingMessage(source_port_id, channel_id, message); |
| 560 return; | 560 return; |
| 561 } | 561 } |
| 562 | 562 |
| 563 DispatchMessage(source_port_id, iter->second, message); | 563 DispatchMessage(source_port_id, iter->second, message); |
| 564 } | 564 } |
| 565 | 565 |
| 566 void MessageService::PostMessageFromNativeProcess(int port_id, | 566 void MessageService::PostMessageFromNative(int port_id, |
| 567 const std::string& message) { | 567 const std::string& message) { |
| 568 PostMessage(port_id, Message(message, false /* user_gesture */)); | 568 PostMessage(port_id, Message(message, false /* user_gesture */)); |
| 569 } | 569 } |
| 570 | 570 |
| 571 void MessageService::Observe(int type, | 571 void MessageService::Observe(int type, |
| 572 const content::NotificationSource& source, | 572 const content::NotificationSource& source, |
| 573 const content::NotificationDetails& details) { | 573 const content::NotificationDetails& details) { |
| 574 switch (type) { | 574 switch (type) { |
| 575 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: | 575 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: |
| 576 case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: { | 576 case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: { |
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 756 } | 756 } |
| 757 | 757 |
| 758 void MessageService::DispatchOnDisconnect(content::RenderProcessHost* source, | 758 void MessageService::DispatchOnDisconnect(content::RenderProcessHost* source, |
| 759 int port_id, | 759 int port_id, |
| 760 const std::string& error_message) { | 760 const std::string& error_message) { |
| 761 ExtensionMessagePort port(source, MSG_ROUTING_CONTROL, ""); | 761 ExtensionMessagePort port(source, MSG_ROUTING_CONTROL, ""); |
| 762 port.DispatchOnDisconnect(GET_OPPOSITE_PORT_ID(port_id), error_message); | 762 port.DispatchOnDisconnect(GET_OPPOSITE_PORT_ID(port_id), error_message); |
| 763 } | 763 } |
| 764 | 764 |
| 765 } // namespace extensions | 765 } // namespace extensions |
| OLD | NEW |