| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 "content/browser/browser_plugin/browser_plugin_guest_manager.h" | 5 #include "content/browser/browser_plugin/browser_plugin_guest_manager.h" |
| 6 | 6 |
| 7 #include "content/browser/browser_plugin/browser_plugin_guest.h" | 7 #include "content/browser/browser_plugin/browser_plugin_guest.h" |
| 8 #include "content/browser/browser_plugin/browser_plugin_host_factory.h" | 8 #include "content/browser/browser_plugin/browser_plugin_host_factory.h" |
| 9 #include "content/browser/renderer_host/render_view_host_impl.h" | 9 #include "content/browser/renderer_host/render_view_host_impl.h" |
| 10 #include "content/browser/web_contents/web_contents_impl.h" | 10 #include "content/browser/web_contents/web_contents_impl.h" |
| 11 #include "content/common/browser_plugin/browser_plugin_constants.h" | 11 #include "content/common/browser_plugin/browser_plugin_constants.h" |
| 12 #include "content/common/browser_plugin/browser_plugin_messages.h" | 12 #include "content/common/browser_plugin/browser_plugin_messages.h" |
| 13 #include "content/common/content_export.h" | 13 #include "content/common/content_export.h" |
| 14 #include "content/public/browser/browser_context.h" | 14 #include "content/public/browser/browser_context.h" |
| 15 #include "content/public/browser/browser_plugin_guest_manager_delegate.h" | 15 #include "content/public/browser/browser_plugin_guest_manager_delegate.h" |
| 16 #include "content/public/browser/content_browser_client.h" | 16 #include "content/public/browser/content_browser_client.h" |
| 17 #include "content/public/browser/render_process_host.h" | |
| 18 #include "content/public/browser/user_metrics.h" | 17 #include "content/public/browser/user_metrics.h" |
| 19 #include "content/public/common/content_client.h" | 18 #include "content/public/common/content_client.h" |
| 20 #include "content/public/common/result_codes.h" | 19 #include "content/public/common/result_codes.h" |
| 21 #include "content/public/common/url_constants.h" | 20 #include "content/public/common/url_constants.h" |
| 22 #include "content/public/common/url_utils.h" | 21 #include "content/public/common/url_utils.h" |
| 23 #include "net/base/escape.h" | |
| 24 | 22 |
| 25 namespace content { | 23 namespace content { |
| 26 | 24 |
| 27 // static | 25 // static |
| 28 BrowserPluginHostFactory* BrowserPluginGuestManager::factory_ = NULL; | 26 BrowserPluginHostFactory* BrowserPluginGuestManager::factory_ = NULL; |
| 29 | 27 |
| 30 BrowserPluginGuestManager::BrowserPluginGuestManager(BrowserContext* context) | 28 BrowserPluginGuestManager::BrowserPluginGuestManager(BrowserContext* context) |
| 31 : context_(context) {} | 29 : context_(context) {} |
| 32 | 30 |
| 33 BrowserPluginGuestManagerDelegate* | 31 BrowserPluginGuestManagerDelegate* |
| (...skipping 29 matching lines...) Expand all Loading... |
| 63 | 61 |
| 64 int BrowserPluginGuestManager::GetNextInstanceID() { | 62 int BrowserPluginGuestManager::GetNextInstanceID() { |
| 65 if (!GetDelegate()) | 63 if (!GetDelegate()) |
| 66 return 0; | 64 return 0; |
| 67 return GetDelegate()->GetNextInstanceID(); | 65 return GetDelegate()->GetNextInstanceID(); |
| 68 } | 66 } |
| 69 | 67 |
| 70 BrowserPluginGuest* BrowserPluginGuestManager::CreateGuest( | 68 BrowserPluginGuest* BrowserPluginGuestManager::CreateGuest( |
| 71 SiteInstance* embedder_site_instance, | 69 SiteInstance* embedder_site_instance, |
| 72 int instance_id, | 70 int instance_id, |
| 73 const BrowserPluginHostMsg_Attach_Params& params, | 71 const std::string& storage_partition_id, |
| 72 bool persist_storage, |
| 74 scoped_ptr<base::DictionaryValue> extra_params) { | 73 scoped_ptr<base::DictionaryValue> extra_params) { |
| 75 RenderProcessHost* embedder_process_host = | 74 if (!GetDelegate()) |
| 76 embedder_site_instance->GetProcess(); | |
| 77 // Validate that the partition id coming from the renderer is valid UTF-8, | |
| 78 // since we depend on this in other parts of the code, such as FilePath | |
| 79 // creation. If the validation fails, treat it as a bad message and kill the | |
| 80 // renderer process. | |
| 81 if (!base::IsStringUTF8(params.storage_partition_id)) { | |
| 82 content::RecordAction( | |
| 83 base::UserMetricsAction("BadMessageTerminate_BPGM")); | |
| 84 base::KillProcess( | |
| 85 embedder_process_host->GetHandle(), | |
| 86 content::RESULT_CODE_KILLED_BAD_MESSAGE, false); | |
| 87 return NULL; | 75 return NULL; |
| 88 } | 76 WebContents* guest_web_contents = |
| 77 GetDelegate()->CreateGuest(embedder_site_instance, |
| 78 instance_id, |
| 79 storage_partition_id, |
| 80 persist_storage, |
| 81 extra_params.Pass()); |
| 89 | 82 |
| 90 const GURL& embedder_site_url = embedder_site_instance->GetSiteURL(); | 83 return static_cast<WebContentsImpl*>(guest_web_contents)-> |
| 91 const std::string& host = embedder_site_url.host(); | 84 GetBrowserPluginGuest(); |
| 92 | |
| 93 std::string url_encoded_partition = net::EscapeQueryParamValue( | |
| 94 params.storage_partition_id, false); | |
| 95 // The SiteInstance of a given webview tag is based on the fact that it's | |
| 96 // a guest process in addition to which platform application the tag | |
| 97 // belongs to and what storage partition is in use, rather than the URL | |
| 98 // that the tag is being navigated to. | |
| 99 GURL guest_site(base::StringPrintf("%s://%s/%s?%s", | |
| 100 kGuestScheme, | |
| 101 host.c_str(), | |
| 102 params.persist_storage ? "persist" : "", | |
| 103 url_encoded_partition.c_str())); | |
| 104 | |
| 105 // If we already have a webview tag in the same app using the same storage | |
| 106 // partition, we should use the same SiteInstance so the existing tag and | |
| 107 // the new tag can script each other. | |
| 108 SiteInstance* guest_site_instance = GetGuestSiteInstance(guest_site); | |
| 109 if (!guest_site_instance) { | |
| 110 // Create the SiteInstance in a new BrowsingInstance, which will ensure | |
| 111 // that webview tags are also not allowed to send messages across | |
| 112 // different partitions. | |
| 113 guest_site_instance = SiteInstance::CreateForURL( | |
| 114 embedder_site_instance->GetBrowserContext(), guest_site); | |
| 115 } | |
| 116 | |
| 117 return WebContentsImpl::CreateGuest( | |
| 118 embedder_site_instance->GetBrowserContext(), | |
| 119 guest_site_instance, | |
| 120 instance_id, | |
| 121 extra_params.Pass()); | |
| 122 } | 85 } |
| 123 | 86 |
| 124 static void BrowserPluginGuestByInstanceIDCallback( | 87 static void BrowserPluginGuestByInstanceIDCallback( |
| 125 const BrowserPluginGuestManager::GuestByInstanceIDCallback& callback, | 88 const BrowserPluginGuestManager::GuestByInstanceIDCallback& callback, |
| 126 WebContents* guest_web_contents) { | 89 WebContents* guest_web_contents) { |
| 127 if (!guest_web_contents) { | 90 if (!guest_web_contents) { |
| 128 callback.Run(NULL); | 91 callback.Run(NULL); |
| 129 return; | 92 return; |
| 130 } | 93 } |
| 131 callback.Run(static_cast<WebContentsImpl*>(guest_web_contents)-> | 94 callback.Run(static_cast<WebContentsImpl*>(guest_web_contents)-> |
| 132 GetBrowserPluginGuest()); | 95 GetBrowserPluginGuest()); |
| 133 } | 96 } |
| 134 | 97 |
| 135 void BrowserPluginGuestManager::MaybeGetGuestByInstanceIDOrKill( | 98 void BrowserPluginGuestManager::MaybeGetGuestByInstanceIDOrKill( |
| 136 int instance_id, | 99 int instance_id, |
| 137 int embedder_render_process_id, | 100 int embedder_render_process_id, |
| 138 const GuestByInstanceIDCallback& callback) const { | 101 const GuestByInstanceIDCallback& callback) const { |
| 139 if (!GetDelegate()) { | 102 if (!GetDelegate()) { |
| 140 callback.Run(NULL); | 103 callback.Run(NULL); |
| 141 return; | 104 return; |
| 142 } | 105 } |
| 143 | 106 |
| 144 GetDelegate()->MaybeGetGuestByInstanceIDOrKill( | 107 GetDelegate()->MaybeGetGuestByInstanceIDOrKill( |
| 145 instance_id, | 108 instance_id, |
| 146 embedder_render_process_id, | 109 embedder_render_process_id, |
| 147 base::Bind(&BrowserPluginGuestByInstanceIDCallback, | 110 base::Bind(&BrowserPluginGuestByInstanceIDCallback, |
| 148 callback)); | 111 callback)); |
| 149 } | 112 } |
| 150 | 113 |
| 151 void BrowserPluginGuestManager::AddGuest(int instance_id, | |
| 152 WebContents* guest_web_contents) { | |
| 153 if (!GetDelegate()) | |
| 154 return; | |
| 155 GetDelegate()->AddGuest(instance_id, guest_web_contents); | |
| 156 } | |
| 157 | |
| 158 void BrowserPluginGuestManager::RemoveGuest(int instance_id) { | |
| 159 if (!GetDelegate()) | |
| 160 return; | |
| 161 GetDelegate()->RemoveGuest(instance_id); | |
| 162 } | |
| 163 | |
| 164 static void BrowserPluginGuestMessageCallback(const IPC::Message& message, | 114 static void BrowserPluginGuestMessageCallback(const IPC::Message& message, |
| 165 BrowserPluginGuest* guest) { | 115 BrowserPluginGuest* guest) { |
| 166 if (!guest) | 116 if (!guest) |
| 167 return; | 117 return; |
| 168 guest->OnMessageReceivedFromEmbedder(message); | 118 guest->OnMessageReceivedFromEmbedder(message); |
| 169 } | 119 } |
| 170 | 120 |
| 171 void BrowserPluginGuestManager::OnMessageReceived(const IPC::Message& message, | 121 void BrowserPluginGuestManager::OnMessageReceived(const IPC::Message& message, |
| 172 int render_process_id) { | 122 int render_process_id) { |
| 173 DCHECK(BrowserPluginGuest::ShouldForwardToBrowserPluginGuest(message)); | 123 DCHECK(BrowserPluginGuest::ShouldForwardToBrowserPluginGuest(message)); |
| 174 int instance_id = 0; | 124 int instance_id = 0; |
| 175 // All allowed messages must have instance_id as their first parameter. | 125 // All allowed messages must have instance_id as their first parameter. |
| 176 PickleIterator iter(message); | 126 PickleIterator iter(message); |
| 177 bool success = iter.ReadInt(&instance_id); | 127 bool success = iter.ReadInt(&instance_id); |
| 178 DCHECK(success); | 128 DCHECK(success); |
| 179 MaybeGetGuestByInstanceIDOrKill(instance_id, | 129 MaybeGetGuestByInstanceIDOrKill(instance_id, |
| 180 render_process_id, | 130 render_process_id, |
| 181 base::Bind(&BrowserPluginGuestMessageCallback, | 131 base::Bind(&BrowserPluginGuestMessageCallback, |
| 182 message)); | 132 message)); |
| 183 } | 133 } |
| 184 | 134 |
| 185 SiteInstance* BrowserPluginGuestManager::GetGuestSiteInstance( | |
| 186 const GURL& guest_site) { | |
| 187 if (!GetDelegate()) | |
| 188 return NULL; | |
| 189 return GetDelegate()->GetGuestSiteInstance(guest_site); | |
| 190 } | |
| 191 | |
| 192 static bool BrowserPluginGuestCallback( | 135 static bool BrowserPluginGuestCallback( |
| 193 const BrowserPluginGuestManager::GuestCallback& callback, | 136 const BrowserPluginGuestManager::GuestCallback& callback, |
| 194 WebContents* guest_web_contents) { | 137 WebContents* guest_web_contents) { |
| 195 return callback.Run(static_cast<WebContentsImpl*>(guest_web_contents) | 138 return callback.Run(static_cast<WebContentsImpl*>(guest_web_contents) |
| 196 ->GetBrowserPluginGuest()); | 139 ->GetBrowserPluginGuest()); |
| 197 } | 140 } |
| 198 | 141 |
| 199 bool BrowserPluginGuestManager::ForEachGuest( | 142 bool BrowserPluginGuestManager::ForEachGuest( |
| 200 WebContents* embedder_web_contents, const GuestCallback& callback) { | 143 WebContents* embedder_web_contents, const GuestCallback& callback) { |
| 201 if (!GetDelegate()) | 144 if (!GetDelegate()) |
| 202 return false; | 145 return false; |
| 203 return GetDelegate()->ForEachGuest(embedder_web_contents, | 146 return GetDelegate()->ForEachGuest(embedder_web_contents, |
| 204 base::Bind(&BrowserPluginGuestCallback, | 147 base::Bind(&BrowserPluginGuestCallback, |
| 205 callback)); | 148 callback)); |
| 206 } | 149 } |
| 207 | 150 |
| 208 } // namespace content | 151 } // namespace content |
| OLD | NEW |