summaryrefslogtreecommitdiffstats
path: root/chromium/content/renderer/pepper/pepper_in_process_router.cc
blob: 45755e1d4d84a155b8a11d65fba34d0fae20b875 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/renderer/pepper/pepper_in_process_router.h"

#include "base/bind.h"
#include "base/message_loop/message_loop.h"
#include "content/public/renderer/render_thread.h"
#include "content/renderer/pepper/renderer_ppapi_host_impl.h"
#include "content/renderer/render_frame_impl.h"
#include "ipc/ipc_message.h"
#include "ipc/ipc_sender.h"
#include "ppapi/proxy/ppapi_messages.h"
#include "ppapi/shared_impl/ppapi_globals.h"
#include "ppapi/shared_impl/resource_tracker.h"

using ppapi::UnpackMessage;

namespace content {

class PepperInProcessRouter::Channel : public IPC::Sender {
 public:
  Channel(const base::Callback<bool(IPC::Message*)>& callback)
      : callback_(callback) {}

  virtual ~Channel() {}

  virtual bool Send(IPC::Message* message) OVERRIDE {
    return callback_.Run(message);
  }

 private:
  base::Callback<bool(IPC::Message*)> callback_;
};

PepperInProcessRouter::PepperInProcessRouter(
    RendererPpapiHostImpl* host_impl)
    : host_impl_(host_impl),
      pending_message_id_(0),
      reply_result_(false),
      weak_factory_(this) {
  browser_channel_.reset(
      new Channel(base::Bind(&PepperInProcessRouter::SendToBrowser,
                             base::Unretained(this))));
  host_to_plugin_router_.reset(
      new Channel(base::Bind(&PepperInProcessRouter::SendToPlugin,
                             base::Unretained(this))));
  plugin_to_host_router_.reset(
      new Channel(base::Bind(&PepperInProcessRouter::SendToHost,
                             base::Unretained(this))));
}

PepperInProcessRouter::~PepperInProcessRouter() {
}

IPC::Sender* PepperInProcessRouter::GetPluginToRendererSender() {
  return plugin_to_host_router_.get();
}

IPC::Sender* PepperInProcessRouter::GetRendererToPluginSender() {
  return host_to_plugin_router_.get();
}

ppapi::proxy::Connection PepperInProcessRouter::GetPluginConnection(
    PP_Instance instance) {
  int routing_id = 0;
  RenderFrame* frame = host_impl_->GetRenderFrameForInstance(instance);
  if (frame)
    routing_id = frame->GetRoutingID();
  return ppapi::proxy::Connection(browser_channel_.get(),
                                  plugin_to_host_router_.get(),
                                  routing_id);
}

// static
bool PepperInProcessRouter::OnPluginMsgReceived(const IPC::Message& msg) {
  // Emulate the proxy by dispatching the relevant message here.
  ppapi::proxy::ResourceMessageReplyParams reply_params;
  IPC::Message nested_msg;

  if (msg.type() == PpapiPluginMsg_ResourceReply::ID) {
    // Resource reply from the renderer (no routing id).
    if (!UnpackMessage<PpapiPluginMsg_ResourceReply>(msg, &reply_params,
                                                     &nested_msg)) {
      NOTREACHED();
      return false;
    }
  } else if (msg.type() == PpapiHostMsg_InProcessResourceReply::ID) {
    // Resource reply from the browser (has a routing id).
    if (!UnpackMessage<PpapiHostMsg_InProcessResourceReply>(msg, &reply_params,
                                                            &nested_msg)) {
      NOTREACHED();
      return false;
    }
  } else {
    return false;
  }
  ppapi::Resource* resource =
      ppapi::PpapiGlobals::Get()->GetResourceTracker()->GetResource(
          reply_params.pp_resource());
  // If the resource doesn't exist, it may have been destroyed so just ignore
  // the message.
  if (resource)
    resource->OnReplyReceived(reply_params, nested_msg);
  return true;
}

bool PepperInProcessRouter::SendToHost(IPC::Message* msg) {
  scoped_ptr<IPC::Message> message(msg);

  if (!message->is_sync()) {
    // If this is a resource destroyed message, post a task to dispatch it.
    // Dispatching it synchronously can cause the host to re-enter the proxy
    // code while we're still in the resource destructor, leading to a crash.
    // http://crbug.com/276368.
    // This won't cause message reordering problems because the resource
    // destroyed message is always the last one sent for a resource.
    if (message->type() == PpapiHostMsg_ResourceDestroyed::ID) {
      base::MessageLoop::current()->PostTask(
          FROM_HERE,
          base::Bind(&PepperInProcessRouter::DispatchHostMsg,
                     weak_factory_.GetWeakPtr(),
                     base::Owned(message.release())));
      return true;
    } else {
      bool result = host_impl_->GetPpapiHost()->OnMessageReceived(*message);
      DCHECK(result) << "The message was not handled by the host.";
      return true;
    }
  }

  pending_message_id_ = IPC::SyncMessage::GetMessageId(*message);
  reply_deserializer_.reset(
      static_cast<IPC::SyncMessage*>(message.get())->GetReplyDeserializer());
  reply_result_ = false;

  bool result = host_impl_->GetPpapiHost()->OnMessageReceived(*message);
  DCHECK(result) << "The message was not handled by the host.";

  pending_message_id_ = 0;
  reply_deserializer_.reset(NULL);
  return reply_result_;
}

bool PepperInProcessRouter::SendToPlugin(IPC::Message* msg) {
  scoped_ptr<IPC::Message> message(msg);
  CHECK(!msg->is_sync());
  if (IPC::SyncMessage::IsMessageReplyTo(*message, pending_message_id_)) {
    if (!msg->is_reply_error())
      reply_result_ = reply_deserializer_->SerializeOutputParameters(*message);
  } else {
    CHECK(!pending_message_id_);
    // Dispatch plugin messages from the message loop.
    base::MessageLoop::current()->PostTask(
        FROM_HERE,
        base::Bind(&PepperInProcessRouter::DispatchPluginMsg,
                   weak_factory_.GetWeakPtr(),
                   base::Owned(message.release())));
  }
  return true;
}

void PepperInProcessRouter::DispatchHostMsg(IPC::Message* msg) {
  bool handled = host_impl_->GetPpapiHost()->OnMessageReceived(*msg);
  DCHECK(handled);
}

void PepperInProcessRouter::DispatchPluginMsg(IPC::Message* msg) {
  bool handled = OnPluginMsgReceived(*msg);
  DCHECK(handled);
}

bool PepperInProcessRouter::SendToBrowser(IPC::Message *msg) {
  return RenderThread::Get()->Send(msg);
}

}  // namespace content