[email protected] | f7885d4 | 2012-04-03 08:30:04 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 8fe74bf | 2012-08-07 21:08:42 | [diff] [blame] | 5 | #ifndef CHROME_RENDERER_EXTENSIONS_REQUEST_SENDER_H_ |
| 6 | #define CHROME_RENDERER_EXTENSIONS_REQUEST_SENDER_H_ |
[email protected] | f7885d4 | 2012-04-03 08:30:04 | [diff] [blame] | 7 | |
| 8 | #include <string> |
| 9 | #include <map> |
| 10 | |
| 11 | #include "base/memory/linked_ptr.h" |
| 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 { |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 19 | class ChromeV8Context; |
[email protected] | 8fe74bf | 2012-08-07 21:08:42 | [diff] [blame] | 20 | class Dispatcher; |
| 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 | |
| 37 | virtual ChromeV8Context* GetContext() = 0; |
| 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 | |
[email protected] | eb7ef5f | 2014-02-06 09:59:19 | [diff] [blame^] | 45 | // Helper class to (re)set the |source_tab_id_| below. |
| 46 | class ScopedTabID { |
| 47 | public: |
| 48 | ScopedTabID(RequestSender* request_sender, int tab_id); |
| 49 | ~ScopedTabID(); |
| 50 | |
| 51 | private: |
| 52 | RequestSender* const request_sender_; |
| 53 | const int tab_id_; |
| 54 | const int previous_tab_id_; |
| 55 | |
| 56 | DISALLOW_COPY_AND_ASSIGN(ScopedTabID); |
| 57 | }; |
| 58 | |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 59 | explicit RequestSender(Dispatcher* dispatcher); |
[email protected] | 8fe74bf | 2012-08-07 21:08:42 | [diff] [blame] | 60 | ~RequestSender(); |
[email protected] | f7885d4 | 2012-04-03 08:30:04 | [diff] [blame] | 61 | |
[email protected] | ec47b08 | 2013-03-29 14:46:17 | [diff] [blame] | 62 | // In order to avoid collision, all |request_id|s passed into StartRequest() |
| 63 | // should be generated by this method. |
| 64 | int GetNextRequestId() const; |
| 65 | |
[email protected] | f7885d4 | 2012-04-03 08:30:04 | [diff] [blame] | 66 | // 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] | 67 | // extension host. The response to this request will be received in |
| 68 | // HandleResponse(). |
| 69 | // TODO(koz): Remove |request_id| and generate that internally. |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 70 | // There are multiple of these per render view though, so we'll |
| 71 | // need to vend the IDs centrally. |
[email protected] | ec47b08 | 2013-03-29 14:46:17 | [diff] [blame] | 72 | void StartRequest(Source* source, |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 73 | const std::string& name, |
[email protected] | 74e21e7 | 2012-07-09 21:20:53 | [diff] [blame] | 74 | int request_id, |
| 75 | bool has_callback, |
| 76 | bool for_io_thread, |
| 77 | base::ListValue* value_args); |
| 78 | |
| 79 | // Handles responses from the extension host to calls made by StartRequest(). |
| 80 | void HandleResponse(int request_id, |
| 81 | bool success, |
| 82 | const base::ListValue& response, |
| 83 | const std::string& error); |
| 84 | |
[email protected] | ec47b08 | 2013-03-29 14:46:17 | [diff] [blame] | 85 | // Notifies this that a request source is no longer valid. |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 86 | // TODO(kalman): Do this in a generic/safe way. |
[email protected] | ec47b08 | 2013-03-29 14:46:17 | [diff] [blame] | 87 | void InvalidateSource(Source* source); |
[email protected] | f7885d4 | 2012-04-03 08:30:04 | [diff] [blame] | 88 | |
| 89 | private: |
[email protected] | eb7ef5f | 2014-02-06 09:59:19 | [diff] [blame^] | 90 | friend class ScopedTabID; |
[email protected] | 74e21e7 | 2012-07-09 21:20:53 | [diff] [blame] | 91 | typedef std::map<int, linked_ptr<PendingRequest> > PendingRequestMap; |
[email protected] | f7885d4 | 2012-04-03 08:30:04 | [diff] [blame] | 92 | |
| 93 | void InsertRequest(int request_id, PendingRequest* pending_request); |
| 94 | linked_ptr<PendingRequest> RemoveRequest(int request_id); |
| 95 | |
[email protected] | 8fe74bf | 2012-08-07 21:08:42 | [diff] [blame] | 96 | Dispatcher* dispatcher_; |
[email protected] | 21776be | 2012-07-09 03:53:32 | [diff] [blame] | 97 | PendingRequestMap pending_requests_; |
| 98 | |
[email protected] | eb7ef5f | 2014-02-06 09:59:19 | [diff] [blame^] | 99 | int source_tab_id_; // Id of the tab sending the request, or -1 if no tab. |
| 100 | |
[email protected] | 8fe74bf | 2012-08-07 21:08:42 | [diff] [blame] | 101 | DISALLOW_COPY_AND_ASSIGN(RequestSender); |
[email protected] | f7885d4 | 2012-04-03 08:30:04 | [diff] [blame] | 102 | }; |
| 103 | |
[email protected] | 8fe74bf | 2012-08-07 21:08:42 | [diff] [blame] | 104 | } // namespace extensions |
| 105 | |
| 106 | #endif // CHROME_RENDERER_EXTENSIONS_REQUEST_SENDER_H_ |