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
)
25 : host_(host
), is_blocking_requests_(false), is_window_ready_(false) {}
27 AppWindowContentsImpl::~AppWindowContentsImpl() {}
29 void AppWindowContentsImpl::Initialize(content::BrowserContext
* context
,
34 content::WebContents::Create(content::WebContents::CreateParams(
35 context
, content::SiteInstance::CreateForURL(context
, url_
))));
37 Observe(web_contents_
.get());
38 web_contents_
->GetMutableRendererPrefs()->
39 browser_handles_all_top_level_requests
= true;
40 web_contents_
->GetRenderViewHost()->SyncRendererPrefs();
43 void AppWindowContentsImpl::LoadContents(int32 creator_process_id
) {
44 // If the new view is in the same process as the creator, block the created
45 // RVH from loading anything until the background page has had a chance to
46 // do any initialization it wants. If it's a different process, the new RVH
47 // shouldn't communicate with the background page anyway (e.g. sandboxed).
48 if (web_contents_
->GetMainFrame()->GetProcess()->GetID() ==
50 SuspendRenderFrameHost(web_contents_
->GetMainFrame());
52 VLOG(1) << "AppWindow created in new process ("
53 << web_contents_
->GetMainFrame()->GetProcess()->GetID()
54 << ") != creator (" << creator_process_id
<< "). Routing disabled.";
57 web_contents_
->GetController().LoadURL(
58 url_
, content::Referrer(), ui::PAGE_TRANSITION_LINK
,
62 void AppWindowContentsImpl::NativeWindowChanged(
63 NativeAppWindow
* native_app_window
) {
65 base::DictionaryValue
* dictionary
= new base::DictionaryValue();
66 args
.Append(dictionary
);
67 host_
->GetSerializedState(dictionary
);
69 content::RenderFrameHost
* rfh
= web_contents_
->GetMainFrame();
70 rfh
->Send(new ExtensionMsg_MessageInvoke(
71 rfh
->GetRoutingID(), host_
->extension_id(), "app.window",
72 "updateAppWindowProperties", args
, false));
75 void AppWindowContentsImpl::NativeWindowClosed() {
76 content::RenderViewHost
* rvh
= web_contents_
->GetRenderViewHost();
77 rvh
->Send(new ExtensionMsg_AppWindowClosed(rvh
->GetRoutingID()));
80 void AppWindowContentsImpl::DispatchWindowShownForTests() const {
82 content::RenderFrameHost
* rfh
= web_contents_
->GetMainFrame();
83 rfh
->Send(new ExtensionMsg_MessageInvoke(
84 rfh
->GetRoutingID(), host_
->extension_id(), "app.window",
85 "appWindowShownForTests", args
, false));
88 void AppWindowContentsImpl::OnWindowReady() {
89 is_window_ready_
= true;
90 if (is_blocking_requests_
) {
91 is_blocking_requests_
= false;
92 content::RenderFrameHost
* frame
= web_contents_
->GetMainFrame();
93 content::BrowserThread::PostTask(
94 content::BrowserThread::IO
, FROM_HERE
,
96 &content::ResourceDispatcherHost::ResumeBlockedRequestsForRoute
,
97 base::Unretained(content::ResourceDispatcherHost::Get()),
98 frame
->GetProcess()->GetID(),
99 frame
->GetRenderViewHost()->GetRoutingID()));
103 content::WebContents
* AppWindowContentsImpl::GetWebContents() const {
104 return web_contents_
.get();
107 WindowController
* AppWindowContentsImpl::GetWindowController() const {
111 bool AppWindowContentsImpl::OnMessageReceived(const IPC::Message
& message
) {
113 IPC_BEGIN_MESSAGE_MAP(AppWindowContentsImpl
, message
)
114 IPC_MESSAGE_HANDLER(ExtensionHostMsg_UpdateDraggableRegions
,
115 UpdateDraggableRegions
)
116 IPC_MESSAGE_UNHANDLED(handled
= false)
117 IPC_END_MESSAGE_MAP()
121 void AppWindowContentsImpl::ReadyToCommitNavigation(
122 content::NavigationHandle
* handle
) {
123 if (!is_window_ready_
)
124 host_
->OnReadyToCommitFirstNavigation();
127 void AppWindowContentsImpl::UpdateDraggableRegions(
128 const std::vector
<DraggableRegion
>& regions
) {
129 host_
->UpdateDraggableRegions(regions
);
132 void AppWindowContentsImpl::SuspendRenderFrameHost(
133 content::RenderFrameHost
* rfh
) {
135 // Don't bother blocking requests if the renderer side is already good to go.
136 if (is_window_ready_
)
138 is_blocking_requests_
= true;
139 // The ResourceDispatcherHost only accepts RenderViewHost child ids.
140 // TODO(devlin): This will need to change for site isolation.
141 content::BrowserThread::PostTask(
142 content::BrowserThread::IO
, FROM_HERE
,
143 base::Bind(&content::ResourceDispatcherHost::BlockRequestsForRoute
,
144 base::Unretained(content::ResourceDispatcherHost::Get()),
145 rfh
->GetProcess()->GetID(),
146 rfh
->GetRenderViewHost()->GetRoutingID()));
149 } // namespace extensions