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/browser/extension_host.h" | 5 #include "extensions/browser/extension_host.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/metrics/field_trial.h" | 8 #include "base/metrics/field_trial.h" |
9 #include "base/metrics/histogram_macros.h" | 9 #include "base/metrics/histogram_macros.h" |
10 #include "base/profiler/scoped_tracker.h" | 10 #include "base/profiler/scoped_tracker.h" |
11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
13 #include "content/public/browser/browser_context.h" | 13 #include "content/public/browser/browser_context.h" |
14 #include "content/public/browser/content_browser_client.h" | 14 #include "content/public/browser/content_browser_client.h" |
15 #include "content/public/browser/native_web_keyboard_event.h" | 15 #include "content/public/browser/native_web_keyboard_event.h" |
16 #include "content/public/browser/notification_service.h" | 16 #include "content/public/browser/notification_service.h" |
17 #include "content/public/browser/render_process_host.h" | 17 #include "content/public/browser/render_process_host.h" |
18 #include "content/public/browser/render_view_host.h" | 18 #include "content/public/browser/render_view_host.h" |
19 #include "content/public/browser/render_widget_host_view.h" | 19 #include "content/public/browser/render_widget_host_view.h" |
20 #include "content/public/browser/site_instance.h" | 20 #include "content/public/browser/site_instance.h" |
21 #include "content/public/browser/web_contents.h" | 21 #include "content/public/browser/web_contents.h" |
22 #include "extensions/browser/bad_message.h" | 22 #include "extensions/browser/bad_message.h" |
23 #include "extensions/browser/event_router.h" | 23 #include "extensions/browser/event_router.h" |
24 #include "extensions/browser/extension_error.h" | 24 #include "extensions/browser/extension_error.h" |
25 #include "extensions/browser/extension_host_delegate.h" | 25 #include "extensions/browser/extension_host_delegate.h" |
26 #include "extensions/browser/extension_host_observer.h" | 26 #include "extensions/browser/extension_host_observer.h" |
27 #include "extensions/browser/extension_host_queue.h" | 27 #include "extensions/browser/extension_host_queue.h" |
28 #include "extensions/browser/extension_registry.h" | 28 #include "extensions/browser/extension_registry.h" |
29 #include "extensions/browser/extension_system.h" | 29 #include "extensions/browser/extension_system.h" |
| 30 #include "extensions/browser/extension_web_contents_observer.h" |
30 #include "extensions/browser/extensions_browser_client.h" | 31 #include "extensions/browser/extensions_browser_client.h" |
31 #include "extensions/browser/load_monitoring_extension_host_queue.h" | 32 #include "extensions/browser/load_monitoring_extension_host_queue.h" |
32 #include "extensions/browser/notification_types.h" | 33 #include "extensions/browser/notification_types.h" |
33 #include "extensions/browser/process_manager.h" | 34 #include "extensions/browser/process_manager.h" |
34 #include "extensions/browser/runtime_data.h" | 35 #include "extensions/browser/runtime_data.h" |
35 #include "extensions/browser/view_type_utils.h" | 36 #include "extensions/browser/view_type_utils.h" |
36 #include "extensions/common/extension.h" | 37 #include "extensions/common/extension.h" |
37 #include "extensions/common/extension_messages.h" | 38 #include "extensions/common/extension_messages.h" |
38 #include "extensions/common/extension_urls.h" | 39 #include "extensions/common/extension_urls.h" |
39 #include "extensions/common/feature_switch.h" | 40 #include "extensions/common/feature_switch.h" |
(...skipping 15 matching lines...) Expand all Loading... |
55 const GURL& url, | 56 const GURL& url, |
56 ViewType host_type) | 57 ViewType host_type) |
57 : delegate_(ExtensionsBrowserClient::Get()->CreateExtensionHostDelegate()), | 58 : delegate_(ExtensionsBrowserClient::Get()->CreateExtensionHostDelegate()), |
58 extension_(extension), | 59 extension_(extension), |
59 extension_id_(extension->id()), | 60 extension_id_(extension->id()), |
60 browser_context_(site_instance->GetBrowserContext()), | 61 browser_context_(site_instance->GetBrowserContext()), |
61 render_view_host_(nullptr), | 62 render_view_host_(nullptr), |
62 has_loaded_once_(false), | 63 has_loaded_once_(false), |
63 document_element_available_(false), | 64 document_element_available_(false), |
64 initial_url_(url), | 65 initial_url_(url), |
65 extension_function_dispatcher_(browser_context_, this), | |
66 extension_host_type_(host_type) { | 66 extension_host_type_(host_type) { |
67 // Not used for panels, see PanelHost. | 67 // Not used for panels, see PanelHost. |
68 DCHECK(host_type == VIEW_TYPE_EXTENSION_BACKGROUND_PAGE || | 68 DCHECK(host_type == VIEW_TYPE_EXTENSION_BACKGROUND_PAGE || |
69 host_type == VIEW_TYPE_EXTENSION_DIALOG || | 69 host_type == VIEW_TYPE_EXTENSION_DIALOG || |
70 host_type == VIEW_TYPE_EXTENSION_POPUP); | 70 host_type == VIEW_TYPE_EXTENSION_POPUP); |
71 host_contents_.reset(WebContents::Create( | 71 host_contents_.reset(WebContents::Create( |
72 WebContents::CreateParams(browser_context_, site_instance))), | 72 WebContents::CreateParams(browser_context_, site_instance))), |
73 content::WebContentsObserver::Observe(host_contents_.get()); | 73 content::WebContentsObserver::Observe(host_contents_.get()); |
74 host_contents_->SetDelegate(this); | 74 host_contents_->SetDelegate(this); |
75 SetViewType(host_contents_.get(), host_type); | 75 SetViewType(host_contents_.get(), host_type); |
76 | 76 |
77 render_view_host_ = host_contents_->GetRenderViewHost(); | 77 render_view_host_ = host_contents_->GetRenderViewHost(); |
78 | 78 |
79 // Listen for when an extension is unloaded from the same profile, as it may | 79 // Listen for when an extension is unloaded from the same profile, as it may |
80 // be the same extension that this points to. | 80 // be the same extension that this points to. |
81 ExtensionRegistry::Get(browser_context_)->AddObserver(this); | 81 ExtensionRegistry::Get(browser_context_)->AddObserver(this); |
82 | 82 |
83 // Set up web contents observers and pref observers. | 83 // Set up web contents observers and pref observers. |
84 delegate_->OnExtensionHostCreated(host_contents()); | 84 delegate_->OnExtensionHostCreated(host_contents()); |
| 85 |
| 86 ExtensionWebContentsObserver::GetForWebContents(host_contents())-> |
| 87 dispatcher()->set_delegate(this); |
85 } | 88 } |
86 | 89 |
87 ExtensionHost::~ExtensionHost() { | 90 ExtensionHost::~ExtensionHost() { |
88 ExtensionRegistry::Get(browser_context_)->RemoveObserver(this); | 91 ExtensionRegistry::Get(browser_context_)->RemoveObserver(this); |
89 | 92 |
90 if (extension_host_type_ == VIEW_TYPE_EXTENSION_BACKGROUND_PAGE && | 93 if (extension_host_type_ == VIEW_TYPE_EXTENSION_BACKGROUND_PAGE && |
91 extension_ && BackgroundInfo::HasLazyBackgroundPage(extension_) && | 94 extension_ && BackgroundInfo::HasLazyBackgroundPage(extension_) && |
92 load_start_.get()) { | 95 load_start_.get()) { |
93 UMA_HISTOGRAM_LONG_TIMES("Extensions.EventPageActiveTime2", | 96 UMA_HISTOGRAM_LONG_TIMES("Extensions.EventPageActiveTime2", |
94 load_start_->Elapsed()); | 97 load_start_->Elapsed()); |
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
314 } | 317 } |
315 } | 318 } |
316 | 319 |
317 void ExtensionHost::CloseContents(WebContents* contents) { | 320 void ExtensionHost::CloseContents(WebContents* contents) { |
318 Close(); | 321 Close(); |
319 } | 322 } |
320 | 323 |
321 bool ExtensionHost::OnMessageReceived(const IPC::Message& message) { | 324 bool ExtensionHost::OnMessageReceived(const IPC::Message& message) { |
322 bool handled = true; | 325 bool handled = true; |
323 IPC_BEGIN_MESSAGE_MAP(ExtensionHost, message) | 326 IPC_BEGIN_MESSAGE_MAP(ExtensionHost, message) |
324 IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request, OnRequest) | |
325 IPC_MESSAGE_HANDLER(ExtensionHostMsg_EventAck, OnEventAck) | 327 IPC_MESSAGE_HANDLER(ExtensionHostMsg_EventAck, OnEventAck) |
326 IPC_MESSAGE_HANDLER(ExtensionHostMsg_IncrementLazyKeepaliveCount, | 328 IPC_MESSAGE_HANDLER(ExtensionHostMsg_IncrementLazyKeepaliveCount, |
327 OnIncrementLazyKeepaliveCount) | 329 OnIncrementLazyKeepaliveCount) |
328 IPC_MESSAGE_HANDLER(ExtensionHostMsg_DecrementLazyKeepaliveCount, | 330 IPC_MESSAGE_HANDLER(ExtensionHostMsg_DecrementLazyKeepaliveCount, |
329 OnDecrementLazyKeepaliveCount) | 331 OnDecrementLazyKeepaliveCount) |
330 IPC_MESSAGE_UNHANDLED(handled = false) | 332 IPC_MESSAGE_UNHANDLED(handled = false) |
331 IPC_END_MESSAGE_MAP() | 333 IPC_END_MESSAGE_MAP() |
332 return handled; | 334 return handled; |
333 } | 335 } |
334 | 336 |
335 void ExtensionHost::OnRequest(const ExtensionHostMsg_Request_Params& params) { | |
336 extension_function_dispatcher_.Dispatch(params, render_view_host()); | |
337 } | |
338 | |
339 void ExtensionHost::OnEventAck(int event_id) { | 337 void ExtensionHost::OnEventAck(int event_id) { |
340 EventRouter* router = EventRouter::Get(browser_context_); | 338 EventRouter* router = EventRouter::Get(browser_context_); |
341 if (router) | 339 if (router) |
342 router->OnEventAck(browser_context_, extension_id()); | 340 router->OnEventAck(browser_context_, extension_id()); |
343 | 341 |
344 // This should always be false since event acks are only sent by extensions | 342 // This should always be false since event acks are only sent by extensions |
345 // with lazy background pages but it doesn't hurt to be extra careful. | 343 // with lazy background pages but it doesn't hurt to be extra careful. |
346 if (!IsBackgroundPage()) { | 344 if (!IsBackgroundPage()) { |
347 NOTREACHED() << "Received EventAck from extension " << extension_id() | 345 NOTREACHED() << "Received EventAck from extension " << extension_id() |
348 << ", which does not have a lazy background page."; | 346 << ", which does not have a lazy background page."; |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
470 } | 468 } |
471 } else if (extension_host_type_ == VIEW_TYPE_EXTENSION_POPUP) { | 469 } else if (extension_host_type_ == VIEW_TYPE_EXTENSION_POPUP) { |
472 UMA_HISTOGRAM_MEDIUM_TIMES("Extensions.PopupLoadTime2", | 470 UMA_HISTOGRAM_MEDIUM_TIMES("Extensions.PopupLoadTime2", |
473 load_start_->Elapsed()); | 471 load_start_->Elapsed()); |
474 UMA_HISTOGRAM_MEDIUM_TIMES("Extensions.PopupCreateTime", | 472 UMA_HISTOGRAM_MEDIUM_TIMES("Extensions.PopupCreateTime", |
475 create_start_.Elapsed()); | 473 create_start_.Elapsed()); |
476 } | 474 } |
477 } | 475 } |
478 | 476 |
479 } // namespace extensions | 477 } // namespace extensions |
OLD | NEW |