| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 EXTENSIONS_RENDERER_REQUEST_SENDER_H_ | 5 #ifndef EXTENSIONS_RENDERER_REQUEST_SENDER_H_ |
| 6 #define EXTENSIONS_RENDERER_REQUEST_SENDER_H_ | 6 #define EXTENSIONS_RENDERER_REQUEST_SENDER_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/memory/linked_ptr.h" | 12 #include "base/memory/linked_ptr.h" |
| 13 #include "v8/include/v8.h" | 13 #include "v8/include/v8.h" |
| 14 | 14 |
| 15 struct ExtensionHostMsg_Request_Params; |
| 16 |
| 15 namespace base { | 17 namespace base { |
| 16 class ListValue; | 18 class ListValue; |
| 17 } | 19 } |
| 18 | 20 |
| 21 namespace content { |
| 22 class RenderFrame; |
| 23 } |
| 24 |
| 19 namespace extensions { | 25 namespace extensions { |
| 20 class ScriptContext; | 26 class ScriptContext; |
| 21 | 27 |
| 22 struct PendingRequest; | 28 struct PendingRequest; |
| 23 | 29 |
| 24 // Responsible for sending requests for named extension API functions to the | 30 // Responsible for sending requests for named extension API functions to the |
| 25 // extension host and routing the responses back to the caller. | 31 // extension host and routing the responses back to the caller. |
| 26 class RequestSender { | 32 class RequestSender { |
| 27 public: | 33 public: |
| 28 // Source represents a user of RequestSender. Every request is associated with | 34 // Source represents a user of RequestSender. Every request is associated with |
| (...skipping 21 matching lines...) Expand all Loading... |
| 50 | 56 |
| 51 private: | 57 private: |
| 52 RequestSender* const request_sender_; | 58 RequestSender* const request_sender_; |
| 53 const int tab_id_; | 59 const int tab_id_; |
| 54 const int previous_tab_id_; | 60 const int previous_tab_id_; |
| 55 | 61 |
| 56 DISALLOW_COPY_AND_ASSIGN(ScopedTabID); | 62 DISALLOW_COPY_AND_ASSIGN(ScopedTabID); |
| 57 }; | 63 }; |
| 58 | 64 |
| 59 RequestSender(); | 65 RequestSender(); |
| 60 ~RequestSender(); | 66 virtual ~RequestSender(); |
| 61 | 67 |
| 62 // In order to avoid collision, all |request_id|s passed into StartRequest() | 68 // In order to avoid collision, all |request_id|s passed into StartRequest() |
| 63 // should be generated by this method. | 69 // should be generated by this method. |
| 64 int GetNextRequestId() const; | 70 int GetNextRequestId() const; |
| 65 | 71 |
| 66 // Makes a call to the API function |name| that is to be handled by the | 72 // Makes a call to the API function |name| that is to be handled by the |
| 67 // extension host. The response to this request will be received in | 73 // extension host. The response to this request will be received in |
| 68 // HandleResponse(). | 74 // HandleResponse(). |
| 69 // TODO(koz): Remove |request_id| and generate that internally. | 75 // TODO(koz): Remove |request_id| and generate that internally. |
| 70 // There are multiple of these per render view though, so we'll | 76 // There are multiple of these per render view though, so we'll |
| 71 // need to vend the IDs centrally. | 77 // need to vend the IDs centrally. |
| 72 void StartRequest(Source* source, | 78 void StartRequest(Source* source, |
| 73 const std::string& name, | 79 const std::string& name, |
| 74 int request_id, | 80 int request_id, |
| 75 bool has_callback, | 81 bool has_callback, |
| 76 bool for_io_thread, | 82 bool for_io_thread, |
| 77 base::ListValue* value_args); | 83 base::ListValue* value_args); |
| 78 | 84 |
| 85 // Sends the IPC to extension host for the API function that is described |
| 86 // in |params|. |
| 87 virtual void SendRequest(content::RenderFrame* render_frame, |
| 88 bool for_io_thread, |
| 89 ExtensionHostMsg_Request_Params& params); |
| 90 |
| 79 // Handles responses from the extension host to calls made by StartRequest(). | 91 // Handles responses from the extension host to calls made by StartRequest(). |
| 80 void HandleResponse(int request_id, | 92 void HandleResponse(int request_id, |
| 81 bool success, | 93 bool success, |
| 82 const base::ListValue& response, | 94 const base::ListValue& response, |
| 83 const std::string& error); | 95 const std::string& error); |
| 84 | 96 |
| 85 // Notifies this that a request source is no longer valid. | 97 // Notifies this that a request source is no longer valid. |
| 86 // TODO(kalman): Do this in a generic/safe way. | 98 // TODO(kalman): Do this in a generic/safe way. |
| 87 void InvalidateSource(Source* source); | 99 void InvalidateSource(Source* source); |
| 88 | 100 |
| 89 private: | 101 private: |
| 90 friend class ScopedTabID; | 102 friend class ScopedTabID; |
| 91 typedef std::map<int, linked_ptr<PendingRequest> > PendingRequestMap; | 103 typedef std::map<int, linked_ptr<PendingRequest> > PendingRequestMap; |
| 92 | 104 |
| 93 void InsertRequest(int request_id, PendingRequest* pending_request); | 105 void InsertRequest(int request_id, PendingRequest* pending_request); |
| 94 linked_ptr<PendingRequest> RemoveRequest(int request_id); | 106 linked_ptr<PendingRequest> RemoveRequest(int request_id); |
| 95 | 107 |
| 96 PendingRequestMap pending_requests_; | 108 PendingRequestMap pending_requests_; |
| 97 | 109 |
| 98 int source_tab_id_; // Id of the tab sending the request, or -1 if no tab. | 110 int source_tab_id_; // Id of the tab sending the request, or -1 if no tab. |
| 99 | 111 |
| 100 DISALLOW_COPY_AND_ASSIGN(RequestSender); | 112 DISALLOW_COPY_AND_ASSIGN(RequestSender); |
| 101 }; | 113 }; |
| 102 | 114 |
| 103 } // namespace extensions | 115 } // namespace extensions |
| 104 | 116 |
| 105 #endif // EXTENSIONS_RENDERER_REQUEST_SENDER_H_ | 117 #endif // EXTENSIONS_RENDERER_REQUEST_SENDER_H_ |
| OLD | NEW |