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