summaryrefslogtreecommitdiffstats
path: root/content/renderer/pepper/pepper_in_process_router.cc
blob: 62f7dbdefe405facc2e85e3da3b68ec492eb1090 (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
// 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.h"
#include "content/renderer/pepper/renderer_ppapi_host_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"

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) {
  dummy_browser_channel_.reset(
      new Channel(base::Bind(&PepperInProcessRouter::DummySendTo,
                             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() {
  return ppapi::proxy::Connection(dummy_browser_channel_.get(),
                                  plugin_to_host_router_.get());
}

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

  if (!message->is_sync()) {
    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.
    MessageLoop::current()->PostTask(
        FROM_HERE,
        base::Bind(&PepperInProcessRouter::DispatchPluginMsg,
          weak_factory_.GetWeakPtr(),
          base::Owned(message.release())));
  }
  return true;
}

void PepperInProcessRouter::DispatchPluginMsg(IPC::Message* msg) {
  // Emulate the proxy by dispatching the relevant message here.
  bool handled = true;
  IPC_BEGIN_MESSAGE_MAP(PepperInProcessRouter, *msg)
    IPC_MESSAGE_HANDLER(PpapiPluginMsg_ResourceReply, OnResourceReply)
    IPC_MESSAGE_UNHANDLED(handled = false)
  IPC_END_MESSAGE_MAP()
  DCHECK(handled) << "The message wasn't handled by the plugin.";
}

bool PepperInProcessRouter::DummySendTo(IPC::Message *msg) {
  NOTREACHED();
  delete msg;
  return false;
}

void PepperInProcessRouter::OnResourceReply(
    const ppapi::proxy::ResourceMessageReplyParams& reply_params,
    const IPC::Message& nested_msg) {
  ppapi::Resource* resource =
      ppapi::PpapiGlobals::Get()->GetResourceTracker()->GetResource(
          reply_params.pp_resource());
  if (!resource) {
    // The resource could have been destroyed while the async processing was
    // pending. Just drop the message.
    return;
  }
  resource->OnReplyReceived(reply_params, nested_msg);
}

}  // namespace content