Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / apps / app_window_contents.cc
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 "chrome/common/extensions/extension_messages.h"
11 #include "content/public/browser/browser_context.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
20 namespace app_window = extensions::api::app_window;
21
22 namespace apps {
23
24 AppWindowContentsImpl::AppWindowContentsImpl(AppWindow* host) : host_(host) {}
25
26 AppWindowContentsImpl::~AppWindowContentsImpl() {}
27
28 void AppWindowContentsImpl::Initialize(content::BrowserContext* context,
29                                        const GURL& url) {
30   url_ = url;
31
32   extension_function_dispatcher_.reset(
33       new ExtensionFunctionDispatcher(context, this));
34
35   web_contents_.reset(
36       content::WebContents::Create(content::WebContents::CreateParams(
37           context, content::SiteInstance::CreateForURL(context, url_))));
38
39   content::WebContentsObserver::Observe(web_contents_.get());
40   web_contents_->GetMutableRendererPrefs()->
41       browser_handles_all_top_level_requests = true;
42   web_contents_->GetRenderViewHost()->SyncRendererPrefs();
43 }
44
45 void AppWindowContentsImpl::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() ==
51       creator_process_id) {
52     SuspendRenderViewHost(web_contents_->GetRenderViewHost());
53   } else {
54     VLOG(1) << "AppWindow created in new process ("
55             << web_contents_->GetRenderViewHost()->GetProcess()->GetID()
56             << ") != creator (" << creator_process_id << "). Routing disabled.";
57   }
58
59   // TODO(jeremya): there's a bug where navigating a web contents to an
60   // extension URL causes it to create a new RVH and discard the old (perfectly
61   // usable) one. To work around this, we watch for a
62   // NOTIFICATION_RENDER_VIEW_HOST_CHANGED message from the web contents (which
63   // will be sent during LoadURL) and suspend resource requests on the new RVH
64   // to ensure that we block the new RVH from loading anything. It should be
65   // okay to remove the NOTIFICATION_RENDER_VIEW_HOST_CHANGED registration once
66   // http://crbug.com/123007 is fixed.
67   registrar_.Add(this, content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED,
68                  content::Source<content::WebContents>(web_contents()));
69   web_contents_->GetController().LoadURL(
70       url_, content::Referrer(), content::PAGE_TRANSITION_LINK,
71       std::string());
72   registrar_.RemoveAll();
73 }
74
75 void AppWindowContentsImpl::NativeWindowChanged(
76     NativeAppWindow* native_app_window) {
77   base::ListValue args;
78   base::DictionaryValue* dictionary = new base::DictionaryValue();
79   args.Append(dictionary);
80   host_->GetSerializedState(dictionary);
81
82   content::RenderViewHost* rvh = web_contents_->GetRenderViewHost();
83   rvh->Send(new ExtensionMsg_MessageInvoke(rvh->GetRoutingID(),
84                                            host_->extension_id(),
85                                            "app.window",
86                                            "updateAppWindowProperties",
87                                            args,
88                                            false));
89 }
90
91 void AppWindowContentsImpl::NativeWindowClosed() {
92   content::RenderViewHost* rvh = web_contents_->GetRenderViewHost();
93   rvh->Send(new ExtensionMsg_AppWindowClosed(rvh->GetRoutingID()));
94 }
95
96 content::WebContents* AppWindowContentsImpl::GetWebContents() const {
97   return web_contents_.get();
98 }
99
100 void AppWindowContentsImpl::Observe(
101     int type,
102     const content::NotificationSource& source,
103     const content::NotificationDetails& details) {
104   switch (type) {
105     case content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED: {
106       // TODO(jeremya): once http://crbug.com/123007 is fixed, we'll no longer
107       // need to suspend resource requests here (the call in the constructor
108       // should be enough).
109       content::Details<std::pair<content::RenderViewHost*,
110                                  content::RenderViewHost*> >
111           host_details(details);
112       if (host_details->first)
113         SuspendRenderViewHost(host_details->second);
114       break;
115     }
116     default:
117       NOTREACHED() << "Received unexpected notification";
118   }
119 }
120
121 bool AppWindowContentsImpl::OnMessageReceived(const IPC::Message& message) {
122   bool handled = true;
123   IPC_BEGIN_MESSAGE_MAP(AppWindowContentsImpl, message)
124     IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request, OnRequest)
125     IPC_MESSAGE_HANDLER(ExtensionHostMsg_UpdateDraggableRegions,
126                         UpdateDraggableRegions)
127     IPC_MESSAGE_UNHANDLED(handled = false)
128   IPC_END_MESSAGE_MAP()
129   return handled;
130 }
131
132 extensions::WindowController*
133 AppWindowContentsImpl::GetExtensionWindowController() const {
134   return NULL;
135 }
136
137 content::WebContents* AppWindowContentsImpl::GetAssociatedWebContents() const {
138   return web_contents_.get();
139 }
140
141 void AppWindowContentsImpl::OnRequest(
142     const ExtensionHostMsg_Request_Params& params) {
143   extension_function_dispatcher_->Dispatch(
144       params, web_contents_->GetRenderViewHost());
145 }
146
147 void AppWindowContentsImpl::UpdateDraggableRegions(
148     const std::vector<extensions::DraggableRegion>& regions) {
149   host_->UpdateDraggableRegions(regions);
150 }
151
152 void AppWindowContentsImpl::SuspendRenderViewHost(
153     content::RenderViewHost* rvh) {
154   DCHECK(rvh);
155   content::BrowserThread::PostTask(
156       content::BrowserThread::IO, FROM_HERE,
157       base::Bind(&content::ResourceDispatcherHost::BlockRequestsForRoute,
158                  base::Unretained(content::ResourceDispatcherHost::Get()),
159                  rvh->GetProcess()->GetID(), rvh->GetRoutingID()));
160 }
161
162 }  // namespace apps