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 #include "chrome/renderer/extensions/request_sender.h" | 5 #include "extensions/renderer/request_sender.h" |
6 | 6 |
7 #include "base/values.h" | 7 #include "base/values.h" |
8 #include "chrome/renderer/extensions/chrome_v8_context.h" | |
9 #include "chrome/renderer/extensions/dispatcher.h" | 8 #include "chrome/renderer/extensions/dispatcher.h" |
10 #include "content/public/renderer/render_view.h" | 9 #include "content/public/renderer/render_view.h" |
11 #include "extensions/common/extension_messages.h" | 10 #include "extensions/common/extension_messages.h" |
| 11 #include "extensions/renderer/script_context.h" |
12 #include "third_party/WebKit/public/web/WebDocument.h" | 12 #include "third_party/WebKit/public/web/WebDocument.h" |
13 #include "third_party/WebKit/public/web/WebFrame.h" | 13 #include "third_party/WebKit/public/web/WebFrame.h" |
14 #include "third_party/WebKit/public/web/WebUserGestureIndicator.h" | 14 #include "third_party/WebKit/public/web/WebUserGestureIndicator.h" |
15 | 15 |
16 namespace extensions { | 16 namespace extensions { |
17 | 17 |
18 // Contains info relevant to a pending API request. | 18 // Contains info relevant to a pending API request. |
19 struct PendingRequest { | 19 struct PendingRequest { |
20 public : | 20 public: |
21 PendingRequest(const std::string& name, RequestSender::Source* source) | 21 PendingRequest(const std::string& name, RequestSender::Source* source) |
22 : name(name), source(source) { | 22 : name(name), source(source) {} |
23 } | |
24 | 23 |
25 std::string name; | 24 std::string name; |
26 RequestSender::Source* source; | 25 RequestSender::Source* source; |
27 }; | 26 }; |
28 | 27 |
29 RequestSender::ScopedTabID::ScopedTabID(RequestSender* request_sender, | 28 RequestSender::ScopedTabID::ScopedTabID(RequestSender* request_sender, |
30 int tab_id) | 29 int tab_id) |
31 : request_sender_(request_sender), | 30 : request_sender_(request_sender), |
32 tab_id_(tab_id), | 31 tab_id_(tab_id), |
33 previous_tab_id_(request_sender->source_tab_id_) { | 32 previous_tab_id_(request_sender->source_tab_id_) { |
34 request_sender_->source_tab_id_ = tab_id; | 33 request_sender_->source_tab_id_ = tab_id; |
35 } | 34 } |
36 | 35 |
37 RequestSender::ScopedTabID::~ScopedTabID() { | 36 RequestSender::ScopedTabID::~ScopedTabID() { |
38 DCHECK_EQ(tab_id_, request_sender_->source_tab_id_); | 37 DCHECK_EQ(tab_id_, request_sender_->source_tab_id_); |
39 request_sender_->source_tab_id_ = previous_tab_id_; | 38 request_sender_->source_tab_id_ = previous_tab_id_; |
40 } | 39 } |
41 | 40 |
42 RequestSender::RequestSender(Dispatcher* dispatcher) | 41 RequestSender::RequestSender(Dispatcher* dispatcher) |
43 : dispatcher_(dispatcher), | 42 : dispatcher_(dispatcher), source_tab_id_(-1) {} |
44 source_tab_id_(-1) { | |
45 } | |
46 | 43 |
47 RequestSender::~RequestSender() { | 44 RequestSender::~RequestSender() {} |
48 } | |
49 | 45 |
50 void RequestSender::InsertRequest(int request_id, | 46 void RequestSender::InsertRequest(int request_id, |
51 PendingRequest* pending_request) { | 47 PendingRequest* pending_request) { |
52 DCHECK_EQ(0u, pending_requests_.count(request_id)); | 48 DCHECK_EQ(0u, pending_requests_.count(request_id)); |
53 pending_requests_[request_id].reset(pending_request); | 49 pending_requests_[request_id].reset(pending_request); |
54 } | 50 } |
55 | 51 |
56 linked_ptr<PendingRequest> RequestSender::RemoveRequest(int request_id) { | 52 linked_ptr<PendingRequest> RequestSender::RemoveRequest(int request_id) { |
57 PendingRequestMap::iterator i = pending_requests_.find(request_id); | 53 PendingRequestMap::iterator i = pending_requests_.find(request_id); |
58 if (i == pending_requests_.end()) | 54 if (i == pending_requests_.end()) |
59 return linked_ptr<PendingRequest>(); | 55 return linked_ptr<PendingRequest>(); |
60 linked_ptr<PendingRequest> result = i->second; | 56 linked_ptr<PendingRequest> result = i->second; |
61 pending_requests_.erase(i); | 57 pending_requests_.erase(i); |
62 return result; | 58 return result; |
63 } | 59 } |
64 | 60 |
65 int RequestSender::GetNextRequestId() const { | 61 int RequestSender::GetNextRequestId() const { |
66 static int next_request_id = 0; | 62 static int next_request_id = 0; |
67 return next_request_id++; | 63 return next_request_id++; |
68 } | 64 } |
69 | 65 |
70 void RequestSender::StartRequest(Source* source, | 66 void RequestSender::StartRequest(Source* source, |
71 const std::string& name, | 67 const std::string& name, |
72 int request_id, | 68 int request_id, |
73 bool has_callback, | 69 bool has_callback, |
74 bool for_io_thread, | 70 bool for_io_thread, |
75 base::ListValue* value_args) { | 71 base::ListValue* value_args) { |
76 ChromeV8Context* context = source->GetContext(); | 72 ScriptContext* context = source->GetContext(); |
77 if (!context) | 73 if (!context) |
78 return; | 74 return; |
79 | 75 |
80 // Get the current RenderView so that we can send a routed IPC message from | 76 // Get the current RenderView so that we can send a routed IPC message from |
81 // the correct source. | 77 // the correct source. |
82 content::RenderView* renderview = context->GetRenderView(); | 78 content::RenderView* renderview = context->GetRenderView(); |
83 if (!renderview) | 79 if (!renderview) |
84 return; | 80 return; |
85 | 81 |
86 const std::set<std::string>& function_names = dispatcher_->function_names(); | 82 const std::set<std::string>& function_names = dispatcher_->function_names(); |
87 if (function_names.find(name) == function_names.end()) { | 83 if (function_names.find(name) == function_names.end()) { |
88 NOTREACHED() << "Unexpected function " << name << | 84 NOTREACHED() |
89 ". Did you remember to register it with ExtensionFunctionRegistry?"; | 85 << "Unexpected function " << name |
| 86 << ". Did you remember to register it with ExtensionFunctionRegistry?"; |
90 return; | 87 return; |
91 } | 88 } |
92 | 89 |
93 // TODO(koz): See if we can make this a CHECK. | 90 // TODO(koz): See if we can make this a CHECK. |
94 if (!dispatcher_->CheckContextAccessToExtensionAPI(name, context)) | 91 if (!dispatcher_->CheckContextAccessToExtensionAPI(name, context)) |
95 return; | 92 return; |
96 | 93 |
97 GURL source_url; | 94 GURL source_url; |
98 if (blink::WebFrame* webframe = context->web_frame()) | 95 if (blink::WebFrame* webframe = context->web_frame()) |
99 source_url = webframe->document().url(); | 96 source_url = webframe->document().url(); |
100 | 97 |
101 InsertRequest(request_id, new PendingRequest(name, source)); | 98 InsertRequest(request_id, new PendingRequest(name, source)); |
102 | 99 |
103 ExtensionHostMsg_Request_Params params; | 100 ExtensionHostMsg_Request_Params params; |
104 params.name = name; | 101 params.name = name; |
105 params.arguments.Swap(value_args); | 102 params.arguments.Swap(value_args); |
106 params.extension_id = context->GetExtensionID(); | 103 params.extension_id = context->GetExtensionID(); |
107 params.source_url = source_url; | 104 params.source_url = source_url; |
108 params.source_tab_id = source_tab_id_; | 105 params.source_tab_id = source_tab_id_; |
109 params.request_id = request_id; | 106 params.request_id = request_id; |
110 params.has_callback = has_callback; | 107 params.has_callback = has_callback; |
111 params.user_gesture = | 108 params.user_gesture = |
112 blink::WebUserGestureIndicator::isProcessingUserGesture(); | 109 blink::WebUserGestureIndicator::isProcessingUserGesture(); |
113 if (for_io_thread) { | 110 if (for_io_thread) { |
114 renderview->Send(new ExtensionHostMsg_RequestForIOThread( | 111 renderview->Send(new ExtensionHostMsg_RequestForIOThread( |
115 renderview->GetRoutingID(), params)); | 112 renderview->GetRoutingID(), params)); |
116 } else { | 113 } else { |
117 renderview->Send(new ExtensionHostMsg_Request( | 114 renderview->Send( |
118 renderview->GetRoutingID(), params)); | 115 new ExtensionHostMsg_Request(renderview->GetRoutingID(), params)); |
119 } | 116 } |
120 } | 117 } |
121 | 118 |
122 void RequestSender::HandleResponse(int request_id, | 119 void RequestSender::HandleResponse(int request_id, |
123 bool success, | 120 bool success, |
124 const base::ListValue& response, | 121 const base::ListValue& response, |
125 const std::string& error) { | 122 const std::string& error) { |
126 linked_ptr<PendingRequest> request = RemoveRequest(request_id); | 123 linked_ptr<PendingRequest> request = RemoveRequest(request_id); |
127 | 124 |
128 if (!request.get()) { | 125 if (!request.get()) { |
129 // This can happen if a context is destroyed while a request is in flight. | 126 // This can happen if a context is destroyed while a request is in flight. |
130 return; | 127 return; |
131 } | 128 } |
132 | 129 |
133 request->source->OnResponseReceived(request->name, request_id, success, | 130 request->source->OnResponseReceived( |
134 response, error); | 131 request->name, request_id, success, response, error); |
135 } | 132 } |
136 | 133 |
137 void RequestSender::InvalidateSource(Source* source) { | 134 void RequestSender::InvalidateSource(Source* source) { |
138 for (PendingRequestMap::iterator it = pending_requests_.begin(); | 135 for (PendingRequestMap::iterator it = pending_requests_.begin(); |
139 it != pending_requests_.end();) { | 136 it != pending_requests_.end();) { |
140 if (it->second->source == source) | 137 if (it->second->source == source) |
141 pending_requests_.erase(it++); | 138 pending_requests_.erase(it++); |
142 else | 139 else |
143 ++it; | 140 ++it; |
144 } | 141 } |
145 } | 142 } |
146 | 143 |
147 } // namespace extensions | 144 } // namespace extensions |
OLD | NEW |