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 "chrome/browser/extensions/app_window_contents.h"
7 #include "chrome/browser/profiles/profile.h"
8 #include "chrome/browser/ui/extensions/native_app_window.h"
9 #include "chrome/common/chrome_notification_types.h"
10 #include "chrome/common/extensions/api/app_window.h"
11 #include "chrome/common/extensions/extension_messages.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
;
22 AppWindowContents::AppWindowContents(ShellWindow
* host
)
26 AppWindowContents::~AppWindowContents() {
29 void AppWindowContents::Initialize(Profile
* profile
, const GURL
& url
) {
32 extension_function_dispatcher_
.reset(
33 new ExtensionFunctionDispatcher(profile
, this));
35 web_contents_
.reset(content::WebContents::Create(
36 content::WebContents::CreateParams(
37 profile
, content::SiteInstance::CreateForURL(profile
, 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 AppWindowContents::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) << "ShellWindow created in new process ("
55 << web_contents_
->GetRenderViewHost()->GetProcess()->GetID()
56 << ") != creator (" << creator_process_id
57 << "). Routing disabled.";
60 // TODO(jeremya): there's a bug where navigating a web contents to an
61 // extension URL causes it to create a new RVH and discard the old
62 // (perfectly usable) one. To work around this, we watch for a RVH_CHANGED
63 // message from the web contents (which will be sent during LoadURL) and
64 // suspend resource requests on the new RVH to ensure that we block the new
65 // RVH from loading anything. It should be okay to remove the
66 // NOTIFICATION_RVH_CHANGED registration once http://crbug.com/123007 is
68 registrar_
.Add(this, content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED
,
69 content::Source
<content::NavigationController
>(
70 &web_contents()->GetController()));
71 web_contents_
->GetController().LoadURL(
72 url_
, content::Referrer(), content::PAGE_TRANSITION_LINK
,
74 registrar_
.RemoveAll();
77 void AppWindowContents::NativeWindowChanged(
78 NativeAppWindow
* native_app_window
) {
80 DictionaryValue
* dictionary
= new DictionaryValue();
81 args
.Append(dictionary
);
83 gfx::Rect bounds
= host_
->GetClientBounds();
84 app_window::Bounds update
;
85 update
.left
.reset(new int(bounds
.x()));
86 update
.top
.reset(new int(bounds
.y()));
87 update
.width
.reset(new int(bounds
.width()));
88 update
.height
.reset(new int(bounds
.height()));
89 dictionary
->Set("bounds", update
.ToValue().release());
90 dictionary
->SetBoolean("fullscreen",
91 native_app_window
->IsFullscreenOrPending());
92 dictionary
->SetBoolean("minimized", native_app_window
->IsMinimized());
93 dictionary
->SetBoolean("maximized", native_app_window
->IsMaximized());
95 content::RenderViewHost
* rvh
= web_contents_
->GetRenderViewHost();
96 rvh
->Send(new ExtensionMsg_MessageInvoke(rvh
->GetRoutingID(),
97 host_
->extension()->id(),
99 "updateAppWindowProperties",
104 void AppWindowContents::NativeWindowClosed() {
105 content::RenderViewHost
* rvh
= web_contents_
->GetRenderViewHost();
106 rvh
->Send(new ExtensionMsg_AppWindowClosed(rvh
->GetRoutingID()));
109 content::WebContents
* AppWindowContents::GetWebContents() const {
110 return web_contents_
.get();
113 void AppWindowContents::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 AppWindowContents::OnMessageReceived(const IPC::Message
& message
) {
136 IPC_BEGIN_MESSAGE_MAP(AppWindowContents
, 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 AppWindowContents::GetExtensionWindowController() const {
150 content::WebContents
* AppWindowContents::GetAssociatedWebContents() const {
151 return web_contents_
.get();
154 void AppWindowContents::OnRequest(
155 const ExtensionHostMsg_Request_Params
& params
) {
156 extension_function_dispatcher_
->Dispatch(
157 params
, web_contents_
->GetRenderViewHost());
160 void AppWindowContents::UpdateDraggableRegions(
161 const std::vector
<extensions::DraggableRegion
>& regions
) {
162 host_
->UpdateDraggableRegions(regions
);
165 void AppWindowContents::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()));