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/browser/extensions/extension_host.h" | 5 #include "chrome/browser/extensions/extension_host.h" |
6 | 6 |
7 #include <list> | 7 #include <list> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/memory/singleton.h" | 10 #include "base/memory/singleton.h" |
11 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
12 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
13 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
14 #include "base/string_util.h" | 14 #include "base/string_util.h" |
15 #include "base/utf_string_conversions.h" | 15 #include "base/utf_string_conversions.h" |
16 #include "chrome/browser/browser_shutdown.h" | 16 #include "chrome/browser/browser_shutdown.h" |
17 #include "chrome/browser/extensions/event_router.h" | 17 #include "chrome/browser/extensions/event_router.h" |
18 #include "chrome/browser/extensions/extension_process_manager.h" | 18 #include "chrome/browser/extensions/extension_process_manager.h" |
19 #include "chrome/browser/extensions/extension_service.h" | 19 #include "chrome/browser/extensions/extension_service.h" |
20 #include "chrome/browser/extensions/extension_system.h" | 20 #include "chrome/browser/extensions/extension_system.h" |
21 #include "chrome/browser/extensions/extension_tab_util.h" | 21 #include "chrome/browser/extensions/extension_tab_util.h" |
22 #include "chrome/browser/extensions/window_controller.h" | 22 #include "chrome/browser/extensions/window_controller.h" |
23 #include "chrome/browser/file_select_helper.h" | 23 #include "chrome/browser/file_select_helper.h" |
| 24 #include "chrome/browser/lifetime/application_lifetime.h" |
24 #include "chrome/browser/profiles/profile.h" | 25 #include "chrome/browser/profiles/profile.h" |
25 #include "chrome/browser/ui/app_modal_dialogs/javascript_dialog_creator.h" | 26 #include "chrome/browser/ui/app_modal_dialogs/javascript_dialog_creator.h" |
26 #include "chrome/browser/ui/browser.h" | 27 #include "chrome/browser/ui/browser.h" |
27 #include "chrome/browser/ui/browser_finder.h" | 28 #include "chrome/browser/ui/browser_finder.h" |
28 #include "chrome/browser/ui/browser_list.h" | 29 #include "chrome/browser/ui/browser_list.h" |
29 #include "chrome/browser/ui/browser_window.h" | 30 #include "chrome/browser/ui/browser_window.h" |
30 #include "chrome/browser/ui/prefs/prefs_tab_helper.h" | 31 #include "chrome/browser/ui/prefs/prefs_tab_helper.h" |
31 #include "chrome/browser/ui/tab_contents/tab_contents.h" | 32 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
32 #include "chrome/browser/view_type_utils.h" | 33 #include "chrome/browser/view_type_utils.h" |
33 #include "chrome/common/chrome_constants.h" | 34 #include "chrome/common/chrome_constants.h" |
(...skipping 23 matching lines...) Expand all Loading... |
57 using WebKit::WebDragOperation; | 58 using WebKit::WebDragOperation; |
58 using WebKit::WebDragOperationsMask; | 59 using WebKit::WebDragOperationsMask; |
59 using content::NativeWebKeyboardEvent; | 60 using content::NativeWebKeyboardEvent; |
60 using content::OpenURLParams; | 61 using content::OpenURLParams; |
61 using content::RenderViewHost; | 62 using content::RenderViewHost; |
62 using content::SiteInstance; | 63 using content::SiteInstance; |
63 using content::WebContents; | 64 using content::WebContents; |
64 | 65 |
65 namespace extensions { | 66 namespace extensions { |
66 | 67 |
| 68 bool ExtensionHost::Delegate::KeepsBrowserProcessAlive() { |
| 69 // By default hosts do not keep the browser process alive. |
| 70 return false; |
| 71 } |
| 72 |
67 // Helper class that rate-limits the creation of renderer processes for | 73 // Helper class that rate-limits the creation of renderer processes for |
68 // ExtensionHosts, to avoid blocking the UI. | 74 // ExtensionHosts, to avoid blocking the UI. |
69 class ExtensionHost::ProcessCreationQueue { | 75 class ExtensionHost::ProcessCreationQueue { |
70 public: | 76 public: |
71 static ProcessCreationQueue* GetInstance() { | 77 static ProcessCreationQueue* GetInstance() { |
72 return Singleton<ProcessCreationQueue>::get(); | 78 return Singleton<ProcessCreationQueue>::get(); |
73 } | 79 } |
74 | 80 |
75 // Add a host to the queue for RenderView creation. | 81 // Add a host to the queue for RenderView creation. |
76 void CreateSoon(ExtensionHost* host) { | 82 void CreateSoon(ExtensionHost* host) { |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 typedef std::list<ExtensionHost*> Queue; | 124 typedef std::list<ExtensionHost*> Queue; |
119 Queue queue_; | 125 Queue queue_; |
120 bool pending_create_; | 126 bool pending_create_; |
121 base::WeakPtrFactory<ProcessCreationQueue> ptr_factory_; | 127 base::WeakPtrFactory<ProcessCreationQueue> ptr_factory_; |
122 }; | 128 }; |
123 | 129 |
124 //////////////// | 130 //////////////// |
125 // ExtensionHost | 131 // ExtensionHost |
126 | 132 |
127 ExtensionHost::ExtensionHost(const Extension* extension, | 133 ExtensionHost::ExtensionHost(const Extension* extension, |
| 134 Delegate* delegate, |
128 SiteInstance* site_instance, | 135 SiteInstance* site_instance, |
129 const GURL& url, | 136 const GURL& url, |
130 chrome::ViewType host_type) | 137 chrome::ViewType host_type) |
131 : extension_(extension), | 138 : extension_(extension), |
132 extension_id_(extension->id()), | 139 extension_id_(extension->id()), |
133 profile_(Profile::FromBrowserContext( | 140 profile_(Profile::FromBrowserContext( |
134 site_instance->GetBrowserContext())), | 141 site_instance->GetBrowserContext())), |
| 142 delegate_(delegate), |
135 render_view_host_(NULL), | 143 render_view_host_(NULL), |
136 did_stop_loading_(false), | 144 did_stop_loading_(false), |
137 document_element_available_(false), | 145 document_element_available_(false), |
138 initial_url_(url), | 146 initial_url_(url), |
139 ALLOW_THIS_IN_INITIALIZER_LIST( | 147 ALLOW_THIS_IN_INITIALIZER_LIST( |
140 extension_function_dispatcher_(profile_, this)), | 148 extension_function_dispatcher_(profile_, this)), |
141 extension_host_type_(host_type), | 149 extension_host_type_(host_type), |
142 associated_web_contents_(NULL) { | 150 associated_web_contents_(NULL) { |
143 host_contents_.reset(WebContents::Create( | 151 host_contents_.reset(WebContents::Create( |
144 profile_, site_instance, MSG_ROUTING_NONE, NULL)); | 152 profile_, site_instance, MSG_ROUTING_NONE, NULL)); |
145 content::WebContentsObserver::Observe(host_contents_.get()); | 153 content::WebContentsObserver::Observe(host_contents_.get()); |
146 host_contents_->SetDelegate(this); | 154 host_contents_->SetDelegate(this); |
147 chrome::SetViewType(host_contents_.get(), host_type); | 155 chrome::SetViewType(host_contents_.get(), host_type); |
148 | 156 |
149 PrefsTabHelper::CreateForWebContents(host_contents()); | 157 PrefsTabHelper::CreateForWebContents(host_contents()); |
150 | 158 |
151 render_view_host_ = host_contents_->GetRenderViewHost(); | 159 render_view_host_ = host_contents_->GetRenderViewHost(); |
152 | 160 |
153 // Listen for when an extension is unloaded from the same profile, as it may | 161 // Listen for when an extension is unloaded from the same profile, as it may |
154 // be the same extension that this points to. | 162 // be the same extension that this points to. |
155 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, | 163 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
156 content::Source<Profile>(profile_)); | 164 content::Source<Profile>(profile_)); |
| 165 |
| 166 if (delegate_->KeepsBrowserProcessAlive()) |
| 167 browser::StartKeepAlive(); |
157 } | 168 } |
158 | 169 |
159 ExtensionHost::~ExtensionHost() { | 170 ExtensionHost::~ExtensionHost() { |
160 if (extension_host_type_ == chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE && | 171 if (extension_host_type_ == chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE && |
161 extension_ && extension_->has_lazy_background_page()) { | 172 extension_ && extension_->has_lazy_background_page()) { |
162 UMA_HISTOGRAM_LONG_TIMES("Extensions.EventPageActiveTime", | 173 UMA_HISTOGRAM_LONG_TIMES("Extensions.EventPageActiveTime", |
163 since_created_.Elapsed()); | 174 since_created_.Elapsed()); |
164 } | 175 } |
165 content::NotificationService::current()->Notify( | 176 content::NotificationService::current()->Notify( |
166 chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, | 177 chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, |
167 content::Source<Profile>(profile_), | 178 content::Source<Profile>(profile_), |
168 content::Details<ExtensionHost>(this)); | 179 content::Details<ExtensionHost>(this)); |
169 ProcessCreationQueue::GetInstance()->Remove(this); | 180 ProcessCreationQueue::GetInstance()->Remove(this); |
| 181 |
| 182 if (delegate_->KeepsBrowserProcessAlive()) |
| 183 browser::EndKeepAlive(); |
170 } | 184 } |
171 | 185 |
172 void ExtensionHost::CreateView(Browser* browser) { | 186 void ExtensionHost::CreateView(Browser* browser) { |
173 #if defined(TOOLKIT_VIEWS) | 187 #if defined(TOOLKIT_VIEWS) |
174 view_.reset(new ExtensionViewViews(this, browser)); | 188 view_.reset(new ExtensionViewViews(this, browser)); |
175 // We own |view_|, so don't auto delete when it's removed from the view | 189 // We own |view_|, so don't auto delete when it's removed from the view |
176 // hierarchy. | 190 // hierarchy. |
177 view_->set_owned_by_client(); | 191 view_->set_owned_by_client(); |
178 #elif defined(OS_MACOSX) | 192 #elif defined(OS_MACOSX) |
179 view_.reset(new ExtensionViewMac(this, browser)); | 193 view_.reset(new ExtensionViewMac(this, browser)); |
(...skipping 454 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
634 } | 648 } |
635 | 649 |
636 void ExtensionHost::RenderViewReady() { | 650 void ExtensionHost::RenderViewReady() { |
637 content::NotificationService::current()->Notify( | 651 content::NotificationService::current()->Notify( |
638 chrome::NOTIFICATION_EXTENSION_HOST_CREATED, | 652 chrome::NOTIFICATION_EXTENSION_HOST_CREATED, |
639 content::Source<Profile>(profile_), | 653 content::Source<Profile>(profile_), |
640 content::Details<ExtensionHost>(this)); | 654 content::Details<ExtensionHost>(this)); |
641 } | 655 } |
642 | 656 |
643 } // namespace extensions | 657 } // namespace extensions |
OLD | NEW |