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