OLD | NEW |
| (Empty) |
1 // Copyright 2014 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/test_guest_manager_delegate.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "base/memory/singleton.h" | |
9 #include "base/strings/stringprintf.h" | |
10 #include "base/values.h" | |
11 #include "content/browser/web_contents/web_contents_impl.h" | |
12 #include "content/public/browser/site_instance.h" | |
13 #include "content/public/browser/web_contents.h" | |
14 #include "content/public/browser/web_contents_observer.h" | |
15 #include "content/public/common/url_constants.h" | |
16 #include "content/public/test/test_utils.h" | |
17 #include "net/base/escape.h" | |
18 | |
19 namespace content { | |
20 | |
21 class GuestWebContentsObserver | |
22 : public content::WebContentsObserver { | |
23 public: | |
24 explicit GuestWebContentsObserver(WebContents* guest_web_contents) | |
25 : WebContentsObserver(guest_web_contents), | |
26 guest_instance_id_(guest_web_contents->GetEmbeddedInstanceID()) { | |
27 } | |
28 | |
29 virtual ~GuestWebContentsObserver() { | |
30 } | |
31 | |
32 virtual void WebContentsDestroyed() OVERRIDE { | |
33 TestGuestManagerDelegate::GetInstance()->RemoveGuest(guest_instance_id_); | |
34 delete this; | |
35 } | |
36 | |
37 private: | |
38 int guest_instance_id_; | |
39 DISALLOW_COPY_AND_ASSIGN(GuestWebContentsObserver); | |
40 }; | |
41 | |
42 TestGuestManagerDelegate::TestGuestManagerDelegate() | |
43 : last_guest_added_(NULL), | |
44 next_instance_id_(0) { | |
45 } | |
46 | |
47 TestGuestManagerDelegate::~TestGuestManagerDelegate() { | |
48 } | |
49 | |
50 // static. | |
51 TestGuestManagerDelegate* TestGuestManagerDelegate::GetInstance() { | |
52 return Singleton<TestGuestManagerDelegate>::get(); | |
53 } | |
54 | |
55 WebContentsImpl* TestGuestManagerDelegate::WaitForGuestAdded() { | |
56 // Check if guests were already created. | |
57 if (last_guest_added_) { | |
58 WebContentsImpl* last_guest_added = last_guest_added_; | |
59 last_guest_added_ = NULL; | |
60 return last_guest_added; | |
61 } | |
62 // Wait otherwise. | |
63 message_loop_runner_ = new MessageLoopRunner(); | |
64 message_loop_runner_->Run(); | |
65 WebContentsImpl* last_guest_added = last_guest_added_; | |
66 last_guest_added_ = NULL; | |
67 return last_guest_added; | |
68 } | |
69 | |
70 content::WebContents* TestGuestManagerDelegate::CreateGuest( | |
71 SiteInstance* embedder_site_instance, | |
72 int instance_id, | |
73 const std::string& storage_partition_id, | |
74 bool persist_storage, | |
75 scoped_ptr<base::DictionaryValue> extra_params) { | |
76 const GURL& embedder_site_url = embedder_site_instance->GetSiteURL(); | |
77 const std::string& host = embedder_site_url.host(); | |
78 | |
79 std::string url_encoded_partition = net::EscapeQueryParamValue( | |
80 storage_partition_id, false); | |
81 GURL guest_site(base::StringPrintf("%s://%s/%s?%s", | |
82 content::kGuestScheme, | |
83 host.c_str(), | |
84 persist_storage ? "persist" : "", | |
85 url_encoded_partition.c_str())); | |
86 | |
87 // If we already have a webview tag in the same app using the same storage | |
88 // partition, we should use the same SiteInstance so the existing tag and | |
89 // the new tag can script each other. | |
90 SiteInstance* guest_site_instance = GetGuestSiteInstance(guest_site); | |
91 if (!guest_site_instance) { | |
92 // Create the SiteInstance in a new BrowsingInstance, which will ensure | |
93 // that webview tags are also not allowed to send messages across | |
94 // different partitions. | |
95 guest_site_instance = SiteInstance::CreateForURL( | |
96 embedder_site_instance->GetBrowserContext(), guest_site); | |
97 } | |
98 WebContents::CreateParams create_params( | |
99 embedder_site_instance->GetBrowserContext(), | |
100 guest_site_instance); | |
101 create_params.guest_instance_id = instance_id; | |
102 create_params.guest_extra_params.reset(extra_params.release()); | |
103 WebContents* guest_web_contents = WebContents::Create(create_params); | |
104 AddGuest(instance_id, guest_web_contents); | |
105 return guest_web_contents; | |
106 } | |
107 | |
108 int TestGuestManagerDelegate::GetNextInstanceID() { | |
109 return ++next_instance_id_; | |
110 } | |
111 | |
112 void TestGuestManagerDelegate::AddGuest( | |
113 int guest_instance_id, | |
114 WebContents* guest_web_contents) { | |
115 DCHECK(guest_web_contents_by_instance_id_.find(guest_instance_id) == | |
116 guest_web_contents_by_instance_id_.end()); | |
117 guest_web_contents_by_instance_id_[guest_instance_id] = guest_web_contents; | |
118 new GuestWebContentsObserver(guest_web_contents); | |
119 last_guest_added_ = static_cast<WebContentsImpl*>(guest_web_contents); | |
120 if (message_loop_runner_) | |
121 message_loop_runner_->Quit(); | |
122 } | |
123 | |
124 void TestGuestManagerDelegate::RemoveGuest( | |
125 int guest_instance_id) { | |
126 GuestInstanceMap::iterator it = | |
127 guest_web_contents_by_instance_id_.find(guest_instance_id); | |
128 DCHECK(it != guest_web_contents_by_instance_id_.end()); | |
129 guest_web_contents_by_instance_id_.erase(it); | |
130 } | |
131 | |
132 void TestGuestManagerDelegate::MaybeGetGuestByInstanceIDOrKill( | |
133 int guest_instance_id, | |
134 int embedder_render_process_id, | |
135 const GuestByInstanceIDCallback& callback) { | |
136 GuestInstanceMap::const_iterator it = | |
137 guest_web_contents_by_instance_id_.find(guest_instance_id); | |
138 if (it == guest_web_contents_by_instance_id_.end()) { | |
139 callback.Run(NULL); | |
140 return; | |
141 } | |
142 callback.Run(it->second); | |
143 } | |
144 | |
145 SiteInstance* TestGuestManagerDelegate::GetGuestSiteInstance( | |
146 const GURL& guest_site) { | |
147 for (GuestInstanceMap::const_iterator it = | |
148 guest_web_contents_by_instance_id_.begin(); | |
149 it != guest_web_contents_by_instance_id_.end(); ++it) { | |
150 if (it->second->GetSiteInstance()->GetSiteURL() == guest_site) | |
151 return it->second->GetSiteInstance(); | |
152 } | |
153 return NULL; | |
154 } | |
155 | |
156 bool TestGuestManagerDelegate::ForEachGuest( | |
157 WebContents* embedder_web_contents, | |
158 const GuestCallback& callback) { | |
159 for (GuestInstanceMap::iterator it = | |
160 guest_web_contents_by_instance_id_.begin(); | |
161 it != guest_web_contents_by_instance_id_.end(); ++it) { | |
162 WebContents* guest = it->second; | |
163 if (embedder_web_contents != guest->GetEmbedderWebContents()) | |
164 continue; | |
165 | |
166 if (callback.Run(guest)) | |
167 return true; | |
168 } | |
169 return false; | |
170 } | |
171 | |
172 } // namespace content | |
OLD | NEW |