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/common/view_messages.h"
11 #include "content/renderer/pepper/pepper_in_process_router.h"
12 #include "content/renderer/render_frame_impl.h"
13 #include "ipc/ipc_message_macros.h"
14 #include "ppapi/proxy/ppapi_messages.h"
15 #include "ppapi/proxy/resource_message_params.h"
16
17 namespace content {
18
PepperBrowserConnection(RenderFrame * render_frame)19 PepperBrowserConnection::PepperBrowserConnection(RenderFrame* render_frame)
20 : RenderFrameObserver(render_frame),
21 RenderFrameObserverTracker<PepperBrowserConnection>(render_frame),
22 next_sequence_number_(1) {}
23
~PepperBrowserConnection()24 PepperBrowserConnection::~PepperBrowserConnection() {}
25
OnMessageReceived(const IPC::Message & msg)26 bool PepperBrowserConnection::OnMessageReceived(const IPC::Message& msg) {
27 // Check if the message is an in-process reply.
28 if (PepperInProcessRouter::OnPluginMsgReceived(msg))
29 return true;
30
31 bool handled = true;
32 IPC_BEGIN_MESSAGE_MAP(PepperBrowserConnection, msg)
33 IPC_MESSAGE_HANDLER(PpapiHostMsg_CreateResourceHostsFromHostReply,
34 OnMsgCreateResourceHostsFromHostReply)
35 IPC_MESSAGE_UNHANDLED(handled = false)
36 IPC_END_MESSAGE_MAP()
37 return handled;
38 }
39
DidCreateInProcessInstance(PP_Instance instance,int render_frame_id,const GURL & document_url,const GURL & plugin_url)40 void PepperBrowserConnection::DidCreateInProcessInstance(
41 PP_Instance instance,
42 int render_frame_id,
43 const GURL& document_url,
44 const GURL& plugin_url) {
45 Send(new ViewHostMsg_DidCreateInProcessInstance(
46 instance,
47 // Browser provides the render process id.
48 PepperRendererInstanceData(
49 0, render_frame_id, document_url, plugin_url)));
50 }
51
DidDeleteInProcessInstance(PP_Instance instance)52 void PepperBrowserConnection::DidDeleteInProcessInstance(PP_Instance instance) {
53 Send(new ViewHostMsg_DidDeleteInProcessInstance(instance));
54 }
55
SendBrowserCreate(int child_process_id,PP_Instance instance,const std::vector<IPC::Message> & nested_msgs,const PendingResourceIDCallback & callback)56 void PepperBrowserConnection::SendBrowserCreate(
57 int child_process_id,
58 PP_Instance instance,
59 const std::vector<IPC::Message>& nested_msgs,
60 const PendingResourceIDCallback& callback) {
61 int32_t sequence_number = GetNextSequence();
62 pending_create_map_[sequence_number] = callback;
63 ppapi::proxy::ResourceMessageCallParams params(0, sequence_number);
64 Send(new PpapiHostMsg_CreateResourceHostsFromHost(
65 routing_id(), child_process_id, params, instance, nested_msgs));
66 }
67
OnMsgCreateResourceHostsFromHostReply(int32_t sequence_number,const std::vector<int> & pending_resource_host_ids)68 void PepperBrowserConnection::OnMsgCreateResourceHostsFromHostReply(
69 int32_t sequence_number,
70 const std::vector<int>& pending_resource_host_ids) {
71 // Check that the message is destined for the plugin this object is associated
72 // with.
73 std::map<int32_t, PendingResourceIDCallback>::iterator it =
74 pending_create_map_.find(sequence_number);
75 if (it != pending_create_map_.end()) {
76 it->second.Run(pending_resource_host_ids);
77 pending_create_map_.erase(it);
78 } else {
79 NOTREACHED();
80 }
81 }
82
GetNextSequence()83 int32_t PepperBrowserConnection::GetNextSequence() {
84 // Return the value with wraparound, making sure we don't make a sequence
85 // number with a 0 ID. Note that signed wraparound is undefined in C++ so we
86 // manually check.
87 int32_t ret = next_sequence_number_;
88 if (next_sequence_number_ == std::numeric_limits<int32_t>::max())
89 next_sequence_number_ = 1; // Skip 0 which is invalid.
90 else
91 next_sequence_number_++;
92 return ret;
93 }
94
95 } // namespace content
96