| 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 content::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(WebContents* web_contents) OVERRIDE { | 32 virtual void WebContentsDestroyed(WebContents* web_contents) 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 fprintf(stderr, ">>>%s\n", __PRETTY_FUNCTION__); | |
| 57 // Check if guests were already created. | 56 // Check if guests were already created. |
| 58 if (last_guest_added_) { | 57 if (last_guest_added_) { |
| 59 WebContentsImpl* last_guest_added = last_guest_added_; | 58 WebContentsImpl* last_guest_added = last_guest_added_; |
| 60 last_guest_added_ = NULL; | 59 last_guest_added_ = NULL; |
| 61 return last_guest_added; | 60 return last_guest_added; |
| 62 } | 61 } |
| 63 // Wait otherwise. | 62 // Wait otherwise. |
| 64 message_loop_runner_ = new MessageLoopRunner(); | 63 message_loop_runner_ = new MessageLoopRunner(); |
| 65 message_loop_runner_->Run(); | 64 message_loop_runner_->Run(); |
| 66 WebContentsImpl* last_guest_added = last_guest_added_; | 65 WebContentsImpl* last_guest_added = last_guest_added_; |
| 67 last_guest_added_ = NULL; | 66 last_guest_added_ = NULL; |
| 68 return last_guest_added; | 67 return last_guest_added; |
| 69 } | 68 } |
| 70 | 69 |
| 71 content::WebContents* TestGuestManagerDelegate::CreateGuest( | 70 content::WebContents* TestGuestManager::CreateGuest( |
| 72 SiteInstance* embedder_site_instance, | 71 SiteInstance* embedder_site_instance, |
| 73 int instance_id, | 72 int instance_id, |
| 74 const StorageInfo& storage_info, | 73 const StorageInfo& storage_info, |
| 75 scoped_ptr<base::DictionaryValue> extra_params) { | 74 scoped_ptr<base::DictionaryValue> extra_params) { |
| 76 const GURL& embedder_site_url = embedder_site_instance->GetSiteURL(); | 75 const GURL& embedder_site_url = embedder_site_instance->GetSiteURL(); |
| 77 const std::string& host = embedder_site_url.host(); | 76 const std::string& host = embedder_site_url.host(); |
| 78 | 77 |
| 79 std::string url_encoded_partition = net::EscapeQueryParamValue( | 78 std::string url_encoded_partition = net::EscapeQueryParamValue( |
| 80 storage_info.partition_id, false); | 79 storage_info.partition_id, false); |
| 81 GURL guest_site(base::StringPrintf("%s://%s/%s?%s", | 80 GURL guest_site(base::StringPrintf("%s://%s/%s?%s", |
| (...skipping 16 matching lines...) Expand all Loading... |
| 98 WebContents::CreateParams create_params( | 97 WebContents::CreateParams create_params( |
| 99 embedder_site_instance->GetBrowserContext(), | 98 embedder_site_instance->GetBrowserContext(), |
| 100 guest_site_instance); | 99 guest_site_instance); |
| 101 create_params.guest_instance_id = instance_id; | 100 create_params.guest_instance_id = instance_id; |
| 102 create_params.guest_extra_params.reset(extra_params.release()); | 101 create_params.guest_extra_params.reset(extra_params.release()); |
| 103 WebContents* guest_web_contents = WebContents::Create(create_params); | 102 WebContents* guest_web_contents = WebContents::Create(create_params); |
| 104 AddGuest(instance_id, guest_web_contents); | 103 AddGuest(instance_id, guest_web_contents); |
| 105 return guest_web_contents; | 104 return guest_web_contents; |
| 106 } | 105 } |
| 107 | 106 |
| 108 int TestGuestManagerDelegate::GetNextInstanceID() { | 107 int TestGuestManager::GetNextInstanceID() { |
| 109 return ++next_instance_id_; | 108 return ++next_instance_id_; |
| 110 } | 109 } |
| 111 | 110 |
| 112 void TestGuestManagerDelegate::AddGuest( | 111 void TestGuestManager::AddGuest( |
| 113 int guest_instance_id, | 112 int guest_instance_id, |
| 114 WebContents* guest_web_contents) { | 113 WebContents* guest_web_contents) { |
| 115 DCHECK(guest_web_contents_by_instance_id_.find(guest_instance_id) == | 114 DCHECK(guest_web_contents_by_instance_id_.find(guest_instance_id) == |
| 116 guest_web_contents_by_instance_id_.end()); | 115 guest_web_contents_by_instance_id_.end()); |
| 117 guest_web_contents_by_instance_id_[guest_instance_id] = guest_web_contents; | 116 guest_web_contents_by_instance_id_[guest_instance_id] = guest_web_contents; |
| 118 new GuestWebContentsObserver(guest_web_contents); | 117 new GuestWebContentsObserver(guest_web_contents); |
| 119 last_guest_added_ = static_cast<WebContentsImpl*>(guest_web_contents); | 118 last_guest_added_ = static_cast<WebContentsImpl*>(guest_web_contents); |
| 120 if (message_loop_runner_) | 119 if (message_loop_runner_) |
| 121 message_loop_runner_->Quit(); | 120 message_loop_runner_->Quit(); |
| 122 } | 121 } |
| 123 | 122 |
| 124 void TestGuestManagerDelegate::RemoveGuest( | 123 void TestGuestManager::RemoveGuest( |
| 125 int guest_instance_id) { | 124 int guest_instance_id) { |
| 126 GuestInstanceMap::iterator it = | 125 GuestInstanceMap::iterator it = |
| 127 guest_web_contents_by_instance_id_.find(guest_instance_id); | 126 guest_web_contents_by_instance_id_.find(guest_instance_id); |
| 128 DCHECK(it != guest_web_contents_by_instance_id_.end()); | 127 DCHECK(it != guest_web_contents_by_instance_id_.end()); |
| 129 guest_web_contents_by_instance_id_.erase(it); | 128 guest_web_contents_by_instance_id_.erase(it); |
| 130 } | 129 } |
| 131 | 130 |
| 132 void TestGuestManagerDelegate::MaybeGetGuestByInstanceIDOrKill( | 131 void TestGuestManager::MaybeGetGuestByInstanceIDOrKill( |
| 133 int guest_instance_id, | 132 int guest_instance_id, |
| 134 int embedder_render_process_id, | 133 int embedder_render_process_id, |
| 135 const GuestByInstanceIDCallback& callback) { | 134 const GuestByInstanceIDCallback& callback) { |
| 136 GuestInstanceMap::const_iterator it = | 135 GuestInstanceMap::const_iterator it = |
| 137 guest_web_contents_by_instance_id_.find(guest_instance_id); | 136 guest_web_contents_by_instance_id_.find(guest_instance_id); |
| 138 if (it == guest_web_contents_by_instance_id_.end()) { | 137 if (it == guest_web_contents_by_instance_id_.end()) { |
| 139 callback.Run(NULL); | 138 callback.Run(NULL); |
| 140 return; | 139 return; |
| 141 } | 140 } |
| 142 callback.Run(it->second); | 141 callback.Run(it->second); |
| 143 } | 142 } |
| 144 | 143 |
| 145 SiteInstance* TestGuestManagerDelegate::GetGuestSiteInstance( | 144 SiteInstance* TestGuestManager::GetGuestSiteInstance( |
| 146 const GURL& guest_site) { | 145 const GURL& guest_site) { |
| 147 for (GuestInstanceMap::const_iterator it = | 146 for (GuestInstanceMap::const_iterator it = |
| 148 guest_web_contents_by_instance_id_.begin(); | 147 guest_web_contents_by_instance_id_.begin(); |
| 149 it != guest_web_contents_by_instance_id_.end(); ++it) { | 148 it != guest_web_contents_by_instance_id_.end(); ++it) { |
| 150 if (it->second->GetSiteInstance()->GetSiteURL() == guest_site) | 149 if (it->second->GetSiteInstance()->GetSiteURL() == guest_site) |
| 151 return it->second->GetSiteInstance(); | 150 return it->second->GetSiteInstance(); |
| 152 } | 151 } |
| 153 return NULL; | 152 return NULL; |
| 154 } | 153 } |
| 155 | 154 |
| 156 bool TestGuestManagerDelegate::ForEachGuest( | 155 bool TestGuestManager::ForEachGuest( |
| 157 WebContents* embedder_web_contents, | 156 WebContents* embedder_web_contents, |
| 158 const GuestCallback& callback) { | 157 const GuestCallback& callback) { |
| 159 for (GuestInstanceMap::iterator it = | 158 for (GuestInstanceMap::iterator it = |
| 160 guest_web_contents_by_instance_id_.begin(); | 159 guest_web_contents_by_instance_id_.begin(); |
| 161 it != guest_web_contents_by_instance_id_.end(); ++it) { | 160 it != guest_web_contents_by_instance_id_.end(); ++it) { |
| 162 WebContents* guest = it->second; | 161 WebContents* guest = it->second; |
| 163 if (embedder_web_contents != guest->GetEmbedderWebContents()) | 162 if (embedder_web_contents != guest->GetEmbedderWebContents()) |
| 164 continue; | 163 continue; |
| 165 | 164 |
| 166 if (callback.Run(guest)) | 165 if (callback.Run(guest)) |
| 167 return true; | 166 return true; |
| 168 } | 167 } |
| 169 return false; | 168 return false; |
| 170 } | 169 } |
| 171 | 170 |
| 172 } // namespace content | 171 } // namespace content |
| OLD | NEW |