[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | f7885d4 | 2012-04-03 08:30:04 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 5 | #ifndef EXTENSIONS_RENDERER_REQUEST_SENDER_H_ |
| 6 | #define EXTENSIONS_RENDERER_REQUEST_SENDER_H_ |
[email protected] | f7885d4 | 2012-04-03 08:30:04 | [diff] [blame] | 7 | |
[email protected] | f7885d4 | 2012-04-03 08:30:04 | [diff] [blame] | 8 | #include <map> |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 9 | #include <string> |
[email protected] | f7885d4 | 2012-04-03 08:30:04 | [diff] [blame] | 10 | |
avi | 2d124c0 | 2015-12-23 06:36:42 | [diff] [blame] | 11 | #include "base/macros.h" |
[email protected] | f7885d4 | 2012-04-03 08:30:04 | [diff] [blame] | 12 | #include "v8/include/v8.h" |
| 13 | |
[email protected] | f7885d4 | 2012-04-03 08:30:04 | [diff] [blame] | 14 | namespace base { |
| 15 | class ListValue; |
| 16 | } |
| 17 | |
[email protected] | 8fe74bf | 2012-08-07 21:08:42 | [diff] [blame] | 18 | namespace extensions { |
Devlin Cronin | 4607e32 | 2017-07-17 16:17:18 | [diff] [blame] | 19 | class IPCMessageSender; |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 20 | class ScriptContext; |
[email protected] | 8fe74bf | 2012-08-07 21:08:42 | [diff] [blame] | 21 | |
[email protected] | f7885d4 | 2012-04-03 08:30:04 | [diff] [blame] | 22 | struct PendingRequest; |
| 23 | |
| 24 | // Responsible for sending requests for named extension API functions to the |
| 25 | // extension host and routing the responses back to the caller. |
[email protected] | 8fe74bf | 2012-08-07 21:08:42 | [diff] [blame] | 26 | class RequestSender { |
[email protected] | f7885d4 | 2012-04-03 08:30:04 | [diff] [blame] | 27 | public: |
[email protected] | ec47b08 | 2013-03-29 14:46:17 | [diff] [blame] | 28 | // Source represents a user of RequestSender. Every request is associated with |
| 29 | // a Source object, which will be notified when the corresponding response |
| 30 | // arrives. When a Source object is going away and there are pending requests, |
| 31 | // it should call InvalidateSource() to make sure no notifications are sent to |
| 32 | // it later. |
| 33 | class Source { |
| 34 | public: |
| 35 | virtual ~Source() {} |
| 36 | |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 37 | virtual ScriptContext* GetContext() = 0; |
[email protected] | ec47b08 | 2013-03-29 14:46:17 | [diff] [blame] | 38 | virtual void OnResponseReceived(const std::string& name, |
| 39 | int request_id, |
| 40 | bool success, |
| 41 | const base::ListValue& response, |
| 42 | const std::string& error) = 0; |
| 43 | }; |
| 44 | |
Devlin Cronin | 4607e32 | 2017-07-17 16:17:18 | [diff] [blame] | 45 | explicit RequestSender(IPCMessageSender* ipc_message_sender); |
| 46 | ~RequestSender(); |
[email protected] | f7885d4 | 2012-04-03 08:30:04 | [diff] [blame] | 47 | |
[email protected] | ec47b08 | 2013-03-29 14:46:17 | [diff] [blame] | 48 | // In order to avoid collision, all |request_id|s passed into StartRequest() |
| 49 | // should be generated by this method. |
| 50 | int GetNextRequestId() const; |
| 51 | |
[email protected] | f7885d4 | 2012-04-03 08:30:04 | [diff] [blame] | 52 | // Makes a call to the API function |name| that is to be handled by the |
[email protected] | 74e21e7 | 2012-07-09 21:20:53 | [diff] [blame] | 53 | // extension host. The response to this request will be received in |
| 54 | // HandleResponse(). |
| 55 | // TODO(koz): Remove |request_id| and generate that internally. |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 56 | // There are multiple of these per render view though, so we'll |
| 57 | // need to vend the IDs centrally. |
rdevlin.cronin | b2b3139 | 2016-06-22 23:39:13 | [diff] [blame] | 58 | // Returns true if the request is successfully sent. |
| 59 | bool StartRequest(Source* source, |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 60 | const std::string& name, |
[email protected] | 74e21e7 | 2012-07-09 21:20:53 | [diff] [blame] | 61 | int request_id, |
| 62 | bool has_callback, |
| 63 | bool for_io_thread, |
| 64 | base::ListValue* value_args); |
| 65 | |
[email protected] | 74e21e7 | 2012-07-09 21:20:53 | [diff] [blame] | 66 | // Handles responses from the extension host to calls made by StartRequest(). |
| 67 | void HandleResponse(int request_id, |
| 68 | bool success, |
| 69 | const base::ListValue& response, |
| 70 | const std::string& error); |
| 71 | |
[email protected] | ec47b08 | 2013-03-29 14:46:17 | [diff] [blame] | 72 | // Notifies this that a request source is no longer valid. |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 73 | // TODO(kalman): Do this in a generic/safe way. |
[email protected] | ec47b08 | 2013-03-29 14:46:17 | [diff] [blame] | 74 | void InvalidateSource(Source* source); |
[email protected] | f7885d4 | 2012-04-03 08:30:04 | [diff] [blame] | 75 | |
| 76 | private: |
[email protected] | eb7ef5f | 2014-02-06 09:59:19 | [diff] [blame] | 77 | friend class ScopedTabID; |
lazyboy | 4cbdfe0a | 2016-09-02 05:32:32 | [diff] [blame] | 78 | using PendingRequestMap = std::map<int, std::unique_ptr<PendingRequest>>; |
[email protected] | f7885d4 | 2012-04-03 08:30:04 | [diff] [blame] | 79 | |
lazyboy | 4cbdfe0a | 2016-09-02 05:32:32 | [diff] [blame] | 80 | void InsertRequest(int request_id, |
| 81 | std::unique_ptr<PendingRequest> pending_request); |
| 82 | std::unique_ptr<PendingRequest> RemoveRequest(int request_id); |
[email protected] | f7885d4 | 2012-04-03 08:30:04 | [diff] [blame] | 83 | |
[email protected] | 21776be | 2012-07-09 03:53:32 | [diff] [blame] | 84 | PendingRequestMap pending_requests_; |
| 85 | |
Devlin Cronin | 4607e32 | 2017-07-17 16:17:18 | [diff] [blame] | 86 | // Guaranteed to outlive this object. |
| 87 | IPCMessageSender* const ipc_message_sender_; |
| 88 | |
[email protected] | 8fe74bf | 2012-08-07 21:08:42 | [diff] [blame] | 89 | DISALLOW_COPY_AND_ASSIGN(RequestSender); |
[email protected] | f7885d4 | 2012-04-03 08:30:04 | [diff] [blame] | 90 | }; |
| 91 | |
[email protected] | 8fe74bf | 2012-08-07 21:08:42 | [diff] [blame] | 92 | } // namespace extensions |
| 93 | |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 94 | #endif // EXTENSIONS_RENDERER_REQUEST_SENDER_H_ |