| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/browser/browser_plugin/browser_plugin_guest_manager.h" | |
| 6 | |
| 7 #include "content/browser/browser_plugin/browser_plugin_guest.h" | |
| 8 #include "content/browser/browser_plugin/browser_plugin_host_factory.h" | |
| 9 #include "content/browser/renderer_host/render_view_host_impl.h" | |
| 10 #include "content/browser/web_contents/web_contents_impl.h" | |
| 11 #include "content/common/browser_plugin/browser_plugin_constants.h" | |
| 12 #include "content/common/browser_plugin/browser_plugin_messages.h" | |
| 13 #include "content/common/content_export.h" | |
| 14 #include "content/public/browser/browser_context.h" | |
| 15 #include "content/public/browser/browser_plugin_guest_manager_delegate.h" | |
| 16 #include "content/public/browser/content_browser_client.h" | |
| 17 #include "content/public/browser/user_metrics.h" | |
| 18 #include "content/public/common/content_client.h" | |
| 19 #include "content/public/common/result_codes.h" | |
| 20 #include "content/public/common/url_constants.h" | |
| 21 #include "content/public/common/url_utils.h" | |
| 22 | |
| 23 namespace content { | |
| 24 | |
| 25 // static | |
| 26 BrowserPluginHostFactory* BrowserPluginGuestManager::factory_ = NULL; | |
| 27 | |
| 28 BrowserPluginGuestManager::BrowserPluginGuestManager(BrowserContext* context) | |
| 29 : context_(context) {} | |
| 30 | |
| 31 BrowserPluginGuestManagerDelegate* | |
| 32 BrowserPluginGuestManager::GetDelegate() const { | |
| 33 return context_->GetGuestManagerDelegate(); | |
| 34 } | |
| 35 | |
| 36 BrowserPluginGuestManager::~BrowserPluginGuestManager() { | |
| 37 } | |
| 38 | |
| 39 // static. | |
| 40 BrowserPluginGuestManager* BrowserPluginGuestManager::FromBrowserContext( | |
| 41 BrowserContext* context) { | |
| 42 BrowserPluginGuestManager* guest_manager = | |
| 43 static_cast<BrowserPluginGuestManager*>( | |
| 44 context->GetUserData( | |
| 45 browser_plugin::kBrowserPluginGuestManagerKeyName)); | |
| 46 if (!guest_manager) { | |
| 47 guest_manager = BrowserPluginGuestManager::Create(context); | |
| 48 context->SetUserData(browser_plugin::kBrowserPluginGuestManagerKeyName, | |
| 49 guest_manager); | |
| 50 } | |
| 51 return guest_manager; | |
| 52 } | |
| 53 | |
| 54 // static | |
| 55 BrowserPluginGuestManager* BrowserPluginGuestManager::Create( | |
| 56 BrowserContext* context) { | |
| 57 if (factory_) | |
| 58 return factory_->CreateBrowserPluginGuestManager(context); | |
| 59 return new BrowserPluginGuestManager(context); | |
| 60 } | |
| 61 | |
| 62 int BrowserPluginGuestManager::GetNextInstanceID() { | |
| 63 if (!GetDelegate()) | |
| 64 return 0; | |
| 65 return GetDelegate()->GetNextInstanceID(); | |
| 66 } | |
| 67 | |
| 68 BrowserPluginGuest* BrowserPluginGuestManager::CreateGuest( | |
| 69 SiteInstance* embedder_site_instance, | |
| 70 int instance_id, | |
| 71 const std::string& storage_partition_id, | |
| 72 bool persist_storage, | |
| 73 scoped_ptr<base::DictionaryValue> extra_params) { | |
| 74 if (!GetDelegate()) | |
| 75 return NULL; | |
| 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()); | |
| 82 | |
| 83 return static_cast<WebContentsImpl*>(guest_web_contents)-> | |
| 84 GetBrowserPluginGuest(); | |
| 85 } | |
| 86 | |
| 87 static void BrowserPluginGuestByInstanceIDCallback( | |
| 88 const BrowserPluginGuestManager::GuestByInstanceIDCallback& callback, | |
| 89 WebContents* guest_web_contents) { | |
| 90 if (!guest_web_contents) { | |
| 91 callback.Run(NULL); | |
| 92 return; | |
| 93 } | |
| 94 callback.Run(static_cast<WebContentsImpl*>(guest_web_contents)-> | |
| 95 GetBrowserPluginGuest()); | |
| 96 } | |
| 97 | |
| 98 void BrowserPluginGuestManager::MaybeGetGuestByInstanceIDOrKill( | |
| 99 int instance_id, | |
| 100 int embedder_render_process_id, | |
| 101 const GuestByInstanceIDCallback& callback) const { | |
| 102 if (!GetDelegate()) { | |
| 103 callback.Run(NULL); | |
| 104 return; | |
| 105 } | |
| 106 | |
| 107 GetDelegate()->MaybeGetGuestByInstanceIDOrKill( | |
| 108 instance_id, | |
| 109 embedder_render_process_id, | |
| 110 base::Bind(&BrowserPluginGuestByInstanceIDCallback, | |
| 111 callback)); | |
| 112 } | |
| 113 | |
| 114 static void BrowserPluginGuestMessageCallback(const IPC::Message& message, | |
| 115 BrowserPluginGuest* guest) { | |
| 116 if (!guest) | |
| 117 return; | |
| 118 guest->OnMessageReceivedFromEmbedder(message); | |
| 119 } | |
| 120 | |
| 121 void BrowserPluginGuestManager::OnMessageReceived(const IPC::Message& message, | |
| 122 int render_process_id) { | |
| 123 DCHECK(BrowserPluginGuest::ShouldForwardToBrowserPluginGuest(message)); | |
| 124 int instance_id = 0; | |
| 125 // All allowed messages must have instance_id as their first parameter. | |
| 126 PickleIterator iter(message); | |
| 127 bool success = iter.ReadInt(&instance_id); | |
| 128 DCHECK(success); | |
| 129 MaybeGetGuestByInstanceIDOrKill(instance_id, | |
| 130 render_process_id, | |
| 131 base::Bind(&BrowserPluginGuestMessageCallback, | |
| 132 message)); | |
| 133 } | |
| 134 | |
| 135 static bool BrowserPluginGuestCallback( | |
| 136 const BrowserPluginGuestManager::GuestCallback& callback, | |
| 137 WebContents* guest_web_contents) { | |
| 138 return callback.Run(static_cast<WebContentsImpl*>(guest_web_contents) | |
| 139 ->GetBrowserPluginGuest()); | |
| 140 } | |
| 141 | |
| 142 bool BrowserPluginGuestManager::ForEachGuest( | |
| 143 WebContents* embedder_web_contents, const GuestCallback& callback) { | |
| 144 if (!GetDelegate()) | |
| 145 return false; | |
| 146 return GetDelegate()->ForEachGuest(embedder_web_contents, | |
| 147 base::Bind(&BrowserPluginGuestCallback, | |
| 148 callback)); | |
| 149 } | |
| 150 | |
| 151 } // namespace content | |
| OLD | NEW |