OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/tab_contents/background_contents.h" | 5 #include "chrome/browser/tab_contents/background_contents.h" |
6 | 6 |
7 #include "chrome/browser/background/background_contents_service.h" | 7 #include "chrome/browser/background/background_contents_service.h" |
8 #include "chrome/browser/extensions/extension_message_service.h" | 8 #include "chrome/browser/extensions/extension_message_service.h" |
9 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
10 #include "chrome/browser/renderer_preferences_util.h" | 10 #include "chrome/browser/renderer_preferences_util.h" |
11 #include "chrome/browser/ui/webui/chrome_web_ui_factory.h" | 11 #include "chrome/browser/ui/webui/chrome_web_ui_factory.h" |
12 #include "chrome/common/chrome_notification_types.h" | 12 #include "chrome/common/chrome_notification_types.h" |
13 #include "chrome/common/extensions/extension_constants.h" | 13 #include "chrome/common/extensions/extension_constants.h" |
14 #include "chrome/common/url_constants.h" | 14 #include "chrome/common/url_constants.h" |
15 #include "chrome/common/chrome_view_types.h" | 15 #include "chrome/common/chrome_view_types.h" |
16 #include "content/browser/browsing_instance.h" | 16 #include "content/browser/browsing_instance.h" |
17 #include "content/browser/renderer_host/render_view_host.h" | 17 #include "content/browser/renderer_host/render_view_host.h" |
18 #include "content/browser/site_instance.h" | 18 #include "content/browser/site_instance.h" |
| 19 #include "content/browser/tab_contents/tab_contents.h" |
19 #include "content/public/browser/notification_service.h" | 20 #include "content/public/browser/notification_service.h" |
20 #include "content/common/view_messages.h" | 21 #include "content/common/view_messages.h" |
21 #include "ui/gfx/rect.h" | 22 #include "ui/gfx/rect.h" |
22 | 23 |
23 //////////////// | 24 //////////////// |
24 // BackgroundContents | 25 // BackgroundContents |
25 | 26 |
26 BackgroundContents::BackgroundContents(SiteInstance* site_instance, | 27 BackgroundContents::BackgroundContents(SiteInstance* site_instance, |
27 int routing_id, | 28 int routing_id, |
28 Delegate* delegate) | 29 Delegate* delegate) |
29 : delegate_(delegate) { | 30 : delegate_(delegate) { |
30 Profile* profile = Profile::FromBrowserContext( | 31 profile_ = Profile::FromBrowserContext( |
31 site_instance->browsing_instance()->browser_context()); | 32 site_instance->browsing_instance()->browser_context()); |
32 | 33 |
33 // TODO(rafaelw): Implement correct session storage. | 34 // TODO(rafaelw): Implement correct session storage. |
34 render_view_host_ = new RenderViewHost(site_instance, this, routing_id, NULL); | 35 tab_contents_.reset(new TabContents( |
| 36 profile_, site_instance, routing_id, NULL, NULL)); |
| 37 tab_contents_->set_view_type(chrome::VIEW_TYPE_BACKGROUND_CONTENTS); |
| 38 tab_contents_->set_delegate(this); |
| 39 TabContentsObserver::Observe(tab_contents_.get()); |
35 | 40 |
36 // Close ourselves when the application is shutting down. | 41 // Close ourselves when the application is shutting down. |
37 registrar_.Add(this, content::NOTIFICATION_APP_TERMINATING, | 42 registrar_.Add(this, content::NOTIFICATION_APP_TERMINATING, |
38 content::NotificationService::AllSources()); | 43 content::NotificationService::AllSources()); |
39 | 44 |
40 // Register for our parent profile to shutdown, so we can shut ourselves down | 45 // Register for our parent profile to shutdown, so we can shut ourselves down |
41 // as well (should only be called for OTR profiles, as we should receive | 46 // as well (should only be called for OTR profiles, as we should receive |
42 // APP_TERMINATING before non-OTR profiles are destroyed). | 47 // APP_TERMINATING before non-OTR profiles are destroyed). |
43 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED, | 48 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED, |
44 content::Source<Profile>(profile)); | 49 content::Source<Profile>(profile_)); |
45 } | 50 } |
46 | 51 |
47 // Exposed to allow creating mocks. | 52 // Exposed to allow creating mocks. |
48 BackgroundContents::BackgroundContents() | 53 BackgroundContents::BackgroundContents() |
49 : delegate_(NULL), | 54 : delegate_(NULL), |
50 render_view_host_(NULL) { | 55 profile_(NULL) { |
51 } | 56 } |
52 | 57 |
53 BackgroundContents::~BackgroundContents() { | 58 BackgroundContents::~BackgroundContents() { |
54 if (!render_view_host_) // Will be null for unit tests. | 59 if (!tab_contents_.get()) // Will be null for unit tests. |
55 return; | 60 return; |
56 Profile* profile = Profile::FromBrowserContext( | |
57 render_view_host_->process()->browser_context()); | |
58 content::NotificationService::current()->Notify( | 61 content::NotificationService::current()->Notify( |
59 chrome::NOTIFICATION_BACKGROUND_CONTENTS_DELETED, | 62 chrome::NOTIFICATION_BACKGROUND_CONTENTS_DELETED, |
60 content::Source<Profile>(profile), | 63 content::Source<Profile>(profile_), |
61 content::Details<BackgroundContents>(this)); | 64 content::Details<BackgroundContents>(this)); |
62 render_view_host_->Shutdown(); // deletes render_view_host | |
63 } | |
64 | |
65 BackgroundContents* BackgroundContents::GetAsBackgroundContents() { | |
66 return this; | |
67 } | |
68 | |
69 RenderViewHostDelegate::View* BackgroundContents::GetViewDelegate() { | |
70 return this; | |
71 } | 65 } |
72 | 66 |
73 const GURL& BackgroundContents::GetURL() const { | 67 const GURL& BackgroundContents::GetURL() const { |
74 return url_; | 68 return tab_contents_.get() ? tab_contents_->GetURL() : GURL::EmptyGURL(); |
75 } | 69 } |
76 | 70 |
77 content::ViewType BackgroundContents::GetRenderViewType() const { | 71 void BackgroundContents::CloseContents(TabContents* source) { |
78 return chrome::VIEW_TYPE_BACKGROUND_CONTENTS; | 72 content::NotificationService::current()->Notify( |
| 73 chrome::NOTIFICATION_BACKGROUND_CONTENTS_CLOSED, |
| 74 content::Source<Profile>(profile_), |
| 75 content::Details<BackgroundContents>(this)); |
| 76 delete this; |
79 } | 77 } |
80 | 78 |
81 void BackgroundContents::DidNavigate( | 79 bool BackgroundContents::ShouldSuppressDialogs() { |
82 RenderViewHost* render_view_host, | 80 return true; |
83 const ViewHostMsg_FrameNavigate_Params& params) { | 81 } |
84 // We only care when the outer frame changes. | |
85 if (!content::PageTransitionIsMainFrame(params.transition)) | |
86 return; | |
87 | 82 |
| 83 void BackgroundContents::DidNavigateMainFramePostCommit(TabContents* tab) { |
88 // Note: because BackgroundContents are only available to extension apps, | 84 // Note: because BackgroundContents are only available to extension apps, |
89 // navigation is limited to urls within the app's extent. This is enforced in | 85 // navigation is limited to urls within the app's extent. This is enforced in |
90 // RenderView::decidePolicyForNaviation. If BackgroundContents become | 86 // RenderView::decidePolicyForNavigation. If BackgroundContents become |
91 // available as a part of the web platform, it probably makes sense to have | 87 // available as a part of the web platform, it probably makes sense to have |
92 // some way to scope navigation of a background page to its opener's security | 88 // some way to scope navigation of a background page to its opener's security |
93 // origin. Note: if the first navigation is to a URL outside the app's | 89 // origin. Note: if the first navigation is to a URL outside the app's |
94 // extent a background page will be opened but will remain at about:blank. | 90 // extent a background page will be opened but will remain at about:blank. |
95 url_ = params.url; | |
96 | |
97 Profile* profile = Profile::FromBrowserContext( | |
98 render_view_host->process()->browser_context()); | |
99 content::NotificationService::current()->Notify( | 91 content::NotificationService::current()->Notify( |
100 chrome::NOTIFICATION_BACKGROUND_CONTENTS_NAVIGATED, | 92 chrome::NOTIFICATION_BACKGROUND_CONTENTS_NAVIGATED, |
101 content::Source<Profile>(profile), | 93 content::Source<Profile>(profile_), |
102 content::Details<BackgroundContents>(this)); | 94 content::Details<BackgroundContents>(this)); |
103 } | 95 } |
104 | 96 |
105 void BackgroundContents::RunJavaScriptMessage( | 97 void BackgroundContents::RenderViewGone() { |
106 const RenderViewHost* rvh, | 98 content::NotificationService::current()->Notify( |
107 const string16& message, | 99 chrome::NOTIFICATION_BACKGROUND_CONTENTS_TERMINATED, |
108 const string16& default_prompt, | 100 content::Source<Profile>(profile_), |
109 const GURL& frame_url, | 101 content::Details<BackgroundContents>(this)); |
110 const int flags, | |
111 IPC::Message* reply_msg, | |
112 bool* did_suppress_message) { | |
113 // TODO(rafaelw): Implement. | |
114 | 102 |
115 // Since we are suppressing messages, just reply as if the user immediately | 103 // Our RenderView went away, so we should go away also, so killing the process |
116 // pressed "Cancel". | 104 // via the TaskManager doesn't permanently leave a BackgroundContents hanging |
117 OnDialogClosed(reply_msg, false, string16()); | 105 // around the system, blocking future instances from being created |
118 | 106 // (http://crbug.com/65189). |
119 *did_suppress_message = true; | 107 delete this; |
120 } | 108 } |
121 | 109 |
122 void BackgroundContents::Observe(int type, | 110 void BackgroundContents::Observe(int type, |
123 const content::NotificationSource& source, | 111 const content::NotificationSource& source, |
124 const content::NotificationDetails& details) { | 112 const content::NotificationDetails& details) { |
125 // TODO(rafaelw): Implement pagegroup ref-counting so that non-persistent | 113 // TODO(rafaelw): Implement pagegroup ref-counting so that non-persistent |
126 // background pages are closed when the last referencing frame is closed. | 114 // background pages are closed when the last referencing frame is closed. |
127 switch (type) { | 115 switch (type) { |
128 case chrome::NOTIFICATION_PROFILE_DESTROYED: | 116 case chrome::NOTIFICATION_PROFILE_DESTROYED: |
129 case content::NOTIFICATION_APP_TERMINATING: { | 117 case content::NOTIFICATION_APP_TERMINATING: { |
130 delete this; | 118 delete this; |
131 break; | 119 break; |
132 } | 120 } |
133 default: | 121 default: |
134 NOTREACHED() << "Unexpected notification sent."; | 122 NOTREACHED() << "Unexpected notification sent."; |
135 break; | 123 break; |
136 } | 124 } |
137 } | 125 } |
138 | |
139 void BackgroundContents::OnDialogClosed(IPC::Message* reply_msg, | |
140 bool success, | |
141 const string16& user_input) { | |
142 render_view_host()->JavaScriptDialogClosed(reply_msg, | |
143 success, | |
144 user_input); | |
145 } | |
146 | |
147 gfx::NativeWindow BackgroundContents::GetDialogRootWindow() { | |
148 NOTIMPLEMENTED(); | |
149 return NULL; | |
150 } | |
151 | |
152 void BackgroundContents::Close(RenderViewHost* render_view_host) { | |
153 Profile* profile = Profile::FromBrowserContext( | |
154 render_view_host->process()->browser_context()); | |
155 content::NotificationService::current()->Notify( | |
156 chrome::NOTIFICATION_BACKGROUND_CONTENTS_CLOSED, | |
157 content::Source<Profile>(profile), | |
158 content::Details<BackgroundContents>(this)); | |
159 delete this; | |
160 } | |
161 | |
162 void BackgroundContents::RenderViewGone(RenderViewHost* rvh, | |
163 base::TerminationStatus status, | |
164 int error_code) { | |
165 Profile* profile = | |
166 Profile::FromBrowserContext(rvh->process()->browser_context()); | |
167 content::NotificationService::current()->Notify( | |
168 chrome::NOTIFICATION_BACKGROUND_CONTENTS_TERMINATED, | |
169 content::Source<Profile>(profile), | |
170 content::Details<BackgroundContents>(this)); | |
171 | |
172 // Our RenderView went away, so we should go away also, so killing the process | |
173 // via the TaskManager doesn't permanently leave a BackgroundContents hanging | |
174 // around the system, blocking future instances from being created | |
175 // (http://crbug.com/65189). | |
176 delete this; | |
177 } | |
178 | |
179 content::RendererPreferences BackgroundContents::GetRendererPrefs( | |
180 content::BrowserContext* browser_context) const { | |
181 Profile* profile = Profile::FromBrowserContext(browser_context); | |
182 content::RendererPreferences preferences; | |
183 renderer_preferences_util::UpdateFromSystemSettings(&preferences, profile); | |
184 return preferences; | |
185 } | |
186 | |
187 WebPreferences BackgroundContents::GetWebkitPrefs() { | |
188 WebPreferences prefs = | |
189 RenderViewHostDelegateHelper::GetWebkitPrefs(render_view_host_); | |
190 | |
191 // Disable all kinds of acceleration for background pages. | |
192 // See http://crbug.com/96005 and http://crbug.com/96006 | |
193 prefs.force_compositing_mode = false; | |
194 prefs.accelerated_compositing_enabled = false; | |
195 prefs.accelerated_2d_canvas_enabled = false; | |
196 prefs.accelerated_video_enabled = false; | |
197 prefs.accelerated_drawing_enabled = false; | |
198 prefs.accelerated_plugins_enabled = false; | |
199 | |
200 return prefs; | |
201 } | |
202 | |
203 void BackgroundContents::CreateNewWindow( | |
204 int route_id, | |
205 const ViewHostMsg_CreateWindow_Params& params) { | |
206 Profile* profile = Profile::FromBrowserContext( | |
207 render_view_host_->process()->browser_context()); | |
208 delegate_view_helper_.CreateNewWindow( | |
209 route_id, | |
210 profile, | |
211 render_view_host_->site_instance(), | |
212 ChromeWebUIFactory::GetInstance()->GetWebUIType(profile, url_), | |
213 this, | |
214 params.window_container_type, | |
215 params.frame_name); | |
216 } | |
217 | |
218 void BackgroundContents::CreateNewWidget(int route_id, | |
219 WebKit::WebPopupType popup_type) { | |
220 NOTREACHED(); | |
221 } | |
222 | |
223 void BackgroundContents::CreateNewFullscreenWidget(int route_id) { | |
224 NOTREACHED(); | |
225 } | |
226 | |
227 void BackgroundContents::ShowCreatedWindow(int route_id, | |
228 WindowOpenDisposition disposition, | |
229 const gfx::Rect& initial_pos, | |
230 bool user_gesture) { | |
231 TabContents* contents = delegate_view_helper_.GetCreatedWindow(route_id); | |
232 if (contents) | |
233 delegate_->AddTabContents(contents, disposition, initial_pos, user_gesture); | |
234 } | |
235 | |
236 void BackgroundContents::ShowCreatedWidget(int route_id, | |
237 const gfx::Rect& initial_pos) { | |
238 NOTIMPLEMENTED(); | |
239 } | |
240 | |
241 void BackgroundContents::ShowCreatedFullscreenWidget(int route_id) { | |
242 NOTIMPLEMENTED(); | |
243 } | |
244 | |
245 // static | |
246 BackgroundContents* | |
247 BackgroundContents::GetBackgroundContentsByID(int render_process_id, | |
248 int render_view_id) { | |
249 RenderViewHost* render_view_host = | |
250 RenderViewHost::FromID(render_process_id, render_view_id); | |
251 if (!render_view_host) | |
252 return NULL; | |
253 | |
254 return render_view_host->delegate()->GetAsBackgroundContents(); | |
255 } | |
OLD | NEW |