Add structure packing support for mojo modules
[chromium-blink-merge.git] / apps / app_window_contents.cc
blob4f11a10e680439f8163f41e22cbcb60c5d02d8cf
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/browser/profiles/profile.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 namespace apps {
24 AppWindowContents::AppWindowContents(ShellWindow* host)
25 : host_(host) {
28 AppWindowContents::~AppWindowContents() {
31 void AppWindowContents::Initialize(Profile* profile, const GURL& url) {
32 url_ = url;
34 extension_function_dispatcher_.reset(
35 new ExtensionFunctionDispatcher(profile, this));
37 web_contents_.reset(content::WebContents::Create(
38 content::WebContents::CreateParams(
39 profile, content::SiteInstance::CreateForURL(profile, 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 AppWindowContents::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() ==
53 creator_process_id) {
54 SuspendRenderViewHost(web_contents_->GetRenderViewHost());
55 } else {
56 VLOG(1) << "ShellWindow created in new process ("
57 << web_contents_->GetRenderViewHost()->GetProcess()->GetID()
58 << ") != creator (" << creator_process_id
59 << "). Routing disabled.";
62 // TODO(jeremya): there's a bug where navigating a web contents to an
63 // extension URL causes it to create a new RVH and discard the old (perfectly
64 // usable) one. To work around this, we watch for a
65 // NOTIFICATION_RENDER_VIEW_HOST_CHANGED message from the web contents (which
66 // will be sent during LoadURL) and suspend resource requests on the new RVH
67 // to ensure that we block the new RVH from loading anything. It should be
68 // okay to remove the NOTIFICATION_RENDER_VIEW_HOST_CHANGED registration once
69 // http://crbug.com/123007 is fixed.
70 registrar_.Add(this, content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED,
71 content::Source<content::WebContents>(web_contents()));
72 web_contents_->GetController().LoadURL(
73 url_, content::Referrer(), content::PAGE_TRANSITION_LINK,
74 std::string());
75 registrar_.RemoveAll();
78 void AppWindowContents::NativeWindowChanged(
79 NativeAppWindow* native_app_window) {
80 base::ListValue args;
81 DictionaryValue* dictionary = new DictionaryValue();
82 args.Append(dictionary);
84 gfx::Rect bounds = host_->GetClientBounds();
85 app_window::Bounds update;
86 update.left.reset(new int(bounds.x()));
87 update.top.reset(new int(bounds.y()));
88 update.width.reset(new int(bounds.width()));
89 update.height.reset(new int(bounds.height()));
90 dictionary->Set("bounds", update.ToValue().release());
91 dictionary->SetBoolean("fullscreen",
92 native_app_window->IsFullscreenOrPending());
93 dictionary->SetBoolean("minimized", native_app_window->IsMinimized());
94 dictionary->SetBoolean("maximized", native_app_window->IsMaximized());
95 dictionary->SetBoolean("alwaysOnTop", native_app_window->IsAlwaysOnTop());
97 content::RenderViewHost* rvh = web_contents_->GetRenderViewHost();
98 rvh->Send(new ExtensionMsg_MessageInvoke(rvh->GetRoutingID(),
99 host_->extension_id(),
100 "app.window",
101 "updateAppWindowProperties",
102 args,
103 false));
106 void AppWindowContents::NativeWindowClosed() {
107 content::RenderViewHost* rvh = web_contents_->GetRenderViewHost();
108 rvh->Send(new ExtensionMsg_AppWindowClosed(rvh->GetRoutingID()));
111 content::WebContents* AppWindowContents::GetWebContents() const {
112 return web_contents_.get();
115 void AppWindowContents::Observe(
116 int type,
117 const content::NotificationSource& source,
118 const content::NotificationDetails& details) {
119 switch (type) {
120 case content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED: {
121 // TODO(jeremya): once http://crbug.com/123007 is fixed, we'll no longer
122 // need to suspend resource requests here (the call in the constructor
123 // should be enough).
124 content::Details<std::pair<content::RenderViewHost*,
125 content::RenderViewHost*> >
126 host_details(details);
127 if (host_details->first)
128 SuspendRenderViewHost(host_details->second);
129 break;
131 default:
132 NOTREACHED() << "Received unexpected notification";
136 bool AppWindowContents::OnMessageReceived(const IPC::Message& message) {
137 bool handled = true;
138 IPC_BEGIN_MESSAGE_MAP(AppWindowContents, message)
139 IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request, OnRequest)
140 IPC_MESSAGE_HANDLER(ExtensionHostMsg_UpdateDraggableRegions,
141 UpdateDraggableRegions)
142 IPC_MESSAGE_UNHANDLED(handled = false)
143 IPC_END_MESSAGE_MAP()
144 return handled;
147 extensions::WindowController*
148 AppWindowContents::GetExtensionWindowController() const {
149 return NULL;
152 content::WebContents* AppWindowContents::GetAssociatedWebContents() const {
153 return web_contents_.get();
156 void AppWindowContents::OnRequest(
157 const ExtensionHostMsg_Request_Params& params) {
158 extension_function_dispatcher_->Dispatch(
159 params, web_contents_->GetRenderViewHost());
162 void AppWindowContents::UpdateDraggableRegions(
163 const std::vector<extensions::DraggableRegion>& regions) {
164 host_->UpdateDraggableRegions(regions);
167 void AppWindowContents::SuspendRenderViewHost(
168 content::RenderViewHost* rvh) {
169 DCHECK(rvh);
170 content::BrowserThread::PostTask(
171 content::BrowserThread::IO, FROM_HERE,
172 base::Bind(&content::ResourceDispatcherHost::BlockRequestsForRoute,
173 base::Unretained(content::ResourceDispatcherHost::Get()),
174 rvh->GetProcess()->GetID(), rvh->GetRoutingID()));
177 } // namespace apps