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 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 content::WebContents
* AppWindowContentsImpl::GetWebContents() const {
99 return web_contents_
.get();
102 void AppWindowContentsImpl::Observe(
104 const content::NotificationSource
& source
,
105 const content::NotificationDetails
& details
) {
107 case content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED
: {
108 // TODO(jeremya): once http://crbug.com/123007 is fixed, we'll no longer
109 // need to suspend resource requests here (the call in the constructor
110 // should be enough).
111 content::Details
<std::pair
<content::RenderViewHost
*,
112 content::RenderViewHost
*> >
113 host_details(details
);
114 if (host_details
->first
)
115 SuspendRenderViewHost(host_details
->second
);
119 NOTREACHED() << "Received unexpected notification";
123 bool AppWindowContentsImpl::OnMessageReceived(const IPC::Message
& message
) {
125 IPC_BEGIN_MESSAGE_MAP(AppWindowContentsImpl
, message
)
126 IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request
, OnRequest
)
127 IPC_MESSAGE_HANDLER(ExtensionHostMsg_UpdateDraggableRegions
,
128 UpdateDraggableRegions
)
129 IPC_MESSAGE_UNHANDLED(handled
= false)
130 IPC_END_MESSAGE_MAP()
134 extensions::WindowController
*
135 AppWindowContentsImpl::GetExtensionWindowController() const {
139 content::WebContents
* AppWindowContentsImpl::GetAssociatedWebContents() const {
140 return web_contents_
.get();
143 void AppWindowContentsImpl::OnRequest(
144 const ExtensionHostMsg_Request_Params
& params
) {
145 extension_function_dispatcher_
->Dispatch(
146 params
, web_contents_
->GetRenderViewHost());
149 void AppWindowContentsImpl::UpdateDraggableRegions(
150 const std::vector
<extensions::DraggableRegion
>& regions
) {
151 host_
->UpdateDraggableRegions(regions
);
154 void AppWindowContentsImpl::SuspendRenderViewHost(
155 content::RenderViewHost
* rvh
) {
157 content::BrowserThread::PostTask(
158 content::BrowserThread::IO
, FROM_HERE
,
159 base::Bind(&content::ResourceDispatcherHost::BlockRequestsForRoute
,
160 base::Unretained(content::ResourceDispatcherHost::Get()),
161 rvh
->GetProcess()->GetID(), rvh
->GetRoutingID()));