OLD | NEW |
| (Empty) |
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 <string> | |
8 #include <utility> | |
9 | |
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" | |
22 | |
23 namespace app_window = extensions::api::app_window; | |
24 using extensions::AppWindow; | |
25 | |
26 namespace apps { | |
27 | |
28 AppWindowContentsImpl::AppWindowContentsImpl(AppWindow* host) : host_(host) {} | |
29 | |
30 AppWindowContentsImpl::~AppWindowContentsImpl() {} | |
31 | |
32 void AppWindowContentsImpl::Initialize(content::BrowserContext* context, | |
33 const GURL& url) { | |
34 url_ = url; | |
35 | |
36 extension_function_dispatcher_.reset( | |
37 new extensions::ExtensionFunctionDispatcher(context, this)); | |
38 | |
39 web_contents_.reset( | |
40 content::WebContents::Create(content::WebContents::CreateParams( | |
41 context, content::SiteInstance::CreateForURL(context, url_)))); | |
42 | |
43 Observe(web_contents_.get()); | |
44 web_contents_->GetMutableRendererPrefs()-> | |
45 browser_handles_all_top_level_requests = true; | |
46 web_contents_->GetRenderViewHost()->SyncRendererPrefs(); | |
47 } | |
48 | |
49 void AppWindowContentsImpl::LoadContents(int32 creator_process_id) { | |
50 // If the new view is in the same process as the creator, block the created | |
51 // RVH from loading anything until the background page has had a chance to | |
52 // do any initialization it wants. If it's a different process, the new RVH | |
53 // shouldn't communicate with the background page anyway (e.g. sandboxed). | |
54 if (web_contents_->GetRenderViewHost()->GetProcess()->GetID() == | |
55 creator_process_id) { | |
56 SuspendRenderViewHost(web_contents_->GetRenderViewHost()); | |
57 } else { | |
58 VLOG(1) << "AppWindow created in new process (" | |
59 << web_contents_->GetRenderViewHost()->GetProcess()->GetID() | |
60 << ") != creator (" << creator_process_id << "). Routing disabled."; | |
61 } | |
62 | |
63 web_contents_->GetController().LoadURL( | |
64 url_, content::Referrer(), content::PAGE_TRANSITION_LINK, | |
65 std::string()); | |
66 } | |
67 | |
68 void AppWindowContentsImpl::NativeWindowChanged( | |
69 extensions::NativeAppWindow* native_app_window) { | |
70 base::ListValue args; | |
71 base::DictionaryValue* dictionary = new base::DictionaryValue(); | |
72 args.Append(dictionary); | |
73 host_->GetSerializedState(dictionary); | |
74 | |
75 content::RenderViewHost* rvh = web_contents_->GetRenderViewHost(); | |
76 rvh->Send(new ExtensionMsg_MessageInvoke(rvh->GetRoutingID(), | |
77 host_->extension_id(), | |
78 "app.window", | |
79 "updateAppWindowProperties", | |
80 args, | |
81 false)); | |
82 } | |
83 | |
84 void AppWindowContentsImpl::NativeWindowClosed() { | |
85 content::RenderViewHost* rvh = web_contents_->GetRenderViewHost(); | |
86 rvh->Send(new ExtensionMsg_AppWindowClosed(rvh->GetRoutingID())); | |
87 } | |
88 | |
89 void AppWindowContentsImpl::DispatchWindowShownForTests() const { | |
90 base::ListValue args; | |
91 content::RenderViewHost* rvh = web_contents_->GetRenderViewHost(); | |
92 rvh->Send(new ExtensionMsg_MessageInvoke(rvh->GetRoutingID(), | |
93 host_->extension_id(), | |
94 "app.window", | |
95 "appWindowShownForTests", | |
96 args, | |
97 false)); | |
98 } | |
99 | |
100 content::WebContents* AppWindowContentsImpl::GetWebContents() const { | |
101 return web_contents_.get(); | |
102 } | |
103 | |
104 bool AppWindowContentsImpl::OnMessageReceived(const IPC::Message& message) { | |
105 bool handled = true; | |
106 IPC_BEGIN_MESSAGE_MAP(AppWindowContentsImpl, message) | |
107 IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request, OnRequest) | |
108 IPC_MESSAGE_HANDLER(ExtensionHostMsg_UpdateDraggableRegions, | |
109 UpdateDraggableRegions) | |
110 IPC_MESSAGE_UNHANDLED(handled = false) | |
111 IPC_END_MESSAGE_MAP() | |
112 return handled; | |
113 } | |
114 | |
115 extensions::WindowController* | |
116 AppWindowContentsImpl::GetExtensionWindowController() const { | |
117 return NULL; | |
118 } | |
119 | |
120 content::WebContents* AppWindowContentsImpl::GetAssociatedWebContents() const { | |
121 return web_contents_.get(); | |
122 } | |
123 | |
124 void AppWindowContentsImpl::OnRequest( | |
125 const ExtensionHostMsg_Request_Params& params) { | |
126 extension_function_dispatcher_->Dispatch( | |
127 params, web_contents_->GetRenderViewHost()); | |
128 } | |
129 | |
130 void AppWindowContentsImpl::UpdateDraggableRegions( | |
131 const std::vector<extensions::DraggableRegion>& regions) { | |
132 host_->UpdateDraggableRegions(regions); | |
133 } | |
134 | |
135 void AppWindowContentsImpl::SuspendRenderViewHost( | |
136 content::RenderViewHost* rvh) { | |
137 DCHECK(rvh); | |
138 content::BrowserThread::PostTask( | |
139 content::BrowserThread::IO, FROM_HERE, | |
140 base::Bind(&content::ResourceDispatcherHost::BlockRequestsForRoute, | |
141 base::Unretained(content::ResourceDispatcherHost::Get()), | |
142 rvh->GetProcess()->GetID(), rvh->GetRoutingID())); | |
143 } | |
144 | |
145 } // namespace apps | |
OLD | NEW |