OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef PPAPI_PROXY_PLUGIN_RESOURCE_H_ | 5 #ifndef PPAPI_PROXY_PLUGIN_RESOURCE_H_ |
6 #define PPAPI_PROXY_PLUGIN_RESOURCE_H_ | 6 #define PPAPI_PROXY_PLUGIN_RESOURCE_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
12 #include "ipc/ipc_message.h" | 12 #include "ipc/ipc_message.h" |
13 #include "ipc/ipc_sender.h" | 13 #include "ipc/ipc_sender.h" |
14 #include "ppapi/c/pp_errors.h" | 14 #include "ppapi/c/pp_errors.h" |
15 #include "ppapi/proxy/connection.h" | 15 #include "ppapi/proxy/connection.h" |
16 #include "ppapi/proxy/plugin_resource_callback.h" | 16 #include "ppapi/proxy/plugin_resource_callback.h" |
17 #include "ppapi/proxy/ppapi_message_utils.h" | 17 #include "ppapi/proxy/ppapi_message_utils.h" |
18 #include "ppapi/proxy/ppapi_proxy_export.h" | 18 #include "ppapi/proxy/ppapi_proxy_export.h" |
| 19 #include "ppapi/proxy/resource_message_params.h" |
19 #include "ppapi/shared_impl/resource.h" | 20 #include "ppapi/shared_impl/resource.h" |
20 | 21 |
21 namespace ppapi { | 22 namespace ppapi { |
22 namespace proxy { | 23 namespace proxy { |
23 | 24 |
24 class PluginDispatcher; | 25 class PluginDispatcher; |
25 | 26 |
26 class PPAPI_PROXY_EXPORT PluginResource : public Resource { | 27 class PPAPI_PROXY_EXPORT PluginResource : public Resource { |
27 public: | 28 public: |
28 PluginResource(Connection connection, PP_Instance instance); | 29 PluginResource(Connection connection, PP_Instance instance); |
(...skipping 15 matching lines...) Expand all Loading... |
44 RENDERER = 0, | 45 RENDERER = 0, |
45 BROWSER = 1 | 46 BROWSER = 1 |
46 }; | 47 }; |
47 | 48 |
48 IPC::Sender* GetSender(Destination dest) { | 49 IPC::Sender* GetSender(Destination dest) { |
49 return dest == RENDERER ? connection_.renderer_sender : | 50 return dest == RENDERER ? connection_.renderer_sender : |
50 connection_.browser_sender; | 51 connection_.browser_sender; |
51 } | 52 } |
52 | 53 |
53 // Sends a create message to the browser or renderer for the current resource. | 54 // Sends a create message to the browser or renderer for the current resource. |
54 void SendCreateToBrowser(const IPC::Message& msg); | 55 void SendCreate(Destination dest, const IPC::Message& msg); |
55 void SendCreateToRenderer(const IPC::Message& msg); | |
56 | 56 |
57 // Sends the given IPC message as a resource request to the host | 57 // Sends the given IPC message as a resource request to the host |
58 // corresponding to this resource object and does not expect a reply. | 58 // corresponding to this resource object and does not expect a reply. |
59 void PostToBrowser(const IPC::Message& msg); | 59 void Post(Destination dest, const IPC::Message& msg); |
60 void PostToRenderer(const IPC::Message& msg); | |
61 | 60 |
62 // Like PostToBrowser/Renderer but expects a response. |callback| is | 61 // Like Post() but expects a response. |callback| is a |base::Callback| that |
63 // a |base::Callback| that will be run when a reply message with a sequence | 62 // will be run when a reply message with a sequence number matching that of |
64 // number matching that of the call is received. |ReplyMsgClass| is the type | 63 // the call is received. |ReplyMsgClass| is the type of the reply message that |
65 // of the reply message that is expected. An example of usage: | 64 // is expected. An example of usage: |
66 // | 65 // |
67 // CallBrowser<PpapiPluginMsg_MyResourceType_MyReplyMessage>( | 66 // Call<PpapiPluginMsg_MyResourceType_MyReplyMessage>( |
| 67 // BROWSER, |
68 // PpapiHostMsg_MyResourceType_MyRequestMessage(), | 68 // PpapiHostMsg_MyResourceType_MyRequestMessage(), |
69 // base::Bind(&MyPluginResource::ReplyHandler, this)); | 69 // base::Bind(&MyPluginResource::ReplyHandler, this)); |
70 // | 70 // |
71 // If a reply message to this call is received whose type does not match | 71 // If a reply message to this call is received whose type does not match |
72 // |ReplyMsgClass| (for example, in the case of an error), the callback will | 72 // |ReplyMsgClass| (for example, in the case of an error), the callback will |
73 // still be invoked but with the default values of the message parameters. | 73 // still be invoked but with the default values of the message parameters. |
74 // | 74 // |
75 // Returns the new request's sequence number which can be used to identify | 75 // Returns the new request's sequence number which can be used to identify |
76 // the callback. | 76 // the callback. |
77 // | 77 // |
78 // Note that all integers (including 0 and -1) are valid request IDs. | 78 // Note that all integers (including 0 and -1) are valid request IDs. |
79 template<typename ReplyMsgClass, typename CallbackType> | 79 template<typename ReplyMsgClass, typename CallbackType> |
80 int32_t CallBrowser(const IPC::Message& msg, const CallbackType& callback); | 80 int32_t Call(Destination dest, |
81 template<typename ReplyMsgClass, typename CallbackType> | 81 const IPC::Message& msg, |
82 int32_t CallRenderer(const IPC::Message& msg, const CallbackType& callback); | 82 const CallbackType& callback); |
83 | 83 |
84 // Calls the browser/renderer with sync messages. Returns the pepper error | 84 // Calls the browser/renderer with sync messages. Returns the pepper error |
85 // code from the call. | 85 // code from the call. |
86 // |ReplyMsgClass| is the type of the reply message that is expected. If it | 86 // |ReplyMsgClass| is the type of the reply message that is expected. If it |
87 // carries x parameters, then the method with x out parameters should be used. | 87 // carries x parameters, then the method with x out parameters should be used. |
88 // An example of usage: | 88 // An example of usage: |
89 // | 89 // |
90 // // Assuming the reply message carries a string and an integer. | 90 // // Assuming the reply message carries a string and an integer. |
91 // std::string param_1; | 91 // std::string param_1; |
92 // int param_2 = 0; | 92 // int param_2 = 0; |
93 // int32_t result = SyncCall<PpapiPluginMsg_MyResourceType_MyReplyMessage>( | 93 // int32_t result = SyncCall<PpapiPluginMsg_MyResourceType_MyReplyMessage>( |
94 // RENDERER, PpapiHostMsg_MyResourceType_MyRequestMessage(), | 94 // RENDERER, PpapiHostMsg_MyResourceType_MyRequestMessage(), |
95 // ¶m_1, ¶m_2); | 95 // ¶m_1, ¶m_2); |
96 template <class ReplyMsgClass> | 96 template <class ReplyMsgClass> |
97 int32_t SyncCall(Destination dest, const IPC::Message& msg); | 97 int32_t SyncCall(Destination dest, const IPC::Message& msg); |
98 template <class ReplyMsgClass, class A> | 98 template <class ReplyMsgClass, class A> |
99 int32_t SyncCall(Destination dest, const IPC::Message& msg, A* a); | 99 int32_t SyncCall(Destination dest, const IPC::Message& msg, A* a); |
100 template <class ReplyMsgClass, class A, class B> | 100 template <class ReplyMsgClass, class A, class B> |
101 int32_t SyncCall(Destination dest, const IPC::Message& msg, A* a, B* b); | 101 int32_t SyncCall(Destination dest, const IPC::Message& msg, A* a, B* b); |
102 template <class ReplyMsgClass, class A, class B, class C> | 102 template <class ReplyMsgClass, class A, class B, class C> |
103 int32_t SyncCall(Destination dest, const IPC::Message& msg, A* a, B* b, C* c); | 103 int32_t SyncCall(Destination dest, const IPC::Message& msg, A* a, B* b, C* c); |
104 template <class ReplyMsgClass, class A, class B, class C, class D> | 104 template <class ReplyMsgClass, class A, class B, class C, class D> |
105 int32_t SyncCall( | 105 int32_t SyncCall( |
106 Destination dest, const IPC::Message& msg, A* a, B* b, C* c, D* d); | 106 Destination dest, const IPC::Message& msg, A* a, B* b, C* c, D* d); |
107 template <class ReplyMsgClass, class A, class B, class C, class D, class E> | 107 template <class ReplyMsgClass, class A, class B, class C, class D, class E> |
108 int32_t SyncCall( | 108 int32_t SyncCall( |
109 Destination dest, const IPC::Message& msg, A* a, B* b, C* c, D* d, E* e); | 109 Destination dest, const IPC::Message& msg, A* a, B* b, C* c, D* d, E* e); |
110 | 110 |
111 private: | 111 private: |
112 // Helper function to send a |PpapiHostMsg_ResourceCall| to the given sender | 112 // Helper function to send a |PpapiHostMsg_ResourceCall| to the given |
113 // with |nested_msg| and |call_params|. | 113 // destination with |nested_msg| and |call_params|. |
114 bool SendResourceCall(IPC::Sender* sender, | 114 bool SendResourceCall(Destination dest, |
115 const ResourceMessageCallParams& call_params, | 115 const ResourceMessageCallParams& call_params, |
116 const IPC::Message& nested_msg); | 116 const IPC::Message& nested_msg); |
117 | 117 |
118 // Helper function to make a Resource Call to a host with a callback. | |
119 template<typename ReplyMsgClass, typename CallbackType> | |
120 int32_t CallHost(IPC::Sender* sender, | |
121 const IPC::Message& msg, | |
122 const CallbackType& callback); | |
123 | |
124 int32_t GenericSyncCall(Destination dest, | 118 int32_t GenericSyncCall(Destination dest, |
125 const IPC::Message& msg, | 119 const IPC::Message& msg, |
126 IPC::Message* reply_msg); | 120 IPC::Message* reply_msg); |
127 | 121 |
128 Connection connection_; | 122 Connection connection_; |
129 | 123 |
130 int32_t next_sequence_number_; | 124 int32_t next_sequence_number_; |
131 | 125 |
132 bool sent_create_to_browser_; | 126 bool sent_create_to_browser_; |
133 bool sent_create_to_renderer_; | 127 bool sent_create_to_renderer_; |
134 | 128 |
135 typedef std::map<int32_t, scoped_refptr<PluginResourceCallbackBase> > | 129 typedef std::map<int32_t, scoped_refptr<PluginResourceCallbackBase> > |
136 CallbackMap; | 130 CallbackMap; |
137 CallbackMap callbacks_; | 131 CallbackMap callbacks_; |
138 | 132 |
139 DISALLOW_COPY_AND_ASSIGN(PluginResource); | 133 DISALLOW_COPY_AND_ASSIGN(PluginResource); |
140 }; | 134 }; |
141 | 135 |
142 template<typename ReplyMsgClass, typename CallbackType> | 136 template<typename ReplyMsgClass, typename CallbackType> |
143 int32_t PluginResource::CallBrowser(const IPC::Message& msg, | 137 int32_t PluginResource::Call(Destination dest, |
144 const CallbackType& callback) { | 138 const IPC::Message& msg, |
145 return CallHost<ReplyMsgClass, CallbackType>( | 139 const CallbackType& callback) { |
146 connection_.browser_sender, msg, callback); | 140 ResourceMessageCallParams params(pp_resource(), next_sequence_number_++); |
147 } | |
148 | |
149 template<typename ReplyMsgClass, typename CallbackType> | |
150 int32_t PluginResource::CallRenderer(const IPC::Message& msg, | |
151 const CallbackType& callback) { | |
152 return CallHost<ReplyMsgClass, CallbackType>( | |
153 connection_.renderer_sender, msg, callback); | |
154 } | |
155 | |
156 template<typename ReplyMsgClass, typename CallbackType> | |
157 int32_t PluginResource::CallHost(IPC::Sender* sender, | |
158 const IPC::Message& msg, | |
159 const CallbackType& callback) { | |
160 ResourceMessageCallParams params(pp_resource(), | |
161 next_sequence_number_++); | |
162 // Stash the |callback| in |callbacks_| identified by the sequence number of | 141 // Stash the |callback| in |callbacks_| identified by the sequence number of |
163 // the call. | 142 // the call. |
164 scoped_refptr<PluginResourceCallbackBase> plugin_callback( | 143 scoped_refptr<PluginResourceCallbackBase> plugin_callback( |
165 new PluginResourceCallback<ReplyMsgClass, CallbackType>(callback)); | 144 new PluginResourceCallback<ReplyMsgClass, CallbackType>(callback)); |
166 callbacks_.insert(std::make_pair(params.sequence(), plugin_callback)); | 145 callbacks_.insert(std::make_pair(params.sequence(), plugin_callback)); |
167 params.set_has_callback(); | 146 params.set_has_callback(); |
168 SendResourceCall(sender, params, msg); | 147 SendResourceCall(dest, params, msg); |
169 return params.sequence(); | 148 return params.sequence(); |
170 } | 149 } |
171 | 150 |
172 template <class ReplyMsgClass> | 151 template <class ReplyMsgClass> |
173 int32_t PluginResource::SyncCall(Destination dest, const IPC::Message& msg) { | 152 int32_t PluginResource::SyncCall(Destination dest, const IPC::Message& msg) { |
174 IPC::Message reply; | 153 IPC::Message reply; |
175 return GenericSyncCall(dest, msg, &reply); | 154 return GenericSyncCall(dest, msg, &reply); |
176 } | 155 } |
177 | 156 |
178 template <class ReplyMsgClass, class A> | 157 template <class ReplyMsgClass, class A> |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 | 206 |
228 if (UnpackMessage<ReplyMsgClass>(reply, a, b, c, d, e)) | 207 if (UnpackMessage<ReplyMsgClass>(reply, a, b, c, d, e)) |
229 return result; | 208 return result; |
230 return PP_ERROR_FAILED; | 209 return PP_ERROR_FAILED; |
231 } | 210 } |
232 | 211 |
233 } // namespace proxy | 212 } // namespace proxy |
234 } // namespace ppapi | 213 } // namespace ppapi |
235 | 214 |
236 #endif // PPAPI_PROXY_PLUGIN_RESOURCE_H_ | 215 #endif // PPAPI_PROXY_PLUGIN_RESOURCE_H_ |
OLD | NEW |