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 #ifndef CHROME_BROWSER_GUESTVIEW_GUESTVIEW_H_
6 #define CHROME_BROWSER_GUESTVIEW_GUESTVIEW_H_
10 #include "base/values.h"
11 #include "content/public/browser/browser_plugin_guest_delegate.h"
12 #include "content/public/browser/web_contents.h"
17 // A GuestView is the base class browser-side API implementation for a <*view>
18 // tag. GuestView maintains an association between a guest WebContents and an
19 // embedder WebContents. It receives events issued from the guest and relays
20 // them to the embedder.
21 class GuestView : public content::BrowserPluginGuestDelegate {
31 Event(const std::string& event_name, scoped_ptr<DictionaryValue> args);
34 const std::string& event_name() const { return event_name_; }
36 scoped_ptr<DictionaryValue> GetArguments();
39 const std::string event_name_;
40 scoped_ptr<DictionaryValue> args_;
43 static Type GetViewTypeFromString(const std::string& api_type);
45 static GuestView* Create(content::WebContents* guest_web_contents,
46 const std::string& extension_id,
49 static GuestView* FromWebContents(content::WebContents* web_contents);
51 static GuestView* From(int embedder_process_id, int instance_id);
53 virtual void Attach(content::WebContents* embedder_web_contents,
54 const base::DictionaryValue& args);
56 content::WebContents* embedder_web_contents() const {
57 return embedder_web_contents_;
60 // Returns the guest WebContents.
61 content::WebContents* guest_web_contents() const {
62 return guest_web_contents_;
65 virtual Type GetViewType() const;
67 // Returns a WebViewGuest if this GuestView belongs to a <webview>.
68 virtual WebViewGuest* AsWebView() = 0;
70 // Returns an AdViewGuest if the GuestView belongs to an <adview>.
71 virtual AdViewGuest* AsAdView() = 0;
73 // Returns whether this guest has an associated embedder.
74 bool attached() const { return !!embedder_web_contents_; }
76 // Returns the instance ID of the <*view> element.
77 int view_instance_id() const { return view_instance_id_; }
79 // Returns the instance ID of the guest WebContents.
80 int guest_instance_id() const { return guest_instance_id_; }
82 // Returns the extension ID of the embedder.
83 const std::string& extension_id() const { return extension_id_; }
85 // Returns the user browser context of the embedder.
86 content::BrowserContext* browser_context() const { return browser_context_; }
88 // Returns the embedder's process ID.
89 int embedder_render_process_id() const { return embedder_render_process_id_; }
92 GuestView(content::WebContents* guest_web_contents,
93 const std::string& extension_id);
96 // Dispatches an event |event_name| to the embedder with the |event| fields.
97 void DispatchEvent(Event* event);
100 void SendQueuedEvents();
102 content::WebContents* guest_web_contents_;
103 content::WebContents* embedder_web_contents_;
104 const std::string extension_id_;
105 int embedder_render_process_id_;
106 content::BrowserContext* browser_context_;
107 // |guest_instance_id_| is a profile-wide unique identifier for a guest
109 const int guest_instance_id_;
110 // |view_instance_id_| is an identifier that's unique within a particular
111 // embedder RenderViewHost for a particular <*view> instance.
112 int view_instance_id_;
114 // This is a queue of Events that are destined to be sent to the embedder once
115 // the guest is attached to a particular embedder.
116 std::queue<Event*> pending_events_;
118 DISALLOW_COPY_AND_ASSIGN(GuestView);
121 #endif // CHROME_BROWSER_GUESTVIEW_GUESTVIEW_H_