Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(385)

Side by Side Diff: content/browser/browser_plugin/test_guest_manager_delegate.cc

Issue 261013005: BrowserPlugin: Move CreateGuest to chrome (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@guestview_manager_simplify_api
Patch Set: Fixed content_browsertests after a change Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
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() 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 std::string& storage_partition_id,
56 bool persist_storage,
57 scoped_ptr<base::DictionaryValue> extra_params) {
58 const GURL& embedder_site_url = embedder_site_instance->GetSiteURL();
59 const std::string& host = embedder_site_url.host();
60
61 std::string url_encoded_partition = net::EscapeQueryParamValue(
62 storage_partition_id, false);
63 GURL guest_site(base::StringPrintf("%s://%s/%s?%s",
64 content::kGuestScheme,
65 host.c_str(),
66 persist_storage ? "persist" : "",
67 url_encoded_partition.c_str()));
68
69 // If we already have a webview tag in the same app using the same storage
70 // partition, we should use the same SiteInstance so the existing tag and
71 // the new tag can script each other.
72 SiteInstance* guest_site_instance = GetGuestSiteInstance(guest_site);
73 if (!guest_site_instance) {
74 // Create the SiteInstance in a new BrowsingInstance, which will ensure
75 // that webview tags are also not allowed to send messages across
76 // different partitions.
77 guest_site_instance = SiteInstance::CreateForURL(
78 embedder_site_instance->GetBrowserContext(), guest_site);
79 }
80 WebContents::CreateParams create_params(
81 embedder_site_instance->GetBrowserContext(),
82 guest_site_instance);
83 create_params.guest_instance_id = instance_id;
84 create_params.guest_extra_params.reset(extra_params.release());
85 WebContents* guest_web_contents = WebContents::Create(create_params);
86 AddGuest(instance_id, guest_web_contents);
87 return guest_web_contents;
88 }
89
26 int TestGuestManagerDelegate::GetNextInstanceID() { 90 int TestGuestManagerDelegate::GetNextInstanceID() {
27 return ++next_instance_id_; 91 return ++next_instance_id_;
28 } 92 }
29 93
30 void TestGuestManagerDelegate::AddGuest( 94 void TestGuestManagerDelegate::AddGuest(
31 int guest_instance_id, 95 int guest_instance_id,
32 WebContents* guest_web_contents) { 96 WebContents* guest_web_contents) {
33 DCHECK(guest_web_contents_by_instance_id_.find(guest_instance_id) == 97 DCHECK(guest_web_contents_by_instance_id_.find(guest_instance_id) ==
34 guest_web_contents_by_instance_id_.end()); 98 guest_web_contents_by_instance_id_.end());
35 guest_web_contents_by_instance_id_[guest_instance_id] = guest_web_contents; 99 guest_web_contents_by_instance_id_[guest_instance_id] = guest_web_contents;
100 new GuestWebContentsObserver(guest_web_contents);
36 } 101 }
37 102
38 void TestGuestManagerDelegate::RemoveGuest( 103 void TestGuestManagerDelegate::RemoveGuest(
39 int guest_instance_id) { 104 int guest_instance_id) {
40 GuestInstanceMap::iterator it = 105 GuestInstanceMap::iterator it =
41 guest_web_contents_by_instance_id_.find(guest_instance_id); 106 guest_web_contents_by_instance_id_.find(guest_instance_id);
42 DCHECK(it != guest_web_contents_by_instance_id_.end()); 107 DCHECK(it != guest_web_contents_by_instance_id_.end());
43 guest_web_contents_by_instance_id_.erase(it); 108 guest_web_contents_by_instance_id_.erase(it);
44 } 109 }
45 110
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 if (embedder_web_contents != guest->GetEmbedderWebContents()) 142 if (embedder_web_contents != guest->GetEmbedderWebContents())
78 continue; 143 continue;
79 144
80 if (callback.Run(guest)) 145 if (callback.Run(guest))
81 return true; 146 return true;
82 } 147 }
83 return false; 148 return false;
84 } 149 }
85 150
86 } // namespace content 151 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/browser_plugin/test_guest_manager_delegate.h ('k') | content/browser/web_contents/web_contents_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698