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 "chrome/common/extensions/extension_messages.h"
11 #include "content/public/browser/browser_context.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "content/public/browser/render_process_host.h"
14 #include "content/public/browser/render_view_host.h"
15 #include "content/public/browser/resource_dispatcher_host.h"
16 #include "content/public/browser/site_instance.h"
17 #include "content/public/browser/web_contents.h"
18 #include "content/public/common/renderer_preferences.h"
20 namespace app_window = extensions::api::app_window;
24 AppWindowContentsImpl::AppWindowContentsImpl(AppWindow* host) : host_(host) {}
26 AppWindowContentsImpl::~AppWindowContentsImpl() {}
28 void AppWindowContentsImpl::Initialize(content::BrowserContext* context,
32 extension_function_dispatcher_.reset(
33 new ExtensionFunctionDispatcher(context, this));
36 content::WebContents::Create(content::WebContents::CreateParams(
37 context, content::SiteInstance::CreateForURL(context, url_))));
39 content::WebContentsObserver::Observe(web_contents_.get());
40 web_contents_->GetMutableRendererPrefs()->
41 browser_handles_all_top_level_requests = true;
42 web_contents_->GetRenderViewHost()->SyncRendererPrefs();
45 void AppWindowContentsImpl::LoadContents(int32 creator_process_id) {
46 // If the new view is in the same process as the creator, block the created
47 // RVH from loading anything until the background page has had a chance to
48 // do any initialization it wants. If it's a different process, the new RVH
49 // shouldn't communicate with the background page anyway (e.g. sandboxed).
50 if (web_contents_->GetRenderViewHost()->GetProcess()->GetID() ==
52 SuspendRenderViewHost(web_contents_->GetRenderViewHost());
54 VLOG(1) << "AppWindow created in new process ("
55 << web_contents_->GetRenderViewHost()->GetProcess()->GetID()
56 << ") != creator (" << creator_process_id << "). Routing disabled.";
59 // TODO(jeremya): there's a bug where navigating a web contents to an
60 // extension URL causes it to create a new RVH and discard the old (perfectly
61 // usable) one. To work around this, we watch for a
62 // NOTIFICATION_RENDER_VIEW_HOST_CHANGED message from the web contents (which
63 // will be sent during LoadURL) and suspend resource requests on the new RVH
64 // to ensure that we block the new RVH from loading anything. It should be
65 // okay to remove the NOTIFICATION_RENDER_VIEW_HOST_CHANGED registration once
66 // http://crbug.com/123007 is fixed.
67 registrar_.Add(this, content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED,
68 content::Source<content::WebContents>(web_contents()));
69 web_contents_->GetController().LoadURL(
70 url_, content::Referrer(), content::PAGE_TRANSITION_LINK,
72 registrar_.RemoveAll();
75 void AppWindowContentsImpl::NativeWindowChanged(
76 NativeAppWindow* native_app_window) {
78 base::DictionaryValue* dictionary = new base::DictionaryValue();
79 args.Append(dictionary);
80 host_->GetSerializedState(dictionary);
82 content::RenderViewHost* rvh = web_contents_->GetRenderViewHost();
83 rvh->Send(new ExtensionMsg_MessageInvoke(rvh->GetRoutingID(),
84 host_->extension_id(),
86 "updateAppWindowProperties",
91 void AppWindowContentsImpl::NativeWindowClosed() {
92 content::RenderViewHost* rvh = web_contents_->GetRenderViewHost();
93 rvh->Send(new ExtensionMsg_AppWindowClosed(rvh->GetRoutingID()));
96 content::WebContents* AppWindowContentsImpl::GetWebContents() const {
97 return web_contents_.get();
100 void AppWindowContentsImpl::Observe(
102 const content::NotificationSource& source,
103 const content::NotificationDetails& details) {
105 case content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED: {
106 // TODO(jeremya): once http://crbug.com/123007 is fixed, we'll no longer
107 // need to suspend resource requests here (the call in the constructor
108 // should be enough).
109 content::Details<std::pair<content::RenderViewHost*,
110 content::RenderViewHost*> >
111 host_details(details);
112 if (host_details->first)
113 SuspendRenderViewHost(host_details->second);
117 NOTREACHED() << "Received unexpected notification";
121 bool AppWindowContentsImpl::OnMessageReceived(const IPC::Message& message) {
123 IPC_BEGIN_MESSAGE_MAP(AppWindowContentsImpl, message)
124 IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request, OnRequest)
125 IPC_MESSAGE_HANDLER(ExtensionHostMsg_UpdateDraggableRegions,
126 UpdateDraggableRegions)
127 IPC_MESSAGE_UNHANDLED(handled = false)
128 IPC_END_MESSAGE_MAP()
132 extensions::WindowController*
133 AppWindowContentsImpl::GetExtensionWindowController() const {
137 content::WebContents* AppWindowContentsImpl::GetAssociatedWebContents() const {
138 return web_contents_.get();
141 void AppWindowContentsImpl::OnRequest(
142 const ExtensionHostMsg_Request_Params& params) {
143 extension_function_dispatcher_->Dispatch(
144 params, web_contents_->GetRenderViewHost());
147 void AppWindowContentsImpl::UpdateDraggableRegions(
148 const std::vector<extensions::DraggableRegion>& regions) {
149 host_->UpdateDraggableRegions(regions);
152 void AppWindowContentsImpl::SuspendRenderViewHost(
153 content::RenderViewHost* rvh) {
155 content::BrowserThread::PostTask(
156 content::BrowserThread::IO, FROM_HERE,
157 base::Bind(&content::ResourceDispatcherHost::BlockRequestsForRoute,
158 base::Unretained(content::ResourceDispatcherHost::Get()),
159 rvh->GetProcess()->GetID(), rvh->GetRoutingID()));