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