| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/extension_frame_helper.h" | 5 #include "extensions/renderer/extension_frame_helper.h" |
| 6 | 6 |
| 7 #include "base/metrics/histogram_macros.h" | 7 #include "base/metrics/histogram_macros.h" |
| 8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
| 9 #include "base/timer/elapsed_timer.h" | 9 #include "base/timer/elapsed_timer.h" |
| 10 #include "content/public/renderer/render_frame.h" | 10 #include "content/public/renderer/render_frame.h" |
| 11 #include "extensions/common/api/messaging/message.h" | 11 #include "extensions/common/api/messaging/message.h" |
| 12 #include "extensions/common/api/messaging/port_id.h" |
| 12 #include "extensions/common/constants.h" | 13 #include "extensions/common/constants.h" |
| 13 #include "extensions/common/extension_messages.h" | 14 #include "extensions/common/extension_messages.h" |
| 14 #include "extensions/common/manifest_handlers/background_info.h" | 15 #include "extensions/common/manifest_handlers/background_info.h" |
| 15 #include "extensions/renderer/console.h" | 16 #include "extensions/renderer/console.h" |
| 16 #include "extensions/renderer/content_watcher.h" | 17 #include "extensions/renderer/content_watcher.h" |
| 17 #include "extensions/renderer/dispatcher.h" | 18 #include "extensions/renderer/dispatcher.h" |
| 18 #include "extensions/renderer/messaging_bindings.h" | 19 #include "extensions/renderer/messaging_bindings.h" |
| 19 #include "extensions/renderer/script_context.h" | 20 #include "extensions/renderer/script_context.h" |
| 20 #include "third_party/WebKit/public/platform/WebSecurityOrigin.h" | 21 #include "third_party/WebKit/public/platform/WebSecurityOrigin.h" |
| 21 #include "third_party/WebKit/public/web/WebConsoleMessage.h" | 22 #include "third_party/WebKit/public/web/WebConsoleMessage.h" |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 } | 84 } |
| 84 | 85 |
| 85 enum class PortType { | 86 enum class PortType { |
| 86 EXTENSION, | 87 EXTENSION, |
| 87 TAB, | 88 TAB, |
| 88 NATIVE_APP, | 89 NATIVE_APP, |
| 89 }; | 90 }; |
| 90 | 91 |
| 91 } // namespace | 92 } // namespace |
| 92 | 93 |
| 93 struct ExtensionFrameHelper::PendingPortRequest { | |
| 94 PendingPortRequest(PortType type, const base::Callback<void(int)>& callback) | |
| 95 : type(type), callback(callback) {} | |
| 96 ~PendingPortRequest() {} | |
| 97 | |
| 98 base::ElapsedTimer timer; | |
| 99 PortType type; | |
| 100 base::Callback<void(int)> callback; | |
| 101 | |
| 102 private: | |
| 103 DISALLOW_COPY_AND_ASSIGN(PendingPortRequest); | |
| 104 }; | |
| 105 | |
| 106 ExtensionFrameHelper::ExtensionFrameHelper(content::RenderFrame* render_frame, | 94 ExtensionFrameHelper::ExtensionFrameHelper(content::RenderFrame* render_frame, |
| 107 Dispatcher* extension_dispatcher) | 95 Dispatcher* extension_dispatcher) |
| 108 : content::RenderFrameObserver(render_frame), | 96 : content::RenderFrameObserver(render_frame), |
| 109 content::RenderFrameObserverTracker<ExtensionFrameHelper>(render_frame), | 97 content::RenderFrameObserverTracker<ExtensionFrameHelper>(render_frame), |
| 110 view_type_(VIEW_TYPE_INVALID), | 98 view_type_(VIEW_TYPE_INVALID), |
| 111 tab_id_(-1), | 99 tab_id_(-1), |
| 112 browser_window_id_(-1), | 100 browser_window_id_(-1), |
| 113 extension_dispatcher_(extension_dispatcher), | 101 extension_dispatcher_(extension_dispatcher), |
| 114 did_create_current_document_element_(false), | 102 did_create_current_document_element_(false), |
| 115 next_port_request_id_(0), | |
| 116 weak_ptr_factory_(this) { | 103 weak_ptr_factory_(this) { |
| 117 g_frame_helpers.Get().insert(this); | 104 g_frame_helpers.Get().insert(this); |
| 118 } | 105 } |
| 119 | 106 |
| 120 ExtensionFrameHelper::~ExtensionFrameHelper() { | 107 ExtensionFrameHelper::~ExtensionFrameHelper() { |
| 121 g_frame_helpers.Get().erase(this); | 108 g_frame_helpers.Get().erase(this); |
| 122 } | 109 } |
| 123 | 110 |
| 124 // static | 111 // static |
| 125 std::vector<content::RenderFrame*> ExtensionFrameHelper::GetExtensionFrames( | 112 std::vector<content::RenderFrame*> ExtensionFrameHelper::GetExtensionFrames( |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 void ExtensionFrameHelper::ScheduleAtDocumentStart( | 174 void ExtensionFrameHelper::ScheduleAtDocumentStart( |
| 188 const base::Closure& callback) { | 175 const base::Closure& callback) { |
| 189 document_element_created_callbacks_.push_back(callback); | 176 document_element_created_callbacks_.push_back(callback); |
| 190 } | 177 } |
| 191 | 178 |
| 192 void ExtensionFrameHelper::ScheduleAtDocumentEnd( | 179 void ExtensionFrameHelper::ScheduleAtDocumentEnd( |
| 193 const base::Closure& callback) { | 180 const base::Closure& callback) { |
| 194 document_load_finished_callbacks_.push_back(callback); | 181 document_load_finished_callbacks_.push_back(callback); |
| 195 } | 182 } |
| 196 | 183 |
| 197 void ExtensionFrameHelper::RequestPortId( | |
| 198 const ExtensionMsg_ExternalConnectionInfo& info, | |
| 199 const std::string& channel_name, | |
| 200 bool include_tls_channel_id, | |
| 201 const base::Callback<void(int)>& callback) { | |
| 202 int port_request_id = next_port_request_id_++; | |
| 203 pending_port_requests_[port_request_id] = | |
| 204 base::MakeUnique<PendingPortRequest>(PortType::EXTENSION, callback); | |
| 205 { | |
| 206 SCOPED_UMA_HISTOGRAM_TIMER( | |
| 207 "Extensions.Messaging.GetPortIdSyncTime.Extension"); | |
| 208 render_frame()->Send(new ExtensionHostMsg_OpenChannelToExtension( | |
| 209 render_frame()->GetRoutingID(), info, channel_name, | |
| 210 include_tls_channel_id, port_request_id)); | |
| 211 } | |
| 212 } | |
| 213 | |
| 214 void ExtensionFrameHelper::RequestTabPortId( | |
| 215 const ExtensionMsg_TabTargetConnectionInfo& info, | |
| 216 const std::string& extension_id, | |
| 217 const std::string& channel_name, | |
| 218 const base::Callback<void(int)>& callback) { | |
| 219 int port_request_id = next_port_request_id_++; | |
| 220 pending_port_requests_[port_request_id] = | |
| 221 base::MakeUnique<PendingPortRequest>(PortType::TAB, callback); | |
| 222 { | |
| 223 SCOPED_UMA_HISTOGRAM_TIMER("Extensions.Messaging.GetPortIdSyncTime.Tab"); | |
| 224 render_frame()->Send(new ExtensionHostMsg_OpenChannelToTab( | |
| 225 render_frame()->GetRoutingID(), info, extension_id, channel_name, | |
| 226 port_request_id)); | |
| 227 } | |
| 228 } | |
| 229 | |
| 230 void ExtensionFrameHelper::RequestNativeAppPortId( | |
| 231 const std::string& native_app_name, | |
| 232 const base::Callback<void(int)>& callback) { | |
| 233 int port_request_id = next_port_request_id_++; | |
| 234 pending_port_requests_[port_request_id] = | |
| 235 base::MakeUnique<PendingPortRequest>(PortType::NATIVE_APP, callback); | |
| 236 { | |
| 237 SCOPED_UMA_HISTOGRAM_TIMER( | |
| 238 "Extensions.Messaging.GetPortIdSyncTime.NativeApp"); | |
| 239 render_frame()->Send(new ExtensionHostMsg_OpenChannelToNativeApp( | |
| 240 render_frame()->GetRoutingID(), native_app_name, port_request_id)); | |
| 241 } | |
| 242 } | |
| 243 | |
| 244 int ExtensionFrameHelper::RequestSyncPortId( | |
| 245 const ExtensionMsg_ExternalConnectionInfo& info, | |
| 246 const std::string& channel_name, | |
| 247 bool include_tls_channel_id) { | |
| 248 int port_id = 0; | |
| 249 render_frame()->Send(new ExtensionHostMsg_OpenChannelToExtensionSync( | |
| 250 render_frame()->GetRoutingID(), info, channel_name, | |
| 251 include_tls_channel_id, &port_id)); | |
| 252 return port_id; | |
| 253 } | |
| 254 | |
| 255 void ExtensionFrameHelper::DidMatchCSS( | 184 void ExtensionFrameHelper::DidMatchCSS( |
| 256 const blink::WebVector<blink::WebString>& newly_matching_selectors, | 185 const blink::WebVector<blink::WebString>& newly_matching_selectors, |
| 257 const blink::WebVector<blink::WebString>& stopped_matching_selectors) { | 186 const blink::WebVector<blink::WebString>& stopped_matching_selectors) { |
| 258 extension_dispatcher_->content_watcher()->DidMatchCSS( | 187 extension_dispatcher_->content_watcher()->DidMatchCSS( |
| 259 render_frame()->GetWebFrame(), newly_matching_selectors, | 188 render_frame()->GetWebFrame(), newly_matching_selectors, |
| 260 stopped_matching_selectors); | 189 stopped_matching_selectors); |
| 261 } | 190 } |
| 262 | 191 |
| 263 void ExtensionFrameHelper::DidStartProvisionalLoad() { | 192 void ExtensionFrameHelper::DidStartProvisionalLoad() { |
| 264 if (!delayed_main_world_script_initialization_) | 193 if (!delayed_main_world_script_initialization_) |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 310 IPC_MESSAGE_HANDLER(ExtensionMsg_DeliverMessage, OnExtensionDeliverMessage) | 239 IPC_MESSAGE_HANDLER(ExtensionMsg_DeliverMessage, OnExtensionDeliverMessage) |
| 311 IPC_MESSAGE_HANDLER(ExtensionMsg_DispatchOnDisconnect, | 240 IPC_MESSAGE_HANDLER(ExtensionMsg_DispatchOnDisconnect, |
| 312 OnExtensionDispatchOnDisconnect) | 241 OnExtensionDispatchOnDisconnect) |
| 313 IPC_MESSAGE_HANDLER(ExtensionMsg_SetTabId, OnExtensionSetTabId) | 242 IPC_MESSAGE_HANDLER(ExtensionMsg_SetTabId, OnExtensionSetTabId) |
| 314 IPC_MESSAGE_HANDLER(ExtensionMsg_UpdateBrowserWindowId, | 243 IPC_MESSAGE_HANDLER(ExtensionMsg_UpdateBrowserWindowId, |
| 315 OnUpdateBrowserWindowId) | 244 OnUpdateBrowserWindowId) |
| 316 IPC_MESSAGE_HANDLER(ExtensionMsg_NotifyRenderViewType, | 245 IPC_MESSAGE_HANDLER(ExtensionMsg_NotifyRenderViewType, |
| 317 OnNotifyRendererViewType) | 246 OnNotifyRendererViewType) |
| 318 IPC_MESSAGE_HANDLER(ExtensionMsg_Response, OnExtensionResponse) | 247 IPC_MESSAGE_HANDLER(ExtensionMsg_Response, OnExtensionResponse) |
| 319 IPC_MESSAGE_HANDLER(ExtensionMsg_MessageInvoke, OnExtensionMessageInvoke) | 248 IPC_MESSAGE_HANDLER(ExtensionMsg_MessageInvoke, OnExtensionMessageInvoke) |
| 320 IPC_MESSAGE_HANDLER(ExtensionMsg_AssignPortId, OnAssignPortId) | |
| 321 IPC_MESSAGE_UNHANDLED(handled = false) | 249 IPC_MESSAGE_UNHANDLED(handled = false) |
| 322 IPC_END_MESSAGE_MAP() | 250 IPC_END_MESSAGE_MAP() |
| 323 return handled; | 251 return handled; |
| 324 } | 252 } |
| 325 | 253 |
| 326 void ExtensionFrameHelper::OnExtensionValidateMessagePort(int port_id) { | 254 void ExtensionFrameHelper::OnExtensionValidateMessagePort(const PortId& id) { |
| 327 MessagingBindings::ValidateMessagePort( | 255 MessagingBindings::ValidateMessagePort( |
| 328 extension_dispatcher_->script_context_set(), port_id, render_frame()); | 256 extension_dispatcher_->script_context_set(), id, render_frame()); |
| 329 } | 257 } |
| 330 | 258 |
| 331 void ExtensionFrameHelper::OnExtensionDispatchOnConnect( | 259 void ExtensionFrameHelper::OnExtensionDispatchOnConnect( |
| 332 int target_port_id, | 260 const PortId& target_port_id, |
| 333 const std::string& channel_name, | 261 const std::string& channel_name, |
| 334 const ExtensionMsg_TabConnectionInfo& source, | 262 const ExtensionMsg_TabConnectionInfo& source, |
| 335 const ExtensionMsg_ExternalConnectionInfo& info, | 263 const ExtensionMsg_ExternalConnectionInfo& info, |
| 336 const std::string& tls_channel_id) { | 264 const std::string& tls_channel_id) { |
| 337 MessagingBindings::DispatchOnConnect( | 265 MessagingBindings::DispatchOnConnect( |
| 338 extension_dispatcher_->script_context_set(), | 266 extension_dispatcher_->script_context_set(), |
| 339 target_port_id, | 267 target_port_id, |
| 340 channel_name, | 268 channel_name, |
| 341 source, | 269 source, |
| 342 info, | 270 info, |
| 343 tls_channel_id, | 271 tls_channel_id, |
| 344 render_frame()); | 272 render_frame()); |
| 345 } | 273 } |
| 346 | 274 |
| 347 void ExtensionFrameHelper::OnExtensionDeliverMessage(int target_id, | 275 void ExtensionFrameHelper::OnExtensionDeliverMessage(const PortId& target_id, |
| 348 const Message& message) { | 276 const Message& message) { |
| 349 MessagingBindings::DeliverMessage( | 277 MessagingBindings::DeliverMessage( |
| 350 extension_dispatcher_->script_context_set(), target_id, message, | 278 extension_dispatcher_->script_context_set(), target_id, message, |
| 351 render_frame()); | 279 render_frame()); |
| 352 } | 280 } |
| 353 | 281 |
| 354 void ExtensionFrameHelper::OnExtensionDispatchOnDisconnect( | 282 void ExtensionFrameHelper::OnExtensionDispatchOnDisconnect( |
| 355 int port_id, | 283 const PortId& id, |
| 356 const std::string& error_message) { | 284 const std::string& error_message) { |
| 357 MessagingBindings::DispatchOnDisconnect( | 285 MessagingBindings::DispatchOnDisconnect( |
| 358 extension_dispatcher_->script_context_set(), port_id, error_message, | 286 extension_dispatcher_->script_context_set(), id, error_message, |
| 359 render_frame()); | 287 render_frame()); |
| 360 } | 288 } |
| 361 | 289 |
| 362 void ExtensionFrameHelper::OnExtensionSetTabId(int tab_id) { | 290 void ExtensionFrameHelper::OnExtensionSetTabId(int tab_id) { |
| 363 CHECK_EQ(tab_id_, -1); | 291 CHECK_EQ(tab_id_, -1); |
| 364 CHECK_GE(tab_id, 0); | 292 CHECK_GE(tab_id, 0); |
| 365 tab_id_ = tab_id; | 293 tab_id_ = tab_id; |
| 366 } | 294 } |
| 367 | 295 |
| 368 void ExtensionFrameHelper::OnUpdateBrowserWindowId(int browser_window_id) { | 296 void ExtensionFrameHelper::OnUpdateBrowserWindowId(int browser_window_id) { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 387 | 315 |
| 388 void ExtensionFrameHelper::OnExtensionMessageInvoke( | 316 void ExtensionFrameHelper::OnExtensionMessageInvoke( |
| 389 const std::string& extension_id, | 317 const std::string& extension_id, |
| 390 const std::string& module_name, | 318 const std::string& module_name, |
| 391 const std::string& function_name, | 319 const std::string& function_name, |
| 392 const base::ListValue& args) { | 320 const base::ListValue& args) { |
| 393 extension_dispatcher_->InvokeModuleSystemMethod( | 321 extension_dispatcher_->InvokeModuleSystemMethod( |
| 394 render_frame(), extension_id, module_name, function_name, args); | 322 render_frame(), extension_id, module_name, function_name, args); |
| 395 } | 323 } |
| 396 | 324 |
| 397 void ExtensionFrameHelper::OnAssignPortId(int port_id, int request_id) { | |
| 398 auto iter = pending_port_requests_.find(request_id); | |
| 399 DCHECK(iter != pending_port_requests_.end()); | |
| 400 PendingPortRequest& request = *iter->second; | |
| 401 switch (request.type) { | |
| 402 case PortType::EXTENSION: { | |
| 403 UMA_HISTOGRAM_TIMES("Extensions.Messaging.GetPortIdAsyncTime.Extension", | |
| 404 request.timer.Elapsed()); | |
| 405 break; | |
| 406 } | |
| 407 case PortType::TAB: { | |
| 408 UMA_HISTOGRAM_TIMES("Extensions.Messaging.GetPortIdAsyncTime.Tab", | |
| 409 request.timer.Elapsed()); | |
| 410 break; | |
| 411 } | |
| 412 case PortType::NATIVE_APP: { | |
| 413 UMA_HISTOGRAM_TIMES("Extensions.Messaging.GetPortIdAsyncTime.NativeApp", | |
| 414 request.timer.Elapsed()); | |
| 415 break; | |
| 416 } | |
| 417 } | |
| 418 request.callback.Run(port_id); | |
| 419 pending_port_requests_.erase(iter); | |
| 420 } | |
| 421 | |
| 422 void ExtensionFrameHelper::OnDestruct() { | 325 void ExtensionFrameHelper::OnDestruct() { |
| 423 delete this; | 326 delete this; |
| 424 } | 327 } |
| 425 | 328 |
| 426 } // namespace extensions | 329 } // namespace extensions |
| OLD | NEW |