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/render_process_host.h" | |
18 #include "content/public/browser/user_metrics.h" | |
19 #include "content/public/common/content_client.h" | |
20 #include "content/public/common/result_codes.h" | |
21 #include "content/public/common/url_constants.h" | |
22 #include "content/public/common/url_utils.h" | |
23 #include "net/base/escape.h" | |
24 | |
25 namespace content { | |
26 | |
27 // static | |
28 BrowserPluginHostFactory* BrowserPluginGuestManager::factory_ = NULL; | |
29 | |
30 BrowserPluginGuestManager::BrowserPluginGuestManager(BrowserContext* context) | |
31 : context_(context) {} | |
32 | |
33 BrowserPluginGuestManagerDelegate* | |
34 BrowserPluginGuestManager::GetDelegate() const { | |
35 return context_->GetGuestManagerDelegate(); | |
36 } | |
37 | |
38 BrowserPluginGuestManager::~BrowserPluginGuestManager() { | |
39 } | |
40 | |
41 // static. | |
42 BrowserPluginGuestManager* BrowserPluginGuestManager::FromBrowserContext( | |
43 BrowserContext* context) { | |
44 BrowserPluginGuestManager* guest_manager = | |
45 static_cast<BrowserPluginGuestManager*>( | |
46 context->GetUserData( | |
47 browser_plugin::kBrowserPluginGuestManagerKeyName)); | |
48 if (!guest_manager) { | |
49 guest_manager = BrowserPluginGuestManager::Create(context); | |
50 context->SetUserData(browser_plugin::kBrowserPluginGuestManagerKeyName, | |
51 guest_manager); | |
52 } | |
53 return guest_manager; | |
54 } | |
55 | |
56 // static | |
57 BrowserPluginGuestManager* BrowserPluginGuestManager::Create( | |
58 BrowserContext* context) { | |
59 if (factory_) | |
60 return factory_->CreateBrowserPluginGuestManager(context); | |
61 return new BrowserPluginGuestManager(context); | |
62 } | |
63 | |
64 int BrowserPluginGuestManager::GetNextInstanceID() { | |
65 if (!GetDelegate()) | |
66 return 0; | |
67 return GetDelegate()->GetNextInstanceID(); | |
68 } | |
69 | |
70 BrowserPluginGuest* BrowserPluginGuestManager::CreateGuest( | |
71 SiteInstance* embedder_site_instance, | |
72 int instance_id, | |
73 const StorageInfo& storage_info, | |
74 scoped_ptr<base::DictionaryValue> extra_params) { | |
75 if (!GetDelegate()) | |
76 return NULL; | |
77 WebContents* guest_web_contents = | |
78 GetDelegate()->CreateGuest(embedder_site_instance, | |
79 instance_id, | |
80 storage_info, | |
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 return; | |
104 | |
105 GetDelegate()->MaybeGetGuestByInstanceIDOrKill( | |
106 instance_id, | |
107 embedder_render_process_id, | |
108 base::Bind(&BrowserPluginGuestByInstanceIDCallback, | |
109 callback)); | |
110 } | |
111 | |
112 static void BrowserPluginGuestMessageCallback(const IPC::Message& message, | |
113 BrowserPluginGuest* guest) { | |
114 if (!guest) | |
115 return; | |
116 guest->OnMessageReceivedFromEmbedder(message); | |
117 } | |
118 | |
119 void BrowserPluginGuestManager::OnMessageReceived(const IPC::Message& message, | |
120 int render_process_id) { | |
121 DCHECK(BrowserPluginGuest::ShouldForwardToBrowserPluginGuest(message)); | |
122 int instance_id = 0; | |
123 // All allowed messages must have instance_id as their first parameter. | |
124 PickleIterator iter(message); | |
125 bool success = iter.ReadInt(&instance_id); | |
126 DCHECK(success); | |
127 MaybeGetGuestByInstanceIDOrKill(instance_id, | |
128 render_process_id, | |
129 base::Bind(&BrowserPluginGuestMessageCallback, | |
130 message)); | |
131 } | |
132 | |
133 static bool BrowserPluginGuestCallback( | |
134 const BrowserPluginGuestManager::GuestCallback& callback, | |
135 WebContents* guest_web_contents) { | |
136 return callback.Run(static_cast<WebContentsImpl*>(guest_web_contents) | |
137 ->GetBrowserPluginGuest()); | |
138 } | |
139 | |
140 bool BrowserPluginGuestManager::ForEachGuest( | |
141 WebContents* embedder_web_contents, const GuestCallback& callback) { | |
142 if (!GetDelegate()) | |
143 return false; | |
144 return GetDelegate()->ForEachGuest(embedder_web_contents, | |
145 base::Bind(&BrowserPluginGuestCallback, | |
146 callback)); | |
147 } | |
148 | |
149 } // namespace content | |
OLD | NEW |