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_delegate.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/public/browser/site_instance.h" | 12 #include "content/public/browser/site_instance.h" |
12 #include "content/public/browser/web_contents.h" | 13 #include "content/public/browser/web_contents.h" |
13 #include "content/public/browser/web_contents_observer.h" | 14 #include "content/public/browser/web_contents_observer.h" |
14 #include "content/public/common/url_constants.h" | 15 #include "content/public/common/url_constants.h" |
| 16 #include "content/public/test/test_utils.h" |
15 #include "net/base/escape.h" | 17 #include "net/base/escape.h" |
16 | 18 |
17 namespace content { | 19 namespace content { |
18 | 20 |
19 class GuestWebContentsObserver | 21 class GuestWebContentsObserver |
20 : public content::WebContentsObserver { | 22 : public content::WebContentsObserver { |
21 public: | 23 public: |
22 explicit GuestWebContentsObserver(WebContents* guest_web_contents) | 24 explicit GuestWebContentsObserver(WebContents* guest_web_contents) |
23 : WebContentsObserver(guest_web_contents), | 25 : WebContentsObserver(guest_web_contents), |
24 guest_instance_id_(guest_web_contents->GetEmbeddedInstanceID()) { | 26 guest_instance_id_(guest_web_contents->GetEmbeddedInstanceID()) { |
25 } | 27 } |
26 | 28 |
27 virtual ~GuestWebContentsObserver() { | 29 virtual ~GuestWebContentsObserver() { |
28 } | 30 } |
29 | 31 |
30 virtual void WebContentsDestroyed(WebContents* web_contents) OVERRIDE { | 32 virtual void WebContentsDestroyed(WebContents* web_contents) OVERRIDE { |
31 TestGuestManagerDelegate::GetInstance()->RemoveGuest(guest_instance_id_); | 33 TestGuestManagerDelegate::GetInstance()->RemoveGuest(guest_instance_id_); |
32 delete this; | 34 delete this; |
33 } | 35 } |
34 | 36 |
35 private: | 37 private: |
36 int guest_instance_id_; | 38 int guest_instance_id_; |
37 DISALLOW_COPY_AND_ASSIGN(GuestWebContentsObserver); | 39 DISALLOW_COPY_AND_ASSIGN(GuestWebContentsObserver); |
38 }; | 40 }; |
39 | 41 |
40 TestGuestManagerDelegate::TestGuestManagerDelegate() | 42 TestGuestManagerDelegate::TestGuestManagerDelegate() |
41 : next_instance_id_(0) { | 43 : last_guest_added_(NULL), |
| 44 next_instance_id_(0) { |
42 } | 45 } |
43 | 46 |
44 TestGuestManagerDelegate::~TestGuestManagerDelegate() { | 47 TestGuestManagerDelegate::~TestGuestManagerDelegate() { |
45 } | 48 } |
46 | 49 |
47 // static. | 50 // static. |
48 TestGuestManagerDelegate* TestGuestManagerDelegate::GetInstance() { | 51 TestGuestManagerDelegate* TestGuestManagerDelegate::GetInstance() { |
49 return Singleton<TestGuestManagerDelegate>::get(); | 52 return Singleton<TestGuestManagerDelegate>::get(); |
50 } | 53 } |
51 | 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 |
52 content::WebContents* TestGuestManagerDelegate::CreateGuest( | 70 content::WebContents* TestGuestManagerDelegate::CreateGuest( |
53 SiteInstance* embedder_site_instance, | 71 SiteInstance* embedder_site_instance, |
54 int instance_id, | 72 int instance_id, |
55 const std::string& storage_partition_id, | 73 const std::string& storage_partition_id, |
56 bool persist_storage, | 74 bool persist_storage, |
57 scoped_ptr<base::DictionaryValue> extra_params) { | 75 scoped_ptr<base::DictionaryValue> extra_params) { |
58 const GURL& embedder_site_url = embedder_site_instance->GetSiteURL(); | 76 const GURL& embedder_site_url = embedder_site_instance->GetSiteURL(); |
59 const std::string& host = embedder_site_url.host(); | 77 const std::string& host = embedder_site_url.host(); |
60 | 78 |
61 std::string url_encoded_partition = net::EscapeQueryParamValue( | 79 std::string url_encoded_partition = net::EscapeQueryParamValue( |
(...skipping 29 matching lines...) Expand all Loading... |
91 return ++next_instance_id_; | 109 return ++next_instance_id_; |
92 } | 110 } |
93 | 111 |
94 void TestGuestManagerDelegate::AddGuest( | 112 void TestGuestManagerDelegate::AddGuest( |
95 int guest_instance_id, | 113 int guest_instance_id, |
96 WebContents* guest_web_contents) { | 114 WebContents* guest_web_contents) { |
97 DCHECK(guest_web_contents_by_instance_id_.find(guest_instance_id) == | 115 DCHECK(guest_web_contents_by_instance_id_.find(guest_instance_id) == |
98 guest_web_contents_by_instance_id_.end()); | 116 guest_web_contents_by_instance_id_.end()); |
99 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; |
100 new GuestWebContentsObserver(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(); |
101 } | 122 } |
102 | 123 |
103 void TestGuestManagerDelegate::RemoveGuest( | 124 void TestGuestManagerDelegate::RemoveGuest( |
104 int guest_instance_id) { | 125 int guest_instance_id) { |
105 GuestInstanceMap::iterator it = | 126 GuestInstanceMap::iterator it = |
106 guest_web_contents_by_instance_id_.find(guest_instance_id); | 127 guest_web_contents_by_instance_id_.find(guest_instance_id); |
107 DCHECK(it != guest_web_contents_by_instance_id_.end()); | 128 DCHECK(it != guest_web_contents_by_instance_id_.end()); |
108 guest_web_contents_by_instance_id_.erase(it); | 129 guest_web_contents_by_instance_id_.erase(it); |
109 } | 130 } |
110 | 131 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 if (embedder_web_contents != guest->GetEmbedderWebContents()) | 163 if (embedder_web_contents != guest->GetEmbedderWebContents()) |
143 continue; | 164 continue; |
144 | 165 |
145 if (callback.Run(guest)) | 166 if (callback.Run(guest)) |
146 return true; | 167 return true; |
147 } | 168 } |
148 return false; | 169 return false; |
149 } | 170 } |
150 | 171 |
151 } // namespace content | 172 } // namespace content |
OLD | NEW |