blob: 4ff7b2b5a60b352ca09847a7e45adeed4f44ae95 [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// Copyright (c) 2006-2008 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.
initial.commit09911bf2008-07-26 23:55:294
initial.commit09911bf2008-07-26 23:55:295#include "chrome/plugin/plugin_channel.h"
6
[email protected]bf24d2c2009-02-24 23:07:457#include "base/command_line.h"
[email protected]4566f132009-03-12 01:55:138#include "base/process_util.h"
initial.commit09911bf2008-07-26 23:55:299#include "base/string_util.h"
[email protected]51d70e02009-03-27 20:45:5910#include "chrome/common/child_process.h"
11#include "chrome/common/plugin_messages.h"
[email protected]bf24d2c2009-02-24 23:07:4512#include "chrome/common/chrome_switches.h"
[email protected]8930d472009-02-21 08:05:2813#include "chrome/plugin/plugin_thread.h"
initial.commit09911bf2008-07-26 23:55:2914
[email protected]fa6caa42009-06-05 05:35:0715PluginChannel* PluginChannel::GetPluginChannel(
16 int process_id, MessageLoop* ipc_message_loop) {
17 // map renderer's process id to a (single) channel to that process
[email protected]9a3a293b2009-06-04 22:28:1618 std::string channel_name = StringPrintf(
[email protected]fa6caa42009-06-05 05:35:0719 "%d.r%d", base::GetCurrentProcId(), process_id);
initial.commit09911bf2008-07-26 23:55:2920
[email protected]4566f132009-03-12 01:55:1321 return static_cast<PluginChannel*>(PluginChannelBase::GetChannel(
initial.commit09911bf2008-07-26 23:55:2922 channel_name,
23 IPC::Channel::MODE_SERVER,
24 ClassFactory,
25 ipc_message_loop,
[email protected]4566f132009-03-12 01:55:1326 false));
initial.commit09911bf2008-07-26 23:55:2927}
28
[email protected]157e5d22009-04-23 18:43:3529PluginChannel::PluginChannel() : renderer_handle_(0), in_send_(0),
30 off_the_record_(false) {
initial.commit09911bf2008-07-26 23:55:2931 SendUnblockingOnlyDuringDispatch();
[email protected]51d70e02009-03-27 20:45:5932 ChildProcess::current()->AddRefProcess();
[email protected]bf24d2c2009-02-24 23:07:4533 const CommandLine* command_line = CommandLine::ForCurrentProcess();
34 log_messages_ = command_line->HasSwitch(switches::kLogPluginMessages);
initial.commit09911bf2008-07-26 23:55:2935}
36
37PluginChannel::~PluginChannel() {
[email protected]157e5d22009-04-23 18:43:3538 if (renderer_handle_)
39 base::CloseProcessHandle(renderer_handle_);
[email protected]51d70e02009-03-27 20:45:5940 ChildProcess::current()->ReleaseProcess();
initial.commit09911bf2008-07-26 23:55:2941}
42
43bool PluginChannel::Send(IPC::Message* msg) {
44 in_send_++;
[email protected]bf24d2c2009-02-24 23:07:4545 if (log_messages_) {
46 LOG(INFO) << "sending message @" << msg << " on channel @" << this
47 << " with type " << msg->type();
48 }
initial.commit09911bf2008-07-26 23:55:2949 bool result = PluginChannelBase::Send(msg);
50 in_send_--;
51 return result;
52}
53
[email protected]bf24d2c2009-02-24 23:07:4554void PluginChannel::OnMessageReceived(const IPC::Message& msg) {
55 if (log_messages_) {
56 LOG(INFO) << "received message @" << &msg << " on channel @" << this
57 << " with type " << msg.type();
58 }
59 PluginChannelBase::OnMessageReceived(msg);
60}
61
initial.commit09911bf2008-07-26 23:55:2962void PluginChannel::OnControlMessageReceived(const IPC::Message& msg) {
63 IPC_BEGIN_MESSAGE_MAP(PluginChannel, msg)
64 IPC_MESSAGE_HANDLER(PluginMsg_CreateInstance, OnCreateInstance)
[email protected]f09c7182009-03-10 12:54:0465 IPC_MESSAGE_HANDLER_DELAY_REPLY(PluginMsg_DestroyInstance,
66 OnDestroyInstance)
initial.commit09911bf2008-07-26 23:55:2967 IPC_MESSAGE_HANDLER(PluginMsg_GenerateRouteID, OnGenerateRouteID)
68 IPC_MESSAGE_UNHANDLED_ERROR()
69 IPC_END_MESSAGE_MAP()
70}
71
72void PluginChannel::OnCreateInstance(const std::string& mime_type,
73 int* instance_id) {
74 *instance_id = GenerateRouteID();
75 scoped_refptr<WebPluginDelegateStub> stub = new WebPluginDelegateStub(
76 mime_type, *instance_id, this);
77 AddRoute(*instance_id, stub, false);
78 plugin_stubs_.push_back(stub);
79}
80
81void PluginChannel::OnDestroyInstance(int instance_id,
82 IPC::Message* reply_msg) {
83 for (size_t i = 0; i < plugin_stubs_.size(); ++i) {
84 if (plugin_stubs_[i]->instance_id() == instance_id) {
85 plugin_stubs_.erase(plugin_stubs_.begin() + i);
86 RemoveRoute(instance_id);
87 Send(reply_msg);
88 return;
89 }
90 }
91
92 NOTREACHED() << "Couldn't find WebPluginDelegateStub to destroy";
93}
94
95void PluginChannel::OnGenerateRouteID(int* route_id) {
96 *route_id = GenerateRouteID();
97}
98
99int PluginChannel::GenerateRouteID() {
[email protected]157e5d22009-04-23 18:43:35100 static int last_id = 0;
101 return ++last_id;
initial.commit09911bf2008-07-26 23:55:29102}
103
[email protected]4566f132009-03-12 01:55:13104void PluginChannel::OnChannelConnected(int32 peer_pid) {
[email protected]6c6cc802009-04-03 17:01:36105 base::ProcessHandle handle;
106 if (!base::OpenProcessHandle(peer_pid, &handle)) {
107 NOTREACHED();
108 }
[email protected]157e5d22009-04-23 18:43:35109 renderer_handle_ = handle;
[email protected]4566f132009-03-12 01:55:13110 PluginChannelBase::OnChannelConnected(peer_pid);
111}
112
initial.commit09911bf2008-07-26 23:55:29113void PluginChannel::OnChannelError() {
[email protected]157e5d22009-04-23 18:43:35114 base::CloseProcessHandle(renderer_handle_);
115 renderer_handle_ = 0;
initial.commit09911bf2008-07-26 23:55:29116 PluginChannelBase::OnChannelError();
117 CleanUp();
118}
119
120void PluginChannel::CleanUp() {
121 // We need to clean up the stubs so that they call NPPDestroy. This will
122 // also lead to them releasing their reference on this object so that it can
123 // be deleted.
124 for (size_t i = 0; i < plugin_stubs_.size(); ++i)
125 RemoveRoute(plugin_stubs_[i]->instance_id());
126
127 // Need to addref this object temporarily because otherwise removing the last
128 // stub will cause the destructor of this object to be called, however at
129 // that point plugin_stubs_ will have one element and its destructor will be
130 // called twice.
131 scoped_refptr<PluginChannel> me(this);
132
133 plugin_stubs_.clear();
license.botbf09a502008-08-24 00:55:55134}