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 CHROME_RENDERER_EXTENSIONS_REQUEST_SENDER_H_ | 5 #ifndef CHROME_RENDERER_EXTENSIONS_REQUEST_SENDER_H_ |
6 #define CHROME_RENDERER_EXTENSIONS_REQUEST_SENDER_H_ | 6 #define CHROME_RENDERER_EXTENSIONS_REQUEST_SENDER_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <map> | 9 #include <map> |
10 | 10 |
(...skipping 24 matching lines...) Expand all Loading... |
35 virtual ~Source() {} | 35 virtual ~Source() {} |
36 | 36 |
37 virtual ChromeV8Context* GetContext() = 0; | 37 virtual ChromeV8Context* GetContext() = 0; |
38 virtual void OnResponseReceived(const std::string& name, | 38 virtual void OnResponseReceived(const std::string& name, |
39 int request_id, | 39 int request_id, |
40 bool success, | 40 bool success, |
41 const base::ListValue& response, | 41 const base::ListValue& response, |
42 const std::string& error) = 0; | 42 const std::string& error) = 0; |
43 }; | 43 }; |
44 | 44 |
| 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 |
45 explicit RequestSender(Dispatcher* dispatcher); | 59 explicit RequestSender(Dispatcher* dispatcher); |
46 ~RequestSender(); | 60 ~RequestSender(); |
47 | 61 |
48 // In order to avoid collision, all |request_id|s passed into StartRequest() | 62 // In order to avoid collision, all |request_id|s passed into StartRequest() |
49 // should be generated by this method. | 63 // should be generated by this method. |
50 int GetNextRequestId() const; | 64 int GetNextRequestId() const; |
51 | 65 |
52 // Makes a call to the API function |name| that is to be handled by the | 66 // Makes a call to the API function |name| that is to be handled by the |
53 // extension host. The response to this request will be received in | 67 // extension host. The response to this request will be received in |
54 // HandleResponse(). | 68 // HandleResponse(). |
(...skipping 11 matching lines...) Expand all Loading... |
66 void HandleResponse(int request_id, | 80 void HandleResponse(int request_id, |
67 bool success, | 81 bool success, |
68 const base::ListValue& response, | 82 const base::ListValue& response, |
69 const std::string& error); | 83 const std::string& error); |
70 | 84 |
71 // Notifies this that a request source is no longer valid. | 85 // Notifies this that a request source is no longer valid. |
72 // TODO(kalman): Do this in a generic/safe way. | 86 // TODO(kalman): Do this in a generic/safe way. |
73 void InvalidateSource(Source* source); | 87 void InvalidateSource(Source* source); |
74 | 88 |
75 private: | 89 private: |
| 90 friend class ScopedTabID; |
76 typedef std::map<int, linked_ptr<PendingRequest> > PendingRequestMap; | 91 typedef std::map<int, linked_ptr<PendingRequest> > PendingRequestMap; |
77 | 92 |
78 void InsertRequest(int request_id, PendingRequest* pending_request); | 93 void InsertRequest(int request_id, PendingRequest* pending_request); |
79 linked_ptr<PendingRequest> RemoveRequest(int request_id); | 94 linked_ptr<PendingRequest> RemoveRequest(int request_id); |
80 | 95 |
81 Dispatcher* dispatcher_; | 96 Dispatcher* dispatcher_; |
82 PendingRequestMap pending_requests_; | 97 PendingRequestMap pending_requests_; |
83 | 98 |
| 99 int source_tab_id_; // Id of the tab sending the request, or -1 if no tab. |
| 100 |
84 DISALLOW_COPY_AND_ASSIGN(RequestSender); | 101 DISALLOW_COPY_AND_ASSIGN(RequestSender); |
85 }; | 102 }; |
86 | 103 |
87 } // namespace extensions | 104 } // namespace extensions |
88 | 105 |
89 #endif // CHROME_RENDERER_EXTENSIONS_REQUEST_SENDER_H_ | 106 #endif // CHROME_RENDERER_EXTENSIONS_REQUEST_SENDER_H_ |
OLD | NEW |