pepper_browser_connection.cc revision 868fa2fe829687343ffae624259930155e16dbd8
1// Copyright 2013 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 "content/renderer/pepper/pepper_browser_connection.h"
6
7#include <limits>
8
9#include "base/logging.h"
10#include "content/renderer/pepper/pepper_plugin_delegate_impl.h"
11#include "content/renderer/render_view_impl.h"
12#include "ipc/ipc_message_macros.h"
13#include "ppapi/proxy/ppapi_messages.h"
14#include "ppapi/proxy/resource_message_params.h"
15
16namespace content {
17
18PepperBrowserConnection::PepperBrowserConnection(
19    PepperPluginDelegateImpl* plugin_delegate)
20    : plugin_delegate_(plugin_delegate),
21      next_sequence_number_(1) {
22}
23
24PepperBrowserConnection::~PepperBrowserConnection() {
25}
26
27bool PepperBrowserConnection::OnMessageReceived(const IPC::Message& msg) {
28  bool handled = true;
29  IPC_BEGIN_MESSAGE_MAP(PepperBrowserConnection, msg)
30    IPC_MESSAGE_HANDLER(PpapiHostMsg_CreateResourceHostFromHostReply,
31                        OnMsgCreateResourceHostFromHostReply)
32    IPC_MESSAGE_UNHANDLED(handled = false)
33  IPC_END_MESSAGE_MAP()
34
35  return handled;
36}
37
38void PepperBrowserConnection::SendBrowserCreate(
39    int child_process_id,
40    PP_Instance instance,
41    const IPC::Message& nested_msg,
42    const PendingResourceIDCallback& callback) {
43  int32_t sequence_number = GetNextSequence();
44  pending_create_map_[sequence_number] = callback;
45  ppapi::proxy::ResourceMessageCallParams params(0, sequence_number);
46  plugin_delegate_->render_view()->Send(
47      new PpapiHostMsg_CreateResourceHostFromHost(
48          child_process_id, params, instance, nested_msg));
49}
50
51void PepperBrowserConnection::OnMsgCreateResourceHostFromHostReply(
52    int32_t sequence_number,
53    int pending_resource_host_id) {
54  // Check that the message is destined for the plugin this object is associated
55  // with.
56  std::map<int32_t, PendingResourceIDCallback>::iterator it =
57      pending_create_map_.find(sequence_number);
58  if (it != pending_create_map_.end()) {
59    it->second.Run(pending_resource_host_id);
60    pending_create_map_.erase(it);
61  } else {
62    NOTREACHED();
63  }
64}
65
66int32_t PepperBrowserConnection::GetNextSequence() {
67  // Return the value with wraparound, making sure we don't make a sequence
68  // number with a 0 ID. Note that signed wraparound is undefined in C++ so we
69  // manually check.
70  int32_t ret = next_sequence_number_;
71  if (next_sequence_number_ == std::numeric_limits<int32_t>::max())
72    next_sequence_number_ = 1;  // Skip 0 which is invalid.
73  else
74    next_sequence_number_++;
75  return ret;
76}
77
78}  // namespace content
79