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