- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / guestview / guestview.cc
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.
4
5 #include "chrome/browser/guestview/guestview.h"
6
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"
15
16 using content::WebContents;
17
18 namespace {
19
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;
24
25 typedef std::map<WebContents*, GuestView*> WebContentsGuestViewMap;
26 static base::LazyInstance<WebContentsGuestViewMap> webcontents_guestview_map =
27     LAZY_INSTANCE_INITIALIZER;
28
29 }  // namespace
30
31 GuestView::Event::Event(const std::string& event_name,
32                         scoped_ptr<DictionaryValue> args)
33     : event_name_(event_name),
34       args_(args.Pass()) {
35 }
36
37 GuestView::Event::~Event() {
38 }
39
40 scoped_ptr<DictionaryValue> GuestView::Event::GetArguments() {
41   return args_.Pass();
42 }
43
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));
55 }
56
57 // static
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;
63   }
64   return GuestView::UNKNOWN;
65 }
66
67 // static
68 GuestView* GuestView::Create(WebContents* guest_web_contents,
69                              const std::string& extension_id,
70                              GuestView::Type view_type) {
71   switch (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);
76     default:
77       NOTREACHED();
78       return NULL;
79   }
80 }
81
82 // static
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;
87 }
88
89 // static
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;
95 }
96
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_);
103
104   std::pair<int, int> key(embedder_render_process_id_, guest_instance_id_);
105   embedder_guestview_map.Get().insert(std::make_pair(key, this));
106
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(
115       FROM_HERE,
116       base::Bind(&GuestView::SendQueuedEvents,
117                   base::Unretained(this)));
118 }
119
120 GuestView::Type GuestView::GetViewType() const {
121   return GuestView::UNKNOWN;
122 }
123
124 WebViewGuest* GuestView::AsWebView() {
125   return NULL;
126 }
127
128 AdViewGuest* GuestView::AsAdView() {
129   return NULL;
130 }
131
132 GuestView::~GuestView() {
133   std::pair<int, int> key(embedder_render_process_id_, guest_instance_id_);
134   embedder_guestview_map.Get().erase(key);
135
136   webcontents_guestview_map.Get().erase(guest_web_contents());
137
138   while (!pending_events_.empty()) {
139     delete pending_events_.front();
140     pending_events_.pop();
141   }
142 }
143
144 void GuestView::DispatchEvent(Event* event) {
145   if (!attached()) {
146     pending_events_.push(event);
147     return;
148   }
149
150   Profile* profile = Profile::FromBrowserContext(browser_context_);
151
152   extensions::EventFilteringInfo info;
153   info.SetURL(GURL());
154   info.SetInstanceID(guest_instance_id_);
155   scoped_ptr<ListValue> args(new ListValue());
156   args->Append(event->GetArguments().release());
157
158   extensions::EventRouter::DispatchEvent(
159       embedder_web_contents_, profile, extension_id_,
160       event->event_name(), args.Pass(),
161       extensions::EventRouter::USER_GESTURE_UNKNOWN, info);
162
163   delete event;
164 }
165
166 void GuestView::SendQueuedEvents() {
167   if (!attached())
168     return;
169
170   while (!pending_events_.empty()) {
171     Event* event = pending_events_.front();
172     pending_events_.pop();
173     DispatchEvent(event);
174   }
175 }