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
3 // found in the LICENSE file.
5 #include "chrome/browser/guestview/guestview.h"
7 #include "base/lazy_instance.h"
8 #include "chrome/browser/extensions/event_router.h"
9 #include "chrome/browser/guestview/adview/adview_guest.h"
10 #include "chrome/browser/guestview/guestview_constants.h"
11 #include "chrome/browser/guestview/webview/webview_guest.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "content/public/browser/render_process_host.h"
14 #include "content/public/browser/web_contents.h"
16 using content::WebContents;
20 // <embedder_process_id, guest_instance_id> => GuestView*
21 typedef std::map<std::pair<int, int>, GuestView*> EmbedderGuestViewMap;
22 static base::LazyInstance<EmbedderGuestViewMap> embedder_guestview_map =
23 LAZY_INSTANCE_INITIALIZER;
25 typedef std::map<WebContents*, GuestView*> WebContentsGuestViewMap;
26 static base::LazyInstance<WebContentsGuestViewMap> webcontents_guestview_map =
27 LAZY_INSTANCE_INITIALIZER;
31 GuestView::Event::Event(const std::string& event_name,
32 scoped_ptr<DictionaryValue> args)
33 : event_name_(event_name),
37 GuestView::Event::~Event() {
40 scoped_ptr<DictionaryValue> GuestView::Event::GetArguments() {
44 GuestView::GuestView(WebContents* guest_web_contents,
45 const std::string& extension_id)
46 : guest_web_contents_(guest_web_contents),
47 embedder_web_contents_(NULL),
48 extension_id_(extension_id),
49 embedder_render_process_id_(0),
50 browser_context_(guest_web_contents->GetBrowserContext()),
51 guest_instance_id_(guest_web_contents->GetEmbeddedInstanceID()),
52 view_instance_id_(guestview::kInstanceIDNone) {
53 webcontents_guestview_map.Get().insert(
54 std::make_pair(guest_web_contents, this));
58 GuestView::Type GuestView::GetViewTypeFromString(const std::string& api_type) {
59 if (api_type == "adview") {
60 return GuestView::ADVIEW;
61 } else if (api_type == "webview") {
62 return GuestView::WEBVIEW;
64 return GuestView::UNKNOWN;
68 GuestView* GuestView::Create(WebContents* guest_web_contents,
69 const std::string& extension_id,
70 GuestView::Type view_type) {
72 case GuestView::WEBVIEW:
73 return new WebViewGuest(guest_web_contents, extension_id);
74 case GuestView::ADVIEW:
75 return new AdViewGuest(guest_web_contents, extension_id);
83 GuestView* GuestView::FromWebContents(WebContents* web_contents) {
84 WebContentsGuestViewMap* guest_map = webcontents_guestview_map.Pointer();
85 WebContentsGuestViewMap::iterator it = guest_map->find(web_contents);
86 return it == guest_map->end() ? NULL : it->second;
90 GuestView* GuestView::From(int embedder_process_id, int guest_instance_id) {
91 EmbedderGuestViewMap* guest_map = embedder_guestview_map.Pointer();
92 EmbedderGuestViewMap::iterator it = guest_map->find(
93 std::make_pair(embedder_process_id, guest_instance_id));
94 return it == guest_map->end() ? NULL : it->second;
97 void GuestView::Attach(content::WebContents* embedder_web_contents,
98 const base::DictionaryValue& args) {
99 embedder_web_contents_ = embedder_web_contents;
100 embedder_render_process_id_ =
101 embedder_web_contents->GetRenderProcessHost()->GetID();
102 args.GetInteger(guestview::kParameterInstanceId, &view_instance_id_);
104 std::pair<int, int> key(embedder_render_process_id_, guest_instance_id_);
105 embedder_guestview_map.Get().insert(std::make_pair(key, this));
107 // GuestView::Attach is called prior to initialization (and initial
108 // navigation) of the guest in the content layer in order to permit mapping
109 // the necessary associations between the <*view> element and its guest. This
110 // is needed by the <webview> WebRequest API to allow intercepting resource
111 // requests during navigation. However, queued events should be fired after
112 // content layer initialization in order to ensure that load events (such as
113 // 'loadstop') fire in embedder after the contentWindow is available.
114 base::MessageLoop::current()->PostTask(
116 base::Bind(&GuestView::SendQueuedEvents,
117 base::Unretained(this)));
120 GuestView::Type GuestView::GetViewType() const {
121 return GuestView::UNKNOWN;
124 WebViewGuest* GuestView::AsWebView() {
128 AdViewGuest* GuestView::AsAdView() {
132 GuestView::~GuestView() {
133 std::pair<int, int> key(embedder_render_process_id_, guest_instance_id_);
134 embedder_guestview_map.Get().erase(key);
136 webcontents_guestview_map.Get().erase(guest_web_contents());
138 while (!pending_events_.empty()) {
139 delete pending_events_.front();
140 pending_events_.pop();
144 void GuestView::DispatchEvent(Event* event) {
146 pending_events_.push(event);
150 Profile* profile = Profile::FromBrowserContext(browser_context_);
152 extensions::EventFilteringInfo info;
154 info.SetInstanceID(guest_instance_id_);
155 scoped_ptr<ListValue> args(new ListValue());
156 args->Append(event->GetArguments().release());
158 extensions::EventRouter::DispatchEvent(
159 embedder_web_contents_, profile, extension_id_,
160 event->event_name(), args.Pass(),
161 extensions::EventRouter::USER_GESTURE_UNKNOWN, info);
166 void GuestView::SendQueuedEvents() {
170 while (!pending_events_.empty()) {
171 Event* event = pending_events_.front();
172 pending_events_.pop();
173 DispatchEvent(event);