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"
10 #include "chrome/browser/chrome_notification_types.h"
11 #include "chrome/common/extensions/api/app_window.h"
12 #include "content/public/browser/browser_context.h"
13 #include "content/public/browser/browser_thread.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/browser/app_window/native_app_window.h"
21 #include "extensions/common/extension_messages.h"
23 namespace app_window
= extensions::api::app_window
;
27 AppWindowContentsImpl::AppWindowContentsImpl(AppWindow
* host
) : host_(host
) {}
29 AppWindowContentsImpl::~AppWindowContentsImpl() {}
31 void AppWindowContentsImpl::Initialize(content::BrowserContext
* context
,
35 extension_function_dispatcher_
.reset(
36 new extensions::ExtensionFunctionDispatcher(context
, this));
39 content::WebContents::Create(content::WebContents::CreateParams(
40 context
, content::SiteInstance::CreateForURL(context
, url_
))));
42 Observe(web_contents_
.get());
43 web_contents_
->GetMutableRendererPrefs()->
44 browser_handles_all_top_level_requests
= true;
45 web_contents_
->GetRenderViewHost()->SyncRendererPrefs();
48 void AppWindowContentsImpl::LoadContents(int32 creator_process_id
) {
49 // If the new view is in the same process as the creator, block the created
50 // RVH from loading anything until the background page has had a chance to
51 // do any initialization it wants. If it's a different process, the new RVH
52 // shouldn't communicate with the background page anyway (e.g. sandboxed).
53 if (web_contents_
->GetRenderViewHost()->GetProcess()->GetID() ==
55 SuspendRenderViewHost(web_contents_
->GetRenderViewHost());
57 VLOG(1) << "AppWindow created in new process ("
58 << web_contents_
->GetRenderViewHost()->GetProcess()->GetID()
59 << ") != creator (" << creator_process_id
<< "). Routing disabled.";
62 web_contents_
->GetController().LoadURL(
63 url_
, content::Referrer(), content::PAGE_TRANSITION_LINK
,
67 void AppWindowContentsImpl::NativeWindowChanged(
68 extensions::NativeAppWindow
* native_app_window
) {
70 base::DictionaryValue
* dictionary
= new base::DictionaryValue();
71 args
.Append(dictionary
);
72 host_
->GetSerializedState(dictionary
);
74 content::RenderViewHost
* rvh
= web_contents_
->GetRenderViewHost();
75 rvh
->Send(new ExtensionMsg_MessageInvoke(rvh
->GetRoutingID(),
76 host_
->extension_id(),
78 "updateAppWindowProperties",
83 void AppWindowContentsImpl::NativeWindowClosed() {
84 content::RenderViewHost
* rvh
= web_contents_
->GetRenderViewHost();
85 rvh
->Send(new ExtensionMsg_AppWindowClosed(rvh
->GetRoutingID()));
88 void AppWindowContentsImpl::DispatchWindowShownForTests() const {
90 content::RenderViewHost
* rvh
= web_contents_
->GetRenderViewHost();
91 rvh
->Send(new ExtensionMsg_MessageInvoke(rvh
->GetRoutingID(),
92 host_
->extension_id(),
94 "appWindowShownForTests",
99 content::WebContents
* AppWindowContentsImpl::GetWebContents() const {
100 return web_contents_
.get();
103 bool AppWindowContentsImpl::OnMessageReceived(const IPC::Message
& message
) {
105 IPC_BEGIN_MESSAGE_MAP(AppWindowContentsImpl
, message
)
106 IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request
, OnRequest
)
107 IPC_MESSAGE_HANDLER(ExtensionHostMsg_UpdateDraggableRegions
,
108 UpdateDraggableRegions
)
109 IPC_MESSAGE_UNHANDLED(handled
= false)
110 IPC_END_MESSAGE_MAP()
114 extensions::WindowController
*
115 AppWindowContentsImpl::GetExtensionWindowController() const {
119 content::WebContents
* AppWindowContentsImpl::GetAssociatedWebContents() const {
120 return web_contents_
.get();
123 void AppWindowContentsImpl::OnRequest(
124 const ExtensionHostMsg_Request_Params
& params
) {
125 extension_function_dispatcher_
->Dispatch(
126 params
, web_contents_
->GetRenderViewHost());
129 void AppWindowContentsImpl::UpdateDraggableRegions(
130 const std::vector
<extensions::DraggableRegion
>& regions
) {
131 host_
->UpdateDraggableRegions(regions
);
134 void AppWindowContentsImpl::SuspendRenderViewHost(
135 content::RenderViewHost
* rvh
) {
137 content::BrowserThread::PostTask(
138 content::BrowserThread::IO
, FROM_HERE
,
139 base::Bind(&content::ResourceDispatcherHost::BlockRequestsForRoute
,
140 base::Unretained(content::ResourceDispatcherHost::Get()),
141 rvh
->GetProcess()->GetID(), rvh
->GetRoutingID()));