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> |
| 9 |
8 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
9 #include "ipc/ipc_sender.h" | 11 #include "ipc/ipc_sender.h" |
10 #include "ppapi/proxy/connection.h" | 12 #include "ppapi/proxy/connection.h" |
| 13 #include "ppapi/proxy/plugin_resource_callback.h" |
11 #include "ppapi/proxy/ppapi_proxy_export.h" | 14 #include "ppapi/proxy/ppapi_proxy_export.h" |
12 #include "ppapi/shared_impl/resource.h" | 15 #include "ppapi/shared_impl/resource.h" |
13 | 16 |
14 namespace IPC { | 17 namespace IPC { |
15 class Message; | 18 class Message; |
16 } | 19 } |
17 | 20 |
18 namespace ppapi { | 21 namespace ppapi { |
19 namespace proxy { | 22 namespace proxy { |
20 | 23 |
21 class PluginDispatcher; | 24 class PluginDispatcher; |
22 | 25 |
23 class PPAPI_PROXY_EXPORT PluginResource : public Resource { | 26 class PPAPI_PROXY_EXPORT PluginResource : public Resource { |
24 public: | 27 public: |
25 PluginResource(Connection connection, PP_Instance instance); | 28 PluginResource(Connection connection, PP_Instance instance); |
26 virtual ~PluginResource(); | 29 virtual ~PluginResource(); |
27 | 30 |
28 // Returns true if we've previously sent a create message to the browser | 31 // Returns true if we've previously sent a create message to the browser |
29 // or renderer. Generally resources will use these to tell if they should | 32 // or renderer. Generally resources will use these to tell if they should |
30 // lazily send create messages. | 33 // lazily send create messages. |
31 bool sent_create_to_browser() const { return sent_create_to_browser_; } | 34 bool sent_create_to_browser() const { return sent_create_to_browser_; } |
32 bool sent_create_to_renderer() const { return sent_create_to_renderer_; } | 35 bool sent_create_to_renderer() const { return sent_create_to_renderer_; } |
33 | 36 |
| 37 // This handles a reply to a resource call. It works by looking up the |
| 38 // callback that was registered when CallBrowser/CallRenderer was called |
| 39 // and calling it with |params| and |msg|. |
| 40 virtual void OnReplyReceived(const proxy::ResourceMessageReplyParams& params, |
| 41 const IPC::Message& msg) OVERRIDE; |
34 protected: | 42 protected: |
35 // Sends a create message to the browser or renderer for the current resource. | 43 // Sends a create message to the browser or renderer for the current resource. |
36 void SendCreateToBrowser(const IPC::Message& msg); | 44 void SendCreateToBrowser(const IPC::Message& msg); |
37 void SendCreateToRenderer(const IPC::Message& msg); | 45 void SendCreateToRenderer(const IPC::Message& msg); |
38 | 46 |
39 // Sends the given IPC message as a resource request to the host | 47 // Sends the given IPC message as a resource request to the host |
40 // corresponding to this resource object and does not expect a reply. | 48 // corresponding to this resource object and does not expect a reply. |
41 void PostToBrowser(const IPC::Message& msg); | 49 void PostToBrowser(const IPC::Message& msg); |
42 void PostToRenderer(const IPC::Message& msg); | 50 void PostToRenderer(const IPC::Message& msg); |
43 | 51 |
44 // Like PostToBrowser/Renderer but expects a response. | 52 // Like PostToBrowser/Renderer but expects a response. |callback| is |
| 53 // a |base::Callback| that will be run when a reply message with a sequence |
| 54 // number matching that of the call is received. |ReplyMsgClass| is the type |
| 55 // of the reply message that is expected. An example of usage: |
| 56 // |
| 57 // CallBrowser<PpapiPluginMsg_MyResourceType_MyReplyMessage>( |
| 58 // PpapiHostMsg_MyResourceType_MyRequestMessage(), |
| 59 // base::Bind(&MyPluginResource::ReplyHandler, this)); |
| 60 // |
| 61 // If a reply message to this call is received whose type does not match |
| 62 // |ReplyMsgClass| (for example, in the case of an error), the callback will |
| 63 // still be invoked but with the default values of the message parameters. |
45 // | 64 // |
46 // Returns the new request's sequence number which can be used to identify | 65 // Returns the new request's sequence number which can be used to identify |
47 // the callback. The host will reply and ppapi::Resource::OnReplyReceived | 66 // the callback. |
48 // will be called. | |
49 // | 67 // |
50 // Note that all integers (including 0 and -1) are valid request IDs. | 68 // Note that all integers (including 0 and -1) are valid request IDs. |
51 int32_t CallBrowser(const IPC::Message& msg); | 69 template<typename ReplyMsgClass, typename CallbackType> |
52 int32_t CallRenderer(const IPC::Message& msg); | 70 int32_t CallBrowser(const IPC::Message& msg, const CallbackType& callback); |
| 71 template<typename ReplyMsgClass, typename CallbackType> |
| 72 int32_t CallRenderer(const IPC::Message& msg, const CallbackType& callback); |
53 | 73 |
54 // Call the browser/renderer with sync messages. The pepper error code from | 74 // Call the browser/renderer with sync messages. The pepper error code from |
55 // the call is returned and the reply message is stored in |reply_msg|. | 75 // the call is returned and the reply message is stored in |reply_msg|. |
56 int32_t CallBrowserSync(const IPC::Message& msg, IPC::Message* reply_msg); | 76 int32_t CallBrowserSync(const IPC::Message& msg, IPC::Message* reply_msg); |
57 int32_t CallRendererSync(const IPC::Message& msg, IPC::Message* reply_msg); | 77 int32_t CallRendererSync(const IPC::Message& msg, IPC::Message* reply_msg); |
58 | 78 |
59 private: | 79 private: |
| 80 // Helper function to send a |PpapiHostMsg_ResourceCall| to the given sender |
| 81 // with |nested_msg| and |call_params|. |
| 82 bool SendResourceCall(IPC::Sender* sender, |
| 83 const ResourceMessageCallParams& call_params, |
| 84 const IPC::Message& nested_msg); |
| 85 |
| 86 // Helper function to make a Resource Call to a host with a callback. |
| 87 template<typename ReplyMsgClass, typename CallbackType> |
| 88 int32_t CallHost(IPC::Sender* sender, |
| 89 const IPC::Message& msg, |
| 90 const CallbackType& callback); |
| 91 |
60 Connection connection_; | 92 Connection connection_; |
61 | 93 |
62 int32_t next_sequence_number_; | 94 int32_t next_sequence_number_; |
63 | 95 |
64 bool sent_create_to_browser_; | 96 bool sent_create_to_browser_; |
65 bool sent_create_to_renderer_; | 97 bool sent_create_to_renderer_; |
66 | 98 |
| 99 typedef std::map<int32_t, scoped_refptr<PluginResourceCallbackBase> > |
| 100 CallbackMap; |
| 101 CallbackMap callbacks_; |
| 102 |
67 DISALLOW_COPY_AND_ASSIGN(PluginResource); | 103 DISALLOW_COPY_AND_ASSIGN(PluginResource); |
68 }; | 104 }; |
69 | 105 |
| 106 template<typename ReplyMsgClass, typename CallbackType> |
| 107 int32_t PluginResource::CallBrowser(const IPC::Message& msg, |
| 108 const CallbackType& callback) { |
| 109 return CallHost<ReplyMsgClass, CallbackType>( |
| 110 connection_.browser_sender, msg, callback); |
| 111 } |
| 112 |
| 113 template<typename ReplyMsgClass, typename CallbackType> |
| 114 int32_t PluginResource::CallRenderer(const IPC::Message& msg, |
| 115 const CallbackType& callback) { |
| 116 return CallHost<ReplyMsgClass, CallbackType>( |
| 117 connection_.renderer_sender, msg, callback); |
| 118 } |
| 119 |
| 120 template<typename ReplyMsgClass, typename CallbackType> |
| 121 int32_t PluginResource::CallHost(IPC::Sender* sender, |
| 122 const IPC::Message& msg, |
| 123 const CallbackType& callback) { |
| 124 ResourceMessageCallParams params(pp_resource(), |
| 125 next_sequence_number_++); |
| 126 // Stash the |callback| in |callbacks_| identified by the sequence number of |
| 127 // the call. |
| 128 scoped_refptr<PluginResourceCallbackBase> plugin_callback( |
| 129 new PluginResourceCallback<ReplyMsgClass, CallbackType>(callback)); |
| 130 callbacks_.insert(std::make_pair(params.sequence(), plugin_callback)); |
| 131 params.set_has_callback(); |
| 132 SendResourceCall(sender, params, msg); |
| 133 return params.sequence(); |
| 134 } |
| 135 |
70 } // namespace proxy | 136 } // namespace proxy |
71 } // namespace ppapi | 137 } // namespace ppapi |
72 | 138 |
73 #endif // PPAPI_PROXY_PLUGIN_RESOURCE_H_ | 139 #endif // PPAPI_PROXY_PLUGIN_RESOURCE_H_ |
OLD | NEW |