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 "extensions/browser/app_window/app_window_contents.h"
10 #include "content/public/browser/browser_context.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "content/public/browser/render_frame_host.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 #include "extensions/browser/app_window/native_app_window.h"
20 #include "extensions/common/extension_messages.h"
22 namespace extensions
{
24 AppWindowContentsImpl::AppWindowContentsImpl(AppWindow
* host
) : host_(host
) {}
26 AppWindowContentsImpl::~AppWindowContentsImpl() {}
28 void AppWindowContentsImpl::Initialize(content::BrowserContext
* context
,
33 content::WebContents::Create(content::WebContents::CreateParams(
34 context
, content::SiteInstance::CreateForURL(context
, url_
))));
36 Observe(web_contents_
.get());
37 web_contents_
->GetMutableRendererPrefs()->
38 browser_handles_all_top_level_requests
= true;
39 web_contents_
->GetRenderViewHost()->SyncRendererPrefs();
42 void AppWindowContentsImpl::LoadContents(int32 creator_process_id
) {
43 // If the new view is in the same process as the creator, block the created
44 // RVH from loading anything until the background page has had a chance to
45 // do any initialization it wants. If it's a different process, the new RVH
46 // shouldn't communicate with the background page anyway (e.g. sandboxed).
47 if (web_contents_
->GetMainFrame()->GetProcess()->GetID() ==
49 SuspendRenderFrameHost(web_contents_
->GetMainFrame());
51 VLOG(1) << "AppWindow created in new process ("
52 << web_contents_
->GetMainFrame()->GetProcess()->GetID()
53 << ") != creator (" << creator_process_id
<< "). Routing disabled.";
56 web_contents_
->GetController().LoadURL(
57 url_
, content::Referrer(), ui::PAGE_TRANSITION_LINK
,
61 void AppWindowContentsImpl::NativeWindowChanged(
62 NativeAppWindow
* native_app_window
) {
64 base::DictionaryValue
* dictionary
= new base::DictionaryValue();
65 args
.Append(dictionary
);
66 host_
->GetSerializedState(dictionary
);
68 content::RenderFrameHost
* rfh
= web_contents_
->GetMainFrame();
69 rfh
->Send(new ExtensionMsg_MessageInvoke(
70 rfh
->GetRoutingID(), host_
->extension_id(), "app.window",
71 "updateAppWindowProperties", args
, false));
74 void AppWindowContentsImpl::NativeWindowClosed() {
75 content::RenderViewHost
* rvh
= web_contents_
->GetRenderViewHost();
76 rvh
->Send(new ExtensionMsg_AppWindowClosed(rvh
->GetRoutingID()));
79 void AppWindowContentsImpl::DispatchWindowShownForTests() const {
81 content::RenderFrameHost
* rfh
= web_contents_
->GetMainFrame();
82 rfh
->Send(new ExtensionMsg_MessageInvoke(
83 rfh
->GetRoutingID(), host_
->extension_id(), "app.window",
84 "appWindowShownForTests", args
, false));
87 content::WebContents
* AppWindowContentsImpl::GetWebContents() const {
88 return web_contents_
.get();
91 WindowController
* AppWindowContentsImpl::GetWindowController() const {
95 bool AppWindowContentsImpl::OnMessageReceived(const IPC::Message
& message
) {
97 IPC_BEGIN_MESSAGE_MAP(AppWindowContentsImpl
, message
)
98 IPC_MESSAGE_HANDLER(ExtensionHostMsg_UpdateDraggableRegions
,
99 UpdateDraggableRegions
)
100 IPC_MESSAGE_UNHANDLED(handled
= false)
101 IPC_END_MESSAGE_MAP()
105 void AppWindowContentsImpl::UpdateDraggableRegions(
106 const std::vector
<DraggableRegion
>& regions
) {
107 host_
->UpdateDraggableRegions(regions
);
110 void AppWindowContentsImpl::SuspendRenderFrameHost(
111 content::RenderFrameHost
* rfh
) {
113 content::BrowserThread::PostTask(
114 content::BrowserThread::IO
, FROM_HERE
,
115 base::Bind(&content::ResourceDispatcherHost::BlockRequestsForRoute
,
116 base::Unretained(content::ResourceDispatcherHost::Get()),
117 rfh
->GetProcess()->GetID(), rfh
->GetRoutingID()));
120 } // namespace extensions