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/shell/browser/browser_plugin/test_guest_manager_delegate.h" | 5 #include "content/shell/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" |
| 10 #include "base/values.h" |
9 #include "content/public/browser/site_instance.h" | 11 #include "content/public/browser/site_instance.h" |
10 #include "content/public/browser/web_contents.h" | 12 #include "content/public/browser/web_contents.h" |
| 13 #include "content/public/browser/web_contents_observer.h" |
| 14 #include "content/public/common/url_constants.h" |
| 15 #include "net/base/escape.h" |
11 | 16 |
12 namespace content { | 17 namespace content { |
13 | 18 |
| 19 class GuestWebContentsObserver |
| 20 : public content::WebContentsObserver { |
| 21 public: |
| 22 explicit GuestWebContentsObserver(WebContents* guest_web_contents) |
| 23 : WebContentsObserver(guest_web_contents), |
| 24 guest_instance_id_(guest_web_contents->GetEmbeddedInstanceID()) { |
| 25 } |
| 26 |
| 27 virtual ~GuestWebContentsObserver() { |
| 28 } |
| 29 |
| 30 virtual void WebContentsDestroyed(WebContents* web_contents) OVERRIDE { |
| 31 TestGuestManagerDelegate::GetInstance()->RemoveGuest(guest_instance_id_); |
| 32 delete this; |
| 33 } |
| 34 |
| 35 private: |
| 36 int guest_instance_id_; |
| 37 DISALLOW_COPY_AND_ASSIGN(GuestWebContentsObserver); |
| 38 }; |
| 39 |
14 TestGuestManagerDelegate::TestGuestManagerDelegate() | 40 TestGuestManagerDelegate::TestGuestManagerDelegate() |
15 : next_instance_id_(0) { | 41 : next_instance_id_(0) { |
16 } | 42 } |
17 | 43 |
18 TestGuestManagerDelegate::~TestGuestManagerDelegate() { | 44 TestGuestManagerDelegate::~TestGuestManagerDelegate() { |
19 } | 45 } |
20 | 46 |
21 // static. | 47 // static. |
22 TestGuestManagerDelegate* TestGuestManagerDelegate::GetInstance() { | 48 TestGuestManagerDelegate* TestGuestManagerDelegate::GetInstance() { |
23 return Singleton<TestGuestManagerDelegate>::get(); | 49 return Singleton<TestGuestManagerDelegate>::get(); |
24 } | 50 } |
25 | 51 |
| 52 content::WebContents* TestGuestManagerDelegate::CreateGuest( |
| 53 SiteInstance* embedder_site_instance, |
| 54 int instance_id, |
| 55 const StorageInfo& storage_info, |
| 56 scoped_ptr<base::DictionaryValue> extra_params) { |
| 57 const GURL& embedder_site_url = embedder_site_instance->GetSiteURL(); |
| 58 const std::string& host = embedder_site_url.host(); |
| 59 |
| 60 std::string url_encoded_partition = net::EscapeQueryParamValue( |
| 61 storage_info.partition_id, false); |
| 62 GURL guest_site(base::StringPrintf("%s://%s/%s?%s", |
| 63 content::kGuestScheme, |
| 64 host.c_str(), |
| 65 storage_info.persist ? "persist" : "", |
| 66 url_encoded_partition.c_str())); |
| 67 |
| 68 // If we already have a webview tag in the same app using the same storage |
| 69 // partition, we should use the same SiteInstance so the existing tag and |
| 70 // the new tag can script each other. |
| 71 SiteInstance* guest_site_instance = GetGuestSiteInstance(guest_site); |
| 72 if (!guest_site_instance) { |
| 73 // Create the SiteInstance in a new BrowsingInstance, which will ensure |
| 74 // that webview tags are also not allowed to send messages across |
| 75 // different partitions. |
| 76 guest_site_instance = SiteInstance::CreateForURL( |
| 77 embedder_site_instance->GetBrowserContext(), guest_site); |
| 78 } |
| 79 WebContents::CreateParams create_params( |
| 80 embedder_site_instance->GetBrowserContext(), |
| 81 guest_site_instance); |
| 82 create_params.guest_instance_id = instance_id; |
| 83 create_params.guest_extra_params.reset(extra_params.release()); |
| 84 WebContents* guest_web_contents = WebContents::Create(create_params); |
| 85 AddGuest(instance_id, guest_web_contents); |
| 86 return guest_web_contents; |
| 87 } |
| 88 |
26 int TestGuestManagerDelegate::GetNextInstanceID() { | 89 int TestGuestManagerDelegate::GetNextInstanceID() { |
27 return ++next_instance_id_; | 90 return ++next_instance_id_; |
28 } | 91 } |
29 | 92 |
30 void TestGuestManagerDelegate::AddGuest( | 93 void TestGuestManagerDelegate::AddGuest( |
31 int guest_instance_id, | 94 int guest_instance_id, |
32 WebContents* guest_web_contents) { | 95 WebContents* guest_web_contents) { |
33 DCHECK(guest_web_contents_by_instance_id_.find(guest_instance_id) == | 96 DCHECK(guest_web_contents_by_instance_id_.find(guest_instance_id) == |
34 guest_web_contents_by_instance_id_.end()); | 97 guest_web_contents_by_instance_id_.end()); |
35 guest_web_contents_by_instance_id_[guest_instance_id] = guest_web_contents; | 98 guest_web_contents_by_instance_id_[guest_instance_id] = guest_web_contents; |
| 99 new GuestWebContentsObserver(guest_web_contents); |
36 } | 100 } |
37 | 101 |
38 void TestGuestManagerDelegate::RemoveGuest( | 102 void TestGuestManagerDelegate::RemoveGuest( |
39 int guest_instance_id) { | 103 int guest_instance_id) { |
40 GuestInstanceMap::iterator it = | 104 GuestInstanceMap::iterator it = |
41 guest_web_contents_by_instance_id_.find(guest_instance_id); | 105 guest_web_contents_by_instance_id_.find(guest_instance_id); |
42 DCHECK(it != guest_web_contents_by_instance_id_.end()); | 106 DCHECK(it != guest_web_contents_by_instance_id_.end()); |
43 guest_web_contents_by_instance_id_.erase(it); | 107 guest_web_contents_by_instance_id_.erase(it); |
44 } | 108 } |
45 | 109 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 if (embedder_web_contents != guest->GetEmbedderWebContents()) | 141 if (embedder_web_contents != guest->GetEmbedderWebContents()) |
78 continue; | 142 continue; |
79 | 143 |
80 if (callback.Run(guest)) | 144 if (callback.Run(guest)) |
81 return true; | 145 return true; |
82 } | 146 } |
83 return false; | 147 return false; |
84 } | 148 } |
85 | 149 |
86 } // namespace content | 150 } // namespace content |
OLD | NEW |