| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/guest_view/guest_view_base.h" | |
| 6 | |
| 7 #include "base/lazy_instance.h" | |
| 8 #include "chrome/browser/guest_view/ad_view/ad_view_guest.h" | |
| 9 #include "chrome/browser/guest_view/guest_view_constants.h" | |
| 10 #include "chrome/browser/guest_view/web_view/web_view_guest.h" | |
| 11 #include "chrome/browser/profiles/profile.h" | |
| 12 #include "chrome/common/content_settings.h" | |
| 13 #include "content/public/browser/render_process_host.h" | |
| 14 #include "content/public/browser/web_contents.h" | |
| 15 #include "content/public/common/url_constants.h" | |
| 16 #include "extensions/browser/event_router.h" | |
| 17 #include "net/base/escape.h" | |
| 18 | |
| 19 using content::WebContents; | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 // <embedder_process_id, guest_instance_id> => GuestViewBase* | |
| 24 typedef std::map<std::pair<int, int>, GuestViewBase*> EmbedderGuestViewMap; | |
| 25 static base::LazyInstance<EmbedderGuestViewMap> embedder_guestview_map = | |
| 26 LAZY_INSTANCE_INITIALIZER; | |
| 27 | |
| 28 typedef std::map<WebContents*, GuestViewBase*> WebContentsGuestViewMap; | |
| 29 static base::LazyInstance<WebContentsGuestViewMap> webcontents_guestview_map = | |
| 30 LAZY_INSTANCE_INITIALIZER; | |
| 31 | |
| 32 } // namespace | |
| 33 | |
| 34 GuestViewBase::Event::Event(const std::string& name, | |
| 35 scoped_ptr<base::DictionaryValue> args) | |
| 36 : name_(name), args_(args.Pass()) { | |
| 37 } | |
| 38 | |
| 39 GuestViewBase::Event::~Event() { | |
| 40 } | |
| 41 | |
| 42 scoped_ptr<base::DictionaryValue> GuestViewBase::Event::GetArguments() { | |
| 43 return args_.Pass(); | |
| 44 } | |
| 45 | |
| 46 GuestViewBase::GuestViewBase(WebContents* guest_web_contents, | |
| 47 const std::string& embedder_extension_id) | |
| 48 : guest_web_contents_(guest_web_contents), | |
| 49 embedder_web_contents_(NULL), | |
| 50 embedder_extension_id_(embedder_extension_id), | |
| 51 embedder_render_process_id_(0), | |
| 52 browser_context_(guest_web_contents->GetBrowserContext()), | |
| 53 guest_instance_id_(guest_web_contents->GetEmbeddedInstanceID()), | |
| 54 view_instance_id_(guestview::kInstanceIDNone), | |
| 55 weak_ptr_factory_(this) { | |
| 56 webcontents_guestview_map.Get().insert( | |
| 57 std::make_pair(guest_web_contents, this)); | |
| 58 } | |
| 59 | |
| 60 // static | |
| 61 GuestViewBase* GuestViewBase::Create(WebContents* guest_web_contents, | |
| 62 const std::string& embedder_extension_id, | |
| 63 const std::string& view_type) { | |
| 64 if (view_type == "webview") { | |
| 65 return new WebViewGuest(guest_web_contents, embedder_extension_id); | |
| 66 } else if (view_type == "adview") { | |
| 67 return new AdViewGuest(guest_web_contents, embedder_extension_id); | |
| 68 } | |
| 69 NOTREACHED(); | |
| 70 return NULL; | |
| 71 } | |
| 72 | |
| 73 // static | |
| 74 GuestViewBase* GuestViewBase::FromWebContents(WebContents* web_contents) { | |
| 75 WebContentsGuestViewMap* guest_map = webcontents_guestview_map.Pointer(); | |
| 76 WebContentsGuestViewMap::iterator it = guest_map->find(web_contents); | |
| 77 return it == guest_map->end() ? NULL : it->second; | |
| 78 } | |
| 79 | |
| 80 // static | |
| 81 GuestViewBase* GuestViewBase::From(int embedder_process_id, | |
| 82 int guest_instance_id) { | |
| 83 EmbedderGuestViewMap* guest_map = embedder_guestview_map.Pointer(); | |
| 84 EmbedderGuestViewMap::iterator it = | |
| 85 guest_map->find(std::make_pair(embedder_process_id, guest_instance_id)); | |
| 86 return it == guest_map->end() ? NULL : it->second; | |
| 87 } | |
| 88 | |
| 89 // static | |
| 90 bool GuestViewBase::GetGuestPartitionConfigForSite( | |
| 91 const GURL& site, | |
| 92 std::string* partition_domain, | |
| 93 std::string* partition_name, | |
| 94 bool* in_memory) { | |
| 95 if (!site.SchemeIs(content::kGuestScheme)) | |
| 96 return false; | |
| 97 | |
| 98 // Since guest URLs are only used for packaged apps, there must be an app | |
| 99 // id in the URL. | |
| 100 CHECK(site.has_host()); | |
| 101 *partition_domain = site.host(); | |
| 102 // Since persistence is optional, the path must either be empty or the | |
| 103 // literal string. | |
| 104 *in_memory = (site.path() != "/persist"); | |
| 105 // The partition name is user supplied value, which we have encoded when the | |
| 106 // URL was created, so it needs to be decoded. | |
| 107 *partition_name = | |
| 108 net::UnescapeURLComponent(site.query(), net::UnescapeRule::NORMAL); | |
| 109 return true; | |
| 110 } | |
| 111 | |
| 112 // static | |
| 113 void GuestViewBase::GetDefaultContentSettingRules( | |
| 114 RendererContentSettingRules* rules, | |
| 115 bool incognito) { | |
| 116 rules->image_rules.push_back( | |
| 117 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), | |
| 118 ContentSettingsPattern::Wildcard(), | |
| 119 CONTENT_SETTING_ALLOW, | |
| 120 std::string(), | |
| 121 incognito)); | |
| 122 | |
| 123 rules->script_rules.push_back( | |
| 124 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), | |
| 125 ContentSettingsPattern::Wildcard(), | |
| 126 CONTENT_SETTING_ALLOW, | |
| 127 std::string(), | |
| 128 incognito)); | |
| 129 } | |
| 130 | |
| 131 void GuestViewBase::Attach(content::WebContents* embedder_web_contents, | |
| 132 const base::DictionaryValue& args) { | |
| 133 embedder_web_contents_ = embedder_web_contents; | |
| 134 embedder_render_process_id_ = | |
| 135 embedder_web_contents->GetRenderProcessHost()->GetID(); | |
| 136 args.GetInteger(guestview::kParameterInstanceId, &view_instance_id_); | |
| 137 | |
| 138 std::pair<int, int> key(embedder_render_process_id_, guest_instance_id_); | |
| 139 embedder_guestview_map.Get().insert(std::make_pair(key, this)); | |
| 140 | |
| 141 // GuestViewBase::Attach is called prior to initialization (and initial | |
| 142 // navigation) of the guest in the content layer in order to permit mapping | |
| 143 // the necessary associations between the <*view> element and its guest. This | |
| 144 // is needed by the <webview> WebRequest API to allow intercepting resource | |
| 145 // requests during navigation. However, queued events should be fired after | |
| 146 // content layer initialization in order to ensure that load events (such as | |
| 147 // 'loadstop') fire in embedder after the contentWindow is available. | |
| 148 if (!in_extension()) | |
| 149 return; | |
| 150 | |
| 151 base::MessageLoop::current()->PostTask( | |
| 152 FROM_HERE, | |
| 153 base::Bind(&GuestViewBase::SendQueuedEvents, | |
| 154 weak_ptr_factory_.GetWeakPtr())); | |
| 155 } | |
| 156 | |
| 157 GuestViewBase::~GuestViewBase() { | |
| 158 std::pair<int, int> key(embedder_render_process_id_, guest_instance_id_); | |
| 159 embedder_guestview_map.Get().erase(key); | |
| 160 | |
| 161 webcontents_guestview_map.Get().erase(guest_web_contents()); | |
| 162 | |
| 163 pending_events_.clear(); | |
| 164 } | |
| 165 | |
| 166 void GuestViewBase::DispatchEvent(Event* event) { | |
| 167 scoped_ptr<Event> event_ptr(event); | |
| 168 if (!in_extension()) { | |
| 169 NOTREACHED(); | |
| 170 return; | |
| 171 } | |
| 172 | |
| 173 if (!attached()) { | |
| 174 pending_events_.push_back(linked_ptr<Event>(event_ptr.release())); | |
| 175 return; | |
| 176 } | |
| 177 | |
| 178 Profile* profile = Profile::FromBrowserContext(browser_context_); | |
| 179 | |
| 180 extensions::EventFilteringInfo info; | |
| 181 info.SetURL(GURL()); | |
| 182 info.SetInstanceID(guest_instance_id_); | |
| 183 scoped_ptr<base::ListValue> args(new base::ListValue()); | |
| 184 args->Append(event->GetArguments().release()); | |
| 185 | |
| 186 extensions::EventRouter::DispatchEvent( | |
| 187 embedder_web_contents_, | |
| 188 profile, | |
| 189 embedder_extension_id_, | |
| 190 event->name(), | |
| 191 args.Pass(), | |
| 192 extensions::EventRouter::USER_GESTURE_UNKNOWN, | |
| 193 info); | |
| 194 } | |
| 195 | |
| 196 void GuestViewBase::SendQueuedEvents() { | |
| 197 if (!attached()) | |
| 198 return; | |
| 199 | |
| 200 while (!pending_events_.empty()) { | |
| 201 linked_ptr<Event> event_ptr = pending_events_.front(); | |
| 202 pending_events_.pop_front(); | |
| 203 DispatchEvent(event_ptr.release()); | |
| 204 } | |
| 205 } | |
| OLD | NEW |