| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/renderer/plugin_channel_host.h" | |
| 6 | |
| 7 #include "content/common/plugin_messages.h" | |
| 8 #include "content/plugin/npobject_base.h" | |
| 9 | |
| 10 #if defined(OS_POSIX) | |
| 11 #include "ipc/ipc_channel_posix.h" | |
| 12 #endif | |
| 13 | |
| 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebBindings.h" | |
| 15 | |
| 16 // A simple MessageFilter that will ignore all messages and respond to sync | |
| 17 // messages with an error when is_listening_ is false. | |
| 18 class IsListeningFilter : public IPC::ChannelProxy::MessageFilter { | |
| 19 public: | |
| 20 IsListeningFilter() : channel_(NULL) {} | |
| 21 | |
| 22 // MessageFilter overrides | |
| 23 virtual void OnFilterRemoved() {} | |
| 24 virtual void OnFilterAdded(IPC::Channel* channel) { channel_ = channel; } | |
| 25 virtual bool OnMessageReceived(const IPC::Message& message); | |
| 26 | |
| 27 static bool is_listening_; | |
| 28 | |
| 29 private: | |
| 30 IPC::Channel* channel_; | |
| 31 | |
| 32 DISALLOW_COPY_AND_ASSIGN(IsListeningFilter); | |
| 33 }; | |
| 34 | |
| 35 bool IsListeningFilter::OnMessageReceived(const IPC::Message& message) { | |
| 36 if (IsListeningFilter::is_listening_) { | |
| 37 // Proceed with normal operation. | |
| 38 return false; | |
| 39 } | |
| 40 | |
| 41 // Always process message reply to prevent renderer from hanging on sync | |
| 42 // messages. | |
| 43 if (message.is_reply() || message.is_reply_error()) { | |
| 44 return false; | |
| 45 } | |
| 46 | |
| 47 // Reply to synchronous messages with an error (so they don't block while | |
| 48 // we're not listening). | |
| 49 if (message.is_sync()) { | |
| 50 IPC::Message* reply = IPC::SyncMessage::GenerateReply(&message); | |
| 51 reply->set_reply_error(); | |
| 52 channel_->Send(reply); | |
| 53 } | |
| 54 return true; | |
| 55 } | |
| 56 | |
| 57 // static | |
| 58 bool IsListeningFilter::is_listening_ = true; | |
| 59 | |
| 60 // static | |
| 61 bool PluginChannelHost::IsListening() { | |
| 62 return IsListeningFilter::is_listening_; | |
| 63 } | |
| 64 | |
| 65 // static | |
| 66 void PluginChannelHost::SetListening(bool flag) { | |
| 67 IsListeningFilter::is_listening_ = flag; | |
| 68 } | |
| 69 | |
| 70 PluginChannelHost* PluginChannelHost::GetPluginChannelHost( | |
| 71 const IPC::ChannelHandle& channel_handle, MessageLoop* ipc_message_loop) { | |
| 72 PluginChannelHost* result = | |
| 73 static_cast<PluginChannelHost*>(PluginChannelBase::GetChannel( | |
| 74 channel_handle, | |
| 75 IPC::Channel::MODE_CLIENT, | |
| 76 ClassFactory, | |
| 77 ipc_message_loop, | |
| 78 true)); | |
| 79 return result; | |
| 80 } | |
| 81 | |
| 82 PluginChannelHost::PluginChannelHost() : expecting_shutdown_(false) { | |
| 83 } | |
| 84 | |
| 85 PluginChannelHost::~PluginChannelHost() { | |
| 86 } | |
| 87 | |
| 88 bool PluginChannelHost::Init(MessageLoop* ipc_message_loop, | |
| 89 bool create_pipe_now) { | |
| 90 bool ret = PluginChannelBase::Init(ipc_message_loop, create_pipe_now); | |
| 91 is_listening_filter_ = new IsListeningFilter; | |
| 92 channel_->AddFilter(is_listening_filter_); | |
| 93 return ret; | |
| 94 } | |
| 95 | |
| 96 int PluginChannelHost::GenerateRouteID() { | |
| 97 int route_id = MSG_ROUTING_NONE; | |
| 98 Send(new PluginMsg_GenerateRouteID(&route_id)); | |
| 99 | |
| 100 return route_id; | |
| 101 } | |
| 102 | |
| 103 void PluginChannelHost::AddRoute(int route_id, | |
| 104 IPC::Channel::Listener* listener, | |
| 105 NPObjectBase* npobject) { | |
| 106 PluginChannelBase::AddRoute(route_id, listener, npobject); | |
| 107 | |
| 108 if (!npobject) | |
| 109 proxies_[route_id] = listener; | |
| 110 } | |
| 111 | |
| 112 void PluginChannelHost::RemoveRoute(int route_id) { | |
| 113 proxies_.erase(route_id); | |
| 114 PluginChannelBase::RemoveRoute(route_id); | |
| 115 } | |
| 116 | |
| 117 bool PluginChannelHost::OnControlMessageReceived(const IPC::Message& message) { | |
| 118 bool handled = true; | |
| 119 IPC_BEGIN_MESSAGE_MAP(PluginChannelHost, message) | |
| 120 IPC_MESSAGE_HANDLER(PluginHostMsg_SetException, OnSetException) | |
| 121 IPC_MESSAGE_HANDLER(PluginHostMsg_PluginShuttingDown, OnPluginShuttingDown) | |
| 122 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 123 IPC_END_MESSAGE_MAP() | |
| 124 DCHECK(handled); | |
| 125 return handled; | |
| 126 } | |
| 127 | |
| 128 void PluginChannelHost::OnSetException(const std::string& message) { | |
| 129 WebKit::WebBindings::setException(NULL, message.c_str()); | |
| 130 } | |
| 131 | |
| 132 void PluginChannelHost::OnPluginShuttingDown(const IPC::Message& message) { | |
| 133 expecting_shutdown_ = true; | |
| 134 } | |
| 135 | |
| 136 void PluginChannelHost::OnChannelError() { | |
| 137 PluginChannelBase::OnChannelError(); | |
| 138 | |
| 139 for (ProxyMap::iterator iter = proxies_.begin(); | |
| 140 iter != proxies_.end(); iter++) { | |
| 141 iter->second->OnChannelError(); | |
| 142 } | |
| 143 | |
| 144 proxies_.clear(); | |
| 145 } | |
| OLD | NEW |