- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / guestview / guestview.h
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 #ifndef CHROME_BROWSER_GUESTVIEW_GUESTVIEW_H_
6 #define CHROME_BROWSER_GUESTVIEW_GUESTVIEW_H_
7
8 #include <queue>
9
10 #include "base/values.h"
11 #include "content/public/browser/browser_plugin_guest_delegate.h"
12 #include "content/public/browser/web_contents.h"
13
14 class AdViewGuest;
15 class WebViewGuest;
16
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 {
22  public:
23   enum Type {
24     WEBVIEW,
25     ADVIEW,
26     UNKNOWN
27   };
28
29   class Event {
30    public:
31      Event(const std::string& event_name, scoped_ptr<DictionaryValue> args);
32      ~Event();
33
34     const std::string& event_name() const { return event_name_; }
35
36     scoped_ptr<DictionaryValue> GetArguments();
37
38    private:
39     const std::string event_name_;
40     scoped_ptr<DictionaryValue> args_;
41   };
42
43   static Type GetViewTypeFromString(const std::string& api_type);
44
45   static GuestView* Create(content::WebContents* guest_web_contents,
46                            const std::string& extension_id,
47                            Type view_type);
48
49   static GuestView* FromWebContents(content::WebContents* web_contents);
50
51   static GuestView* From(int embedder_process_id, int instance_id);
52
53   virtual void Attach(content::WebContents* embedder_web_contents,
54                       const base::DictionaryValue& args);
55
56   content::WebContents* embedder_web_contents() const {
57     return embedder_web_contents_;
58   }
59
60   // Returns the guest WebContents.
61   content::WebContents* guest_web_contents() const {
62     return guest_web_contents_;
63   }
64
65   virtual Type GetViewType() const;
66
67   // Returns a WebViewGuest if this GuestView belongs to a <webview>.
68   virtual WebViewGuest* AsWebView() = 0;
69
70   // Returns an AdViewGuest if the GuestView belongs to an <adview>.
71   virtual AdViewGuest* AsAdView() = 0;
72
73   // Returns whether this guest has an associated embedder.
74   bool attached() const { return !!embedder_web_contents_; }
75
76   // Returns the instance ID of the <*view> element.
77   int view_instance_id() const { return view_instance_id_; }
78
79   // Returns the instance ID of the guest WebContents.
80   int guest_instance_id() const { return guest_instance_id_; }
81
82   // Returns the extension ID of the embedder.
83   const std::string& extension_id() const { return extension_id_; }
84
85   // Returns the user browser context of the embedder.
86   content::BrowserContext* browser_context() const { return browser_context_; }
87
88   // Returns the embedder's process ID.
89   int embedder_render_process_id() const { return embedder_render_process_id_; }
90
91  protected:
92   GuestView(content::WebContents* guest_web_contents,
93             const std::string& extension_id);
94   virtual ~GuestView();
95
96   // Dispatches an event |event_name| to the embedder with the |event| fields.
97   void DispatchEvent(Event* event);
98
99  private:
100   void SendQueuedEvents();
101
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
108   // WebContents.
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_;
113
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_;
117
118   DISALLOW_COPY_AND_ASSIGN(GuestView);
119 };
120
121 #endif  // CHROME_BROWSER_GUESTVIEW_GUESTVIEW_H_