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. 4 5#include "apps/app_window_contents.h" 6 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" 19 20namespace app_window = extensions::api::app_window; 21 22namespace { 23 24const int kUnboundedSize = apps::ShellWindow::SizeConstraints::kUnboundedSize; 25 26} 27 28namespace apps { 29 30AppWindowContents::AppWindowContents(ShellWindow* host) 31 : host_(host) { 32} 33 34AppWindowContents::~AppWindowContents() { 35} 36 37void AppWindowContents::Initialize(Profile* profile, const GURL& url) { 38 url_ = url; 39 40 extension_function_dispatcher_.reset( 41 new ExtensionFunctionDispatcher(profile, this)); 42 43 web_contents_.reset(content::WebContents::Create( 44 content::WebContents::CreateParams( 45 profile, content::SiteInstance::CreateForURL(profile, url_)))); 46 47 content::WebContentsObserver::Observe(web_contents_.get()); 48 web_contents_->GetMutableRendererPrefs()-> 49 browser_handles_all_top_level_requests = true; 50 web_contents_->GetRenderViewHost()->SyncRendererPrefs(); 51} 52 53void AppWindowContents::LoadContents(int32 creator_process_id) { 54 // If the new view is in the same process as the creator, block the created 55 // RVH from loading anything until the background page has had a chance to 56 // do any initialization it wants. If it's a different process, the new RVH 57 // shouldn't communicate with the background page anyway (e.g. sandboxed). 58 if (web_contents_->GetRenderViewHost()->GetProcess()->GetID() == 59 creator_process_id) { 60 SuspendRenderViewHost(web_contents_->GetRenderViewHost()); 61 } else { 62 VLOG(1) << "ShellWindow created in new process (" 63 << web_contents_->GetRenderViewHost()->GetProcess()->GetID() 64 << ") != creator (" << creator_process_id 65 << "). Routing disabled."; 66 } 67 68 // TODO(jeremya): there's a bug where navigating a web contents to an 69 // extension URL causes it to create a new RVH and discard the old (perfectly 70 // usable) one. To work around this, we watch for a 71 // NOTIFICATION_RENDER_VIEW_HOST_CHANGED message from the web contents (which 72 // will be sent during LoadURL) and suspend resource requests on the new RVH 73 // to ensure that we block the new RVH from loading anything. It should be 74 // okay to remove the NOTIFICATION_RENDER_VIEW_HOST_CHANGED registration once 75 // http://crbug.com/123007 is fixed. 76 registrar_.Add(this, content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED, 77 content::Source<content::WebContents>(web_contents())); 78 web_contents_->GetController().LoadURL( 79 url_, content::Referrer(), content::PAGE_TRANSITION_LINK, 80 std::string()); 81 registrar_.RemoveAll(); 82} 83 84void AppWindowContents::NativeWindowChanged( 85 NativeAppWindow* native_app_window) { 86 base::ListValue args; 87 DictionaryValue* dictionary = new DictionaryValue(); 88 args.Append(dictionary); 89 90 gfx::Rect bounds = host_->GetClientBounds(); 91 app_window::Bounds update; 92 update.left.reset(new int(bounds.x())); 93 update.top.reset(new int(bounds.y())); 94 update.width.reset(new int(bounds.width())); 95 update.height.reset(new int(bounds.height())); 96 dictionary->Set("bounds", update.ToValue().release()); 97 dictionary->SetBoolean("fullscreen", 98 native_app_window->IsFullscreenOrPending()); 99 dictionary->SetBoolean("minimized", native_app_window->IsMinimized()); 100 dictionary->SetBoolean("maximized", native_app_window->IsMaximized()); 101 dictionary->SetBoolean("alwaysOnTop", host_->IsAlwaysOnTop()); 102 103 const ShellWindow::SizeConstraints& size_constraints = 104 host_->size_constraints(); 105 gfx::Size min_size = size_constraints.GetMinimumSize(); 106 gfx::Size max_size = size_constraints.GetMaximumSize(); 107 if (min_size.width() != kUnboundedSize) 108 dictionary->SetInteger("minWidth", min_size.width()); 109 if (min_size.height() != kUnboundedSize) 110 dictionary->SetInteger("minHeight", min_size.height()); 111 if (max_size.width() != kUnboundedSize) 112 dictionary->SetInteger("maxWidth", max_size.width()); 113 if (max_size.height() != kUnboundedSize) 114 dictionary->SetInteger("maxHeight", max_size.height()); 115 116 content::RenderViewHost* rvh = web_contents_->GetRenderViewHost(); 117 rvh->Send(new ExtensionMsg_MessageInvoke(rvh->GetRoutingID(), 118 host_->extension_id(), 119 "app.window", 120 "updateAppWindowProperties", 121 args, 122 false)); 123} 124 125void AppWindowContents::NativeWindowClosed() { 126 content::RenderViewHost* rvh = web_contents_->GetRenderViewHost(); 127 rvh->Send(new ExtensionMsg_AppWindowClosed(rvh->GetRoutingID())); 128} 129 130content::WebContents* AppWindowContents::GetWebContents() const { 131 return web_contents_.get(); 132} 133 134void AppWindowContents::Observe( 135 int type, 136 const content::NotificationSource& source, 137 const content::NotificationDetails& details) { 138 switch (type) { 139 case content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED: { 140 // TODO(jeremya): once http://crbug.com/123007 is fixed, we'll no longer 141 // need to suspend resource requests here (the call in the constructor 142 // should be enough). 143 content::Details<std::pair<content::RenderViewHost*, 144 content::RenderViewHost*> > 145 host_details(details); 146 if (host_details->first) 147 SuspendRenderViewHost(host_details->second); 148 break; 149 } 150 default: 151 NOTREACHED() << "Received unexpected notification"; 152 } 153} 154 155bool AppWindowContents::OnMessageReceived(const IPC::Message& message) { 156 bool handled = true; 157 IPC_BEGIN_MESSAGE_MAP(AppWindowContents, message) 158 IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request, OnRequest) 159 IPC_MESSAGE_HANDLER(ExtensionHostMsg_UpdateDraggableRegions, 160 UpdateDraggableRegions) 161 IPC_MESSAGE_UNHANDLED(handled = false) 162 IPC_END_MESSAGE_MAP() 163 return handled; 164} 165 166extensions::WindowController* 167AppWindowContents::GetExtensionWindowController() const { 168 return NULL; 169} 170 171content::WebContents* AppWindowContents::GetAssociatedWebContents() const { 172 return web_contents_.get(); 173} 174 175void AppWindowContents::OnRequest( 176 const ExtensionHostMsg_Request_Params& params) { 177 extension_function_dispatcher_->Dispatch( 178 params, web_contents_->GetRenderViewHost()); 179} 180 181void AppWindowContents::UpdateDraggableRegions( 182 const std::vector<extensions::DraggableRegion>& regions) { 183 host_->UpdateDraggableRegions(regions); 184} 185 186void AppWindowContents::SuspendRenderViewHost( 187 content::RenderViewHost* rvh) { 188 DCHECK(rvh); 189 content::BrowserThread::PostTask( 190 content::BrowserThread::IO, FROM_HERE, 191 base::Bind(&content::ResourceDispatcherHost::BlockRequestsForRoute, 192 base::Unretained(content::ResourceDispatcherHost::Get()), 193 rvh->GetProcess()->GetID(), rvh->GetRoutingID())); 194} 195 196} // namespace apps 197