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
3 // found in the LICENSE file.
5 #include "apps/app_window_contents.h"
7 #include "apps/ui/native_app_window.h"
8 #include "chrome/browser/chrome_notification_types.h"
9 #include "chrome/common/extensions/api/app_window.h"
10 #include "content/public/browser/browser_context.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "content/public/browser/notification_details.h"
13 #include "content/public/browser/notification_source.h"
14 #include "content/public/browser/render_process_host.h"
15 #include "content/public/browser/render_view_host.h"
16 #include "content/public/browser/resource_dispatcher_host.h"
17 #include "content/public/browser/site_instance.h"
18 #include "content/public/browser/web_contents.h"
19 #include "content/public/common/renderer_preferences.h"
20 #include "extensions/common/extension_messages.h"
22 namespace app_window
= extensions::api::app_window
;
26 AppWindowContentsImpl::AppWindowContentsImpl(AppWindow
* host
) : host_(host
) {}
28 AppWindowContentsImpl::~AppWindowContentsImpl() {}
30 void AppWindowContentsImpl::Initialize(content::BrowserContext
* context
,
34 extension_function_dispatcher_
.reset(
35 new extensions::ExtensionFunctionDispatcher(context
, this));
38 content::WebContents::Create(content::WebContents::CreateParams(
39 context
, content::SiteInstance::CreateForURL(context
, url_
))));
41 content::WebContentsObserver::Observe(web_contents_
.get());
42 web_contents_
->GetMutableRendererPrefs()->
43 browser_handles_all_top_level_requests
= true;
44 web_contents_
->GetRenderViewHost()->SyncRendererPrefs();
47 void AppWindowContentsImpl::LoadContents(int32 creator_process_id
) {
48 // If the new view is in the same process as the creator, block the created
49 // RVH from loading anything until the background page has had a chance to
50 // do any initialization it wants. If it's a different process, the new RVH
51 // shouldn't communicate with the background page anyway (e.g. sandboxed).
52 if (web_contents_
->GetRenderViewHost()->GetProcess()->GetID() ==
54 SuspendRenderViewHost(web_contents_
->GetRenderViewHost());
56 VLOG(1) << "AppWindow created in new process ("
57 << web_contents_
->GetRenderViewHost()->GetProcess()->GetID()
58 << ") != creator (" << creator_process_id
<< "). Routing disabled.";
61 // TODO(jeremya): there's a bug where navigating a web contents to an
62 // extension URL causes it to create a new RVH and discard the old (perfectly
63 // usable) one. To work around this, we watch for a
64 // NOTIFICATION_RENDER_VIEW_HOST_CHANGED message from the web contents (which
65 // will be sent during LoadURL) and suspend resource requests on the new RVH
66 // to ensure that we block the new RVH from loading anything. It should be
67 // okay to remove the NOTIFICATION_RENDER_VIEW_HOST_CHANGED registration once
68 // http://crbug.com/123007 is fixed.
69 registrar_
.Add(this, content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED
,
70 content::Source
<content::WebContents
>(web_contents()));
71 web_contents_
->GetController().LoadURL(
72 url_
, content::Referrer(), content::PAGE_TRANSITION_LINK
,
74 registrar_
.RemoveAll();
77 void AppWindowContentsImpl::NativeWindowChanged(
78 NativeAppWindow
* native_app_window
) {
80 base::DictionaryValue
* dictionary
= new base::DictionaryValue();
81 args
.Append(dictionary
);
82 host_
->GetSerializedState(dictionary
);
84 content::RenderViewHost
* rvh
= web_contents_
->GetRenderViewHost();
85 rvh
->Send(new ExtensionMsg_MessageInvoke(rvh
->GetRoutingID(),
86 host_
->extension_id(),
88 "updateAppWindowProperties",
93 void AppWindowContentsImpl::NativeWindowClosed() {
94 content::RenderViewHost
* rvh
= web_contents_
->GetRenderViewHost();
95 rvh
->Send(new ExtensionMsg_AppWindowClosed(rvh
->GetRoutingID()));
98 void AppWindowContentsImpl::DispatchWindowShownForTests() const {
100 content::RenderViewHost
* rvh
= web_contents_
->GetRenderViewHost();
101 rvh
->Send(new ExtensionMsg_MessageInvoke(rvh
->GetRoutingID(),
102 host_
->extension_id(),
104 "appWindowShownForTests",
109 content::WebContents
* AppWindowContentsImpl::GetWebContents() const {
110 return web_contents_
.get();
113 void AppWindowContentsImpl::Observe(
115 const content::NotificationSource
& source
,
116 const content::NotificationDetails
& details
) {
118 case content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED
: {
119 // TODO(jeremya): once http://crbug.com/123007 is fixed, we'll no longer
120 // need to suspend resource requests here (the call in the constructor
121 // should be enough).
122 content::Details
<std::pair
<content::RenderViewHost
*,
123 content::RenderViewHost
*> >
124 host_details(details
);
125 if (host_details
->first
)
126 SuspendRenderViewHost(host_details
->second
);
130 NOTREACHED() << "Received unexpected notification";
134 bool AppWindowContentsImpl::OnMessageReceived(const IPC::Message
& message
) {
136 IPC_BEGIN_MESSAGE_MAP(AppWindowContentsImpl
, message
)
137 IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request
, OnRequest
)
138 IPC_MESSAGE_HANDLER(ExtensionHostMsg_UpdateDraggableRegions
,
139 UpdateDraggableRegions
)
140 IPC_MESSAGE_UNHANDLED(handled
= false)
141 IPC_END_MESSAGE_MAP()
145 extensions::WindowController
*
146 AppWindowContentsImpl::GetExtensionWindowController() const {
150 content::WebContents
* AppWindowContentsImpl::GetAssociatedWebContents() const {
151 return web_contents_
.get();
154 void AppWindowContentsImpl::OnRequest(
155 const ExtensionHostMsg_Request_Params
& params
) {
156 extension_function_dispatcher_
->Dispatch(
157 params
, web_contents_
->GetRenderViewHost());
160 void AppWindowContentsImpl::UpdateDraggableRegions(
161 const std::vector
<extensions::DraggableRegion
>& regions
) {
162 host_
->UpdateDraggableRegions(regions
);
165 void AppWindowContentsImpl::SuspendRenderViewHost(
166 content::RenderViewHost
* rvh
) {
168 content::BrowserThread::PostTask(
169 content::BrowserThread::IO
, FROM_HERE
,
170 base::Bind(&content::ResourceDispatcherHost::BlockRequestsForRoute
,
171 base::Unretained(content::ResourceDispatcherHost::Get()),
172 rvh
->GetProcess()->GetID(), rvh
->GetRoutingID()));