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 private: | 74 private: |
| 75 // Helper function to send a |PpapiHostMsg_ResourceCall| to the given sender |
| 76 // with |nested_msg| and |call_params|. |
| 77 bool SendResourceCall(IPC::Sender* sender, |
| 78 const ResourceMessageCallParams& call_params, |
| 79 const IPC::Message& nested_msg); |
| 80 |
| 81 // Helper function to make a Resource Call to a host with a callback. |
| 82 template<typename ReplyMsgClass, typename CallbackType> |
| 83 int32_t CallHost(IPC::Sender* sender, |
| 84 const IPC::Message& msg, |
| 85 const CallbackType& callback); |
| 86 |
55 Connection connection_; | 87 Connection connection_; |
56 | 88 |
57 int32_t next_sequence_number_; | 89 int32_t next_sequence_number_; |
58 | 90 |
59 bool sent_create_to_browser_; | 91 bool sent_create_to_browser_; |
60 bool sent_create_to_renderer_; | 92 bool sent_create_to_renderer_; |
61 | 93 |
| 94 typedef std::map<int32_t, scoped_refptr<PluginResourceCallbackBase> > |
| 95 CallbackMap; |
| 96 CallbackMap callbacks_; |
| 97 |
62 DISALLOW_COPY_AND_ASSIGN(PluginResource); | 98 DISALLOW_COPY_AND_ASSIGN(PluginResource); |
63 }; | 99 }; |
64 | 100 |
| 101 template<typename ReplyMsgClass, typename CallbackType> |
| 102 int32_t PluginResource::CallBrowser(const IPC::Message& msg, |
| 103 const CallbackType& callback) { |
| 104 return CallHost<ReplyMsgClass, CallbackType>( |
| 105 connection_.browser_sender, msg, callback); |
| 106 } |
| 107 |
| 108 template<typename ReplyMsgClass, typename CallbackType> |
| 109 int32_t PluginResource::CallRenderer(const IPC::Message& msg, |
| 110 const CallbackType& callback) { |
| 111 return CallHost<ReplyMsgClass, CallbackType>( |
| 112 connection_.renderer_sender, msg, callback); |
| 113 } |
| 114 |
| 115 template<typename ReplyMsgClass, typename CallbackType> |
| 116 int32_t PluginResource::CallHost(IPC::Sender* sender, |
| 117 const IPC::Message& msg, |
| 118 const CallbackType& callback) { |
| 119 ResourceMessageCallParams params(pp_resource(), |
| 120 next_sequence_number_++); |
| 121 // Stash the |callback| in |callbacks_| identified by the sequence number of |
| 122 // the call. |
| 123 scoped_refptr<PluginResourceCallbackBase> plugin_callback( |
| 124 new PluginResourceCallback<ReplyMsgClass, CallbackType>(callback)); |
| 125 callbacks_.insert(std::make_pair(params.sequence(), plugin_callback)); |
| 126 params.set_has_callback(); |
| 127 SendResourceCall(sender, params, msg); |
| 128 return params.sequence(); |
| 129 } |
| 130 |
65 } // namespace proxy | 131 } // namespace proxy |
66 } // namespace ppapi | 132 } // namespace ppapi |
67 | 133 |
68 #endif // PPAPI_PROXY_PLUGIN_RESOURCE_H_ | 134 #endif // PPAPI_PROXY_PLUGIN_RESOURCE_H_ |
OLD | NEW |