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