| 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 #include "extensions/renderer/request_sender.h" | 5 #include "extensions/renderer/request_sender.h" |
| 6 | 6 |
| 7 #include "base/metrics/histogram_macros.h" |
| 8 #include "base/timer/elapsed_timer.h" |
| 7 #include "base/values.h" | 9 #include "base/values.h" |
| 8 #include "content/public/renderer/render_frame.h" | 10 #include "content/public/renderer/render_frame.h" |
| 9 #include "extensions/common/extension_messages.h" | 11 #include "extensions/common/extension_messages.h" |
| 10 #include "extensions/renderer/script_context.h" | 12 #include "extensions/renderer/script_context.h" |
| 11 #include "third_party/WebKit/public/web/WebDocument.h" | 13 #include "third_party/WebKit/public/web/WebDocument.h" |
| 12 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 14 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| 13 #include "third_party/WebKit/public/web/WebScopedUserGesture.h" | 15 #include "third_party/WebKit/public/web/WebScopedUserGesture.h" |
| 14 #include "third_party/WebKit/public/web/WebUserGestureIndicator.h" | 16 #include "third_party/WebKit/public/web/WebUserGestureIndicator.h" |
| 15 #include "third_party/WebKit/public/web/WebUserGestureToken.h" | 17 #include "third_party/WebKit/public/web/WebUserGestureToken.h" |
| 16 | 18 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 linked_ptr<PendingRequest> result = i->second; | 61 linked_ptr<PendingRequest> result = i->second; |
| 60 pending_requests_.erase(i); | 62 pending_requests_.erase(i); |
| 61 return result; | 63 return result; |
| 62 } | 64 } |
| 63 | 65 |
| 64 int RequestSender::GetNextRequestId() const { | 66 int RequestSender::GetNextRequestId() const { |
| 65 static int next_request_id = 0; | 67 static int next_request_id = 0; |
| 66 return next_request_id++; | 68 return next_request_id++; |
| 67 } | 69 } |
| 68 | 70 |
| 69 void RequestSender::StartRequest(Source* source, | 71 bool RequestSender::StartRequest(Source* source, |
| 70 const std::string& name, | 72 const std::string& name, |
| 71 int request_id, | 73 int request_id, |
| 72 bool has_callback, | 74 bool has_callback, |
| 73 bool for_io_thread, | 75 bool for_io_thread, |
| 74 base::ListValue* value_args) { | 76 base::ListValue* value_args) { |
| 75 ScriptContext* context = source->GetContext(); | 77 ScriptContext* context = source->GetContext(); |
| 76 if (!context) | 78 if (!context) |
| 77 return; | 79 return false; |
| 78 | 80 |
| 79 bool for_service_worker = | 81 bool for_service_worker = |
| 80 context->context_type() == Feature::SERVICE_WORKER_CONTEXT; | 82 context->context_type() == Feature::SERVICE_WORKER_CONTEXT; |
| 81 // Get the current RenderFrame so that we can send a routed IPC message from | 83 // Get the current RenderFrame so that we can send a routed IPC message from |
| 82 // the correct source. | 84 // the correct source. |
| 83 // Note that |render_frame| would be nullptr for Service Workers. Service | 85 // Note that |render_frame| would be nullptr for Service Workers. Service |
| 84 // Workers use control IPC instead. | 86 // Workers use control IPC instead. |
| 85 content::RenderFrame* render_frame = context->GetRenderFrame(); | 87 content::RenderFrame* render_frame = context->GetRenderFrame(); |
| 86 if (!for_service_worker && !render_frame) { | 88 if (!for_service_worker && !render_frame) { |
| 87 // It is important to early exit here for non Service Worker contexts so | 89 // It is important to early exit here for non Service Worker contexts so |
| 88 // that we do not create orphaned PendingRequests below. | 90 // that we do not create orphaned PendingRequests below. |
| 89 return; | 91 return false; |
| 90 } | 92 } |
| 91 | 93 |
| 92 // TODO(koz): See if we can make this a CHECK. | 94 // TODO(koz): See if we can make this a CHECK. |
| 93 if (!context->HasAccessOrThrowError(name)) | 95 if (!context->HasAccessOrThrowError(name)) |
| 94 return; | 96 return false; |
| 95 | 97 |
| 96 GURL source_url; | 98 GURL source_url; |
| 97 if (blink::WebLocalFrame* webframe = context->web_frame()) | 99 if (blink::WebLocalFrame* webframe = context->web_frame()) |
| 98 source_url = webframe->document().url(); | 100 source_url = webframe->document().url(); |
| 99 | 101 |
| 100 InsertRequest(request_id, new PendingRequest(name, source, | 102 InsertRequest(request_id, new PendingRequest(name, source, |
| 101 blink::WebUserGestureIndicator::currentUserGestureToken())); | 103 blink::WebUserGestureIndicator::currentUserGestureToken())); |
| 102 | 104 |
| 103 ExtensionHostMsg_Request_Params params; | 105 ExtensionHostMsg_Request_Params params; |
| 104 params.name = name; | 106 params.name = name; |
| 105 params.arguments.Swap(value_args); | 107 params.arguments.Swap(value_args); |
| 106 params.extension_id = context->GetExtensionID(); | 108 params.extension_id = context->GetExtensionID(); |
| 107 params.source_url = source_url; | 109 params.source_url = source_url; |
| 108 params.source_tab_id = source_tab_id_; | 110 params.source_tab_id = source_tab_id_; |
| 109 params.request_id = request_id; | 111 params.request_id = request_id; |
| 110 params.has_callback = has_callback; | 112 params.has_callback = has_callback; |
| 111 params.user_gesture = | 113 params.user_gesture = |
| 112 blink::WebUserGestureIndicator::isProcessingUserGesture(); | 114 blink::WebUserGestureIndicator::isProcessingUserGesture(); |
| 113 | 115 |
| 114 // Set Service Worker specific params to default values. | 116 // Set Service Worker specific params to default values. |
| 115 params.worker_thread_id = -1; | 117 params.worker_thread_id = -1; |
| 116 params.embedded_worker_id = -1; | 118 params.embedded_worker_id = -1; |
| 117 | 119 |
| 118 SendRequest(render_frame, for_io_thread, params); | 120 SendRequest(render_frame, for_io_thread, params); |
| 121 return true; |
| 119 } | 122 } |
| 120 | 123 |
| 121 void RequestSender::SendRequest(content::RenderFrame* render_frame, | 124 void RequestSender::SendRequest(content::RenderFrame* render_frame, |
| 122 bool for_io_thread, | 125 bool for_io_thread, |
| 123 ExtensionHostMsg_Request_Params& params) { | 126 ExtensionHostMsg_Request_Params& params) { |
| 124 if (for_io_thread) { | 127 if (for_io_thread) { |
| 125 render_frame->Send(new ExtensionHostMsg_RequestForIOThread( | 128 render_frame->Send(new ExtensionHostMsg_RequestForIOThread( |
| 126 render_frame->GetRoutingID(), params)); | 129 render_frame->GetRoutingID(), params)); |
| 127 } else { | 130 } else { |
| 128 render_frame->Send( | 131 render_frame->Send( |
| 129 new ExtensionHostMsg_Request(render_frame->GetRoutingID(), params)); | 132 new ExtensionHostMsg_Request(render_frame->GetRoutingID(), params)); |
| 130 } | 133 } |
| 131 } | 134 } |
| 132 | 135 |
| 133 void RequestSender::HandleResponse(int request_id, | 136 void RequestSender::HandleResponse(int request_id, |
| 134 bool success, | 137 bool success, |
| 135 const base::ListValue& response, | 138 const base::ListValue& response, |
| 136 const std::string& error) { | 139 const std::string& error) { |
| 140 base::ElapsedTimer timer; |
| 137 linked_ptr<PendingRequest> request = RemoveRequest(request_id); | 141 linked_ptr<PendingRequest> request = RemoveRequest(request_id); |
| 138 | 142 |
| 139 if (!request.get()) { | 143 if (!request.get()) { |
| 140 // This can happen if a context is destroyed while a request is in flight. | 144 // This can happen if a context is destroyed while a request is in flight. |
| 141 return; | 145 return; |
| 142 } | 146 } |
| 143 | 147 |
| 148 // TODO(devlin): Would it be useful to partition this data based on |
| 149 // extension function once we have a suitable baseline? crbug.com/608561. |
| 144 blink::WebScopedUserGesture gesture(request->token); | 150 blink::WebScopedUserGesture gesture(request->token); |
| 145 request->source->OnResponseReceived( | 151 request->source->OnResponseReceived( |
| 146 request->name, request_id, success, response, error); | 152 request->name, request_id, success, response, error); |
| 153 UMA_HISTOGRAM_TIMES("Extensions.Functions.HandleResponseElapsedTime", |
| 154 timer.Elapsed()); |
| 147 } | 155 } |
| 148 | 156 |
| 149 void RequestSender::InvalidateSource(Source* source) { | 157 void RequestSender::InvalidateSource(Source* source) { |
| 150 for (PendingRequestMap::iterator it = pending_requests_.begin(); | 158 for (PendingRequestMap::iterator it = pending_requests_.begin(); |
| 151 it != pending_requests_.end();) { | 159 it != pending_requests_.end();) { |
| 152 if (it->second->source == source) | 160 if (it->second->source == source) |
| 153 pending_requests_.erase(it++); | 161 pending_requests_.erase(it++); |
| 154 else | 162 else |
| 155 ++it; | 163 ++it; |
| 156 } | 164 } |
| 157 } | 165 } |
| 158 | 166 |
| 159 } // namespace extensions | 167 } // namespace extensions |
| OLD | NEW |