Fix off-by-one error in StatsTable::GetRowValue().
[chromium-blink-merge.git] / apps / app_window_contents.cc
blobd163c2ce84ace7bce1cdb270d9412ce4e87b2d98
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/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
64 // (perfectly usable) one. To work around this, we watch for a RVH_CHANGED
65 // message from the web contents (which will be sent during LoadURL) and
66 // suspend resource requests on the new RVH to ensure that we block the new
67 // RVH from loading anything. It should be okay to remove the
68 // NOTIFICATION_RVH_CHANGED registration once http://crbug.com/123007 is
69 // fixed.
70 registrar_.Add(this, content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED,
71 content::Source<content::NavigationController>(
72 &web_contents()->GetController()));
73 web_contents_->GetController().LoadURL(
74 url_, content::Referrer(), content::PAGE_TRANSITION_LINK,
75 std::string());
76 registrar_.RemoveAll();
79 void AppWindowContents::NativeWindowChanged(
80 NativeAppWindow* native_app_window) {
81 base::ListValue args;
82 DictionaryValue* dictionary = new DictionaryValue();
83 args.Append(dictionary);
85 gfx::Rect bounds = host_->GetClientBounds();
86 app_window::Bounds update;
87 update.left.reset(new int(bounds.x()));
88 update.top.reset(new int(bounds.y()));
89 update.width.reset(new int(bounds.width()));
90 update.height.reset(new int(bounds.height()));
91 dictionary->Set("bounds", update.ToValue().release());
92 dictionary->SetBoolean("fullscreen",
93 native_app_window->IsFullscreenOrPending());
94 dictionary->SetBoolean("minimized", native_app_window->IsMinimized());
95 dictionary->SetBoolean("maximized", native_app_window->IsMaximized());
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