OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/tab_contents/background_contents.h" | |
6 | |
7 #include "chrome/browser/background_contents_service.h" | |
8 #include "chrome/browser/browsing_instance.h" | |
9 #include "chrome/browser/desktop_notification_handler.h" | |
10 #include "chrome/browser/profiles/profile.h" | |
11 #include "chrome/browser/renderer_host/render_view_host.h" | |
12 #include "chrome/browser/renderer_host/site_instance.h" | |
13 #include "chrome/browser/renderer_preferences_util.h" | |
14 #include "chrome/common/extensions/extension_constants.h" | |
15 #include "chrome/common/notification_service.h" | |
16 #include "chrome/common/url_constants.h" | |
17 #include "chrome/common/view_types.h" | |
18 #include "chrome/common/render_messages_params.h" | |
19 #include "ui/gfx/rect.h" | |
20 | |
21 //////////////// | |
22 // BackgroundContents | |
23 | |
24 BackgroundContents::BackgroundContents(SiteInstance* site_instance, | |
25 int routing_id, | |
26 Delegate* delegate) | |
27 : delegate_(delegate) { | |
28 Profile* profile = site_instance->browsing_instance()->profile(); | |
29 | |
30 // TODO(rafaelw): Implement correct session storage. | |
31 render_view_host_ = new RenderViewHost(site_instance, this, routing_id, NULL); | |
32 render_view_host_->AllowScriptToClose(true); | |
33 | |
34 // Close ourselves when the application is shutting down. | |
35 registrar_.Add(this, NotificationType::APP_TERMINATING, | |
36 NotificationService::AllSources()); | |
37 | |
38 // Register for our parent profile to shutdown, so we can shut ourselves down | |
39 // as well (should only be called for OTR profiles, as we should receive | |
40 // APP_TERMINATING before non-OTR profiles are destroyed). | |
41 registrar_.Add(this, NotificationType::PROFILE_DESTROYED, | |
42 Source<Profile>(profile)); | |
43 desktop_notification_handler_.reset( | |
44 new DesktopNotificationHandler(NULL, site_instance->GetProcess())); | |
45 } | |
46 | |
47 // Exposed to allow creating mocks. | |
48 BackgroundContents::BackgroundContents() | |
49 : delegate_(NULL), | |
50 render_view_host_(NULL) { | |
51 } | |
52 | |
53 BackgroundContents::~BackgroundContents() { | |
54 if (!render_view_host_) // Will be null for unit tests. | |
55 return; | |
56 Profile* profile = render_view_host_->process()->profile(); | |
57 NotificationService::current()->Notify( | |
58 NotificationType::BACKGROUND_CONTENTS_DELETED, | |
59 Source<Profile>(profile), | |
60 Details<BackgroundContents>(this)); | |
61 render_view_host_->Shutdown(); // deletes render_view_host | |
62 } | |
63 | |
64 BackgroundContents* BackgroundContents::GetAsBackgroundContents() { | |
65 return this; | |
66 } | |
67 | |
68 RenderViewHostDelegate::View* BackgroundContents::GetViewDelegate() { | |
69 return this; | |
70 } | |
71 | |
72 const GURL& BackgroundContents::GetURL() const { | |
73 return url_; | |
74 } | |
75 | |
76 ViewType::Type BackgroundContents::GetRenderViewType() const { | |
77 return ViewType::BACKGROUND_CONTENTS; | |
78 } | |
79 | |
80 int BackgroundContents::GetBrowserWindowID() const { | |
81 return extension_misc::kUnknownWindowId; | |
82 } | |
83 | |
84 void BackgroundContents::DidNavigate( | |
85 RenderViewHost* render_view_host, | |
86 const ViewHostMsg_FrameNavigate_Params& params) { | |
87 // We only care when the outer frame changes. | |
88 if (!PageTransition::IsMainFrame(params.transition)) | |
89 return; | |
90 | |
91 // Note: because BackgroundContents are only available to extension apps, | |
92 // navigation is limited to urls within the app's extent. This is enforced in | |
93 // RenderView::decidePolicyForNaviation. If BackgroundContents become | |
94 // available as a part of the web platform, it probably makes sense to have | |
95 // some way to scope navigation of a background page to its opener's security | |
96 // origin. Note: if the first navigation is to a URL outside the app's | |
97 // extent a background page will be opened but will remain at about:blank. | |
98 url_ = params.url; | |
99 | |
100 Profile* profile = render_view_host->process()->profile(); | |
101 NotificationService::current()->Notify( | |
102 NotificationType::BACKGROUND_CONTENTS_NAVIGATED, | |
103 Source<Profile>(profile), | |
104 Details<BackgroundContents>(this)); | |
105 } | |
106 | |
107 void BackgroundContents::RunJavaScriptMessage( | |
108 const std::wstring& message, | |
109 const std::wstring& default_prompt, | |
110 const GURL& frame_url, | |
111 const int flags, | |
112 IPC::Message* reply_msg, | |
113 bool* did_suppress_message) { | |
114 // TODO(rafaelw): Implement, The JavaScriptModalDialog needs to learn about | |
115 // BackgroundContents. | |
116 *did_suppress_message = true; | |
117 } | |
118 | |
119 bool BackgroundContents::PreHandleKeyboardEvent( | |
120 const NativeWebKeyboardEvent& event, | |
121 bool* is_keyboard_shortcut) { | |
122 return false; | |
123 } | |
124 | |
125 void BackgroundContents::Observe(NotificationType type, | |
126 const NotificationSource& source, | |
127 const NotificationDetails& details) { | |
128 // TODO(rafaelw): Implement pagegroup ref-counting so that non-persistent | |
129 // background pages are closed when the last referencing frame is closed. | |
130 switch (type.value) { | |
131 case NotificationType::PROFILE_DESTROYED: | |
132 case NotificationType::APP_TERMINATING: { | |
133 delete this; | |
134 break; | |
135 } | |
136 default: | |
137 NOTREACHED() << "Unexpected notification sent."; | |
138 break; | |
139 } | |
140 } | |
141 | |
142 void BackgroundContents::OnMessageBoxClosed(IPC::Message* reply_msg, | |
143 bool success, | |
144 const std::wstring& prompt) { | |
145 render_view_host_->JavaScriptMessageBoxClosed(reply_msg, success, prompt); | |
146 } | |
147 | |
148 gfx::NativeWindow BackgroundContents::GetMessageBoxRootWindow() { | |
149 NOTIMPLEMENTED(); | |
150 return NULL; | |
151 } | |
152 | |
153 TabContents* BackgroundContents::AsTabContents() { | |
154 return NULL; | |
155 } | |
156 | |
157 ExtensionHost* BackgroundContents::AsExtensionHost() { | |
158 return NULL; | |
159 } | |
160 | |
161 void BackgroundContents::UpdateInspectorSetting(const std::string& key, | |
162 const std::string& value) { | |
163 Profile* profile = render_view_host_->process()->profile(); | |
164 RenderViewHostDelegateHelper::UpdateInspectorSetting(profile, key, value); | |
165 } | |
166 | |
167 void BackgroundContents::ClearInspectorSettings() { | |
168 Profile* profile = render_view_host_->process()->profile(); | |
169 RenderViewHostDelegateHelper::ClearInspectorSettings(profile); | |
170 } | |
171 | |
172 void BackgroundContents::Close(RenderViewHost* render_view_host) { | |
173 Profile* profile = render_view_host->process()->profile(); | |
174 NotificationService::current()->Notify( | |
175 NotificationType::BACKGROUND_CONTENTS_CLOSED, | |
176 Source<Profile>(profile), | |
177 Details<BackgroundContents>(this)); | |
178 delete this; | |
179 } | |
180 | |
181 void BackgroundContents::RenderViewGone(RenderViewHost* rvh, | |
182 base::TerminationStatus status, | |
183 int error_code) { | |
184 // Our RenderView went away, so we should go away also, so killing the process | |
185 // via the TaskManager doesn't permanently leave a BackgroundContents hanging | |
186 // around the system, blocking future instances from being created | |
187 // (http://crbug.com/65189). | |
188 delete this; | |
189 } | |
190 | |
191 bool BackgroundContents::OnMessageReceived(const IPC::Message& message) { | |
192 // Forward desktop notification IPCs if any to the | |
193 // DesktopNotificationHandler. | |
194 return desktop_notification_handler_->OnMessageReceived(message); | |
195 } | |
196 | |
197 RendererPreferences BackgroundContents::GetRendererPrefs( | |
198 Profile* profile) const { | |
199 RendererPreferences preferences; | |
200 renderer_preferences_util::UpdateFromSystemSettings(&preferences, profile); | |
201 return preferences; | |
202 } | |
203 | |
204 WebPreferences BackgroundContents::GetWebkitPrefs() { | |
205 // TODO(rafaelw): Consider enabling the webkit_prefs.dom_paste_enabled for | |
206 // apps. | |
207 Profile* profile = render_view_host_->process()->profile(); | |
208 return RenderViewHostDelegateHelper::GetWebkitPrefs(profile, | |
209 false); // is_dom_ui | |
210 } | |
211 | |
212 void BackgroundContents::ProcessWebUIMessage( | |
213 const ViewHostMsg_DomMessage_Params& params) { | |
214 // TODO(rafaelw): It may make sense for extensions to be able to open | |
215 // BackgroundContents to chrome-extension://<id> pages. Consider implementing. | |
216 render_view_host_->BlockExtensionRequest(params.request_id); | |
217 } | |
218 | |
219 void BackgroundContents::CreateNewWindow( | |
220 int route_id, | |
221 const ViewHostMsg_CreateWindow_Params& params) { | |
222 delegate_view_helper_.CreateNewWindow( | |
223 route_id, | |
224 render_view_host_->process()->profile(), | |
225 render_view_host_->site_instance(), | |
226 WebUIFactory::GetWebUIType(render_view_host_->process()->profile(), url_), | |
227 this, | |
228 params.window_container_type, | |
229 params.frame_name); | |
230 } | |
231 | |
232 void BackgroundContents::CreateNewWidget(int route_id, | |
233 WebKit::WebPopupType popup_type) { | |
234 NOTREACHED(); | |
235 } | |
236 | |
237 void BackgroundContents::CreateNewFullscreenWidget(int route_id) { | |
238 NOTREACHED(); | |
239 } | |
240 | |
241 void BackgroundContents::ShowCreatedWindow(int route_id, | |
242 WindowOpenDisposition disposition, | |
243 const gfx::Rect& initial_pos, | |
244 bool user_gesture) { | |
245 TabContents* contents = delegate_view_helper_.GetCreatedWindow(route_id); | |
246 if (contents) | |
247 delegate_->AddTabContents(contents, disposition, initial_pos, user_gesture); | |
248 } | |
249 | |
250 void BackgroundContents::ShowCreatedWidget(int route_id, | |
251 const gfx::Rect& initial_pos) { | |
252 NOTIMPLEMENTED(); | |
253 } | |
254 | |
255 void BackgroundContents::ShowCreatedFullscreenWidget(int route_id) { | |
256 NOTIMPLEMENTED(); | |
257 } | |
258 | |
259 // static | |
260 BackgroundContents* | |
261 BackgroundContents::GetBackgroundContentsByID(int render_process_id, | |
262 int render_view_id) { | |
263 RenderViewHost* render_view_host = | |
264 RenderViewHost::FromID(render_process_id, render_view_id); | |
265 if (!render_view_host) | |
266 return NULL; | |
267 | |
268 return render_view_host->delegate()->GetAsBackgroundContents(); | |
269 } | |
OLD | NEW |