• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/common/extensions/api/app_window.h"
10 #include "content/public/browser/browser_context.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "content/public/browser/notification_details.h"
13 #include "content/public/browser/notification_source.h"
14 #include "content/public/browser/render_process_host.h"
15 #include "content/public/browser/render_view_host.h"
16 #include "content/public/browser/resource_dispatcher_host.h"
17 #include "content/public/browser/site_instance.h"
18 #include "content/public/browser/web_contents.h"
19 #include "content/public/common/renderer_preferences.h"
20 #include "extensions/common/extension_messages.h"
21 
22 namespace app_window = extensions::api::app_window;
23 
24 namespace apps {
25 
AppWindowContentsImpl(AppWindow * host)26 AppWindowContentsImpl::AppWindowContentsImpl(AppWindow* host) : host_(host) {}
27 
~AppWindowContentsImpl()28 AppWindowContentsImpl::~AppWindowContentsImpl() {}
29 
Initialize(content::BrowserContext * context,const GURL & url)30 void AppWindowContentsImpl::Initialize(content::BrowserContext* context,
31                                        const GURL& url) {
32   url_ = url;
33 
34   extension_function_dispatcher_.reset(
35       new extensions::ExtensionFunctionDispatcher(context, this));
36 
37   web_contents_.reset(
38       content::WebContents::Create(content::WebContents::CreateParams(
39           context, content::SiteInstance::CreateForURL(context, url_))));
40 
41   content::WebContentsObserver::Observe(web_contents_.get());
42   web_contents_->GetMutableRendererPrefs()->
43       browser_handles_all_top_level_requests = true;
44   web_contents_->GetRenderViewHost()->SyncRendererPrefs();
45 }
46 
LoadContents(int32 creator_process_id)47 void AppWindowContentsImpl::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) << "AppWindow created in new process ("
57             << web_contents_->GetRenderViewHost()->GetProcess()->GetID()
58             << ") != creator (" << creator_process_id << "). Routing disabled.";
59   }
60 
61   // TODO(jeremya): there's a bug where navigating a web contents to an
62   // extension URL causes it to create a new RVH and discard the old (perfectly
63   // usable) one. To work around this, we watch for a
64   // NOTIFICATION_RENDER_VIEW_HOST_CHANGED message from the web contents (which
65   // will be sent during LoadURL) and suspend resource requests on the new RVH
66   // to ensure that we block the new RVH from loading anything. It should be
67   // okay to remove the NOTIFICATION_RENDER_VIEW_HOST_CHANGED registration once
68   // http://crbug.com/123007 is fixed.
69   registrar_.Add(this, content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED,
70                  content::Source<content::WebContents>(web_contents()));
71   web_contents_->GetController().LoadURL(
72       url_, content::Referrer(), content::PAGE_TRANSITION_LINK,
73       std::string());
74   registrar_.RemoveAll();
75 }
76 
NativeWindowChanged(NativeAppWindow * native_app_window)77 void AppWindowContentsImpl::NativeWindowChanged(
78     NativeAppWindow* native_app_window) {
79   base::ListValue args;
80   base::DictionaryValue* dictionary = new base::DictionaryValue();
81   args.Append(dictionary);
82   host_->GetSerializedState(dictionary);
83 
84   content::RenderViewHost* rvh = web_contents_->GetRenderViewHost();
85   rvh->Send(new ExtensionMsg_MessageInvoke(rvh->GetRoutingID(),
86                                            host_->extension_id(),
87                                            "app.window",
88                                            "updateAppWindowProperties",
89                                            args,
90                                            false));
91 }
92 
NativeWindowClosed()93 void AppWindowContentsImpl::NativeWindowClosed() {
94   content::RenderViewHost* rvh = web_contents_->GetRenderViewHost();
95   rvh->Send(new ExtensionMsg_AppWindowClosed(rvh->GetRoutingID()));
96 }
97 
DispatchWindowShownForTests() const98 void AppWindowContentsImpl::DispatchWindowShownForTests() const {
99   base::ListValue args;
100   content::RenderViewHost* rvh = web_contents_->GetRenderViewHost();
101   rvh->Send(new ExtensionMsg_MessageInvoke(rvh->GetRoutingID(),
102                                            host_->extension_id(),
103                                            "app.window",
104                                            "appWindowShownForTests",
105                                            args,
106                                            false));
107 }
108 
GetWebContents() const109 content::WebContents* AppWindowContentsImpl::GetWebContents() const {
110   return web_contents_.get();
111 }
112 
Observe(int type,const content::NotificationSource & source,const content::NotificationDetails & details)113 void AppWindowContentsImpl::Observe(
114     int type,
115     const content::NotificationSource& source,
116     const content::NotificationDetails& details) {
117   switch (type) {
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);
127       break;
128     }
129     default:
130       NOTREACHED() << "Received unexpected notification";
131   }
132 }
133 
OnMessageReceived(const IPC::Message & message)134 bool AppWindowContentsImpl::OnMessageReceived(const IPC::Message& message) {
135   bool handled = true;
136   IPC_BEGIN_MESSAGE_MAP(AppWindowContentsImpl, 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()
142   return handled;
143 }
144 
145 extensions::WindowController*
GetExtensionWindowController() const146 AppWindowContentsImpl::GetExtensionWindowController() const {
147   return NULL;
148 }
149 
GetAssociatedWebContents() const150 content::WebContents* AppWindowContentsImpl::GetAssociatedWebContents() const {
151   return web_contents_.get();
152 }
153 
OnRequest(const ExtensionHostMsg_Request_Params & params)154 void AppWindowContentsImpl::OnRequest(
155     const ExtensionHostMsg_Request_Params& params) {
156   extension_function_dispatcher_->Dispatch(
157       params, web_contents_->GetRenderViewHost());
158 }
159 
UpdateDraggableRegions(const std::vector<extensions::DraggableRegion> & regions)160 void AppWindowContentsImpl::UpdateDraggableRegions(
161     const std::vector<extensions::DraggableRegion>& regions) {
162   host_->UpdateDraggableRegions(regions);
163 }
164 
SuspendRenderViewHost(content::RenderViewHost * rvh)165 void AppWindowContentsImpl::SuspendRenderViewHost(
166     content::RenderViewHost* rvh) {
167   DCHECK(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()));
173 }
174 
175 }  // namespace apps
176