23fb58187cebd1de87132ffc1c1ccc1f07b5fb88
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / browser / runtime.h
1 // Copyright (c) 2013 Intel Corporation. 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 XWALK_RUNTIME_BROWSER_RUNTIME_H_
6 #define XWALK_RUNTIME_BROWSER_RUNTIME_H_
7
8 #include <vector>
9
10 #include "base/basictypes.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "xwalk/runtime/browser/ui/native_app_window.h"
14 #include "content/public/browser/notification_observer.h"
15 #include "content/public/browser/notification_registrar.h"
16 #include "content/public/browser/web_contents_delegate.h"
17 #include "content/public/browser/web_contents_observer.h"
18 #include "content/public/common/favicon_url.h"
19 #include "url/gurl.h"
20 #include "ui/gfx/image/image.h"
21
22 namespace content {
23 class ColorChooser;
24 struct FileChooserParams;
25 class RenderProcessHost;
26 class SiteInstance;
27 class WebContents;
28 }
29
30 namespace xwalk {
31
32 class NativeAppWindow;
33 class RuntimeContext;
34
35 // Runtime represents the running environment for a web page. It is responsible
36 // for maintaning its owned WebContents and handling any communication between
37 // WebContents and native app window.
38 class Runtime : public content::WebContentsDelegate,
39                 public content::WebContentsObserver,
40                 public content::NotificationObserver,
41                 public NativeAppWindowDelegate {
42  public:
43   // New "Runtimes" are also created from Runtime::WebContentsCreated which
44   // is overridden WebContentsDelegate method. The "observer" is needed to
45   // observe appearance and removal of such Runtime instances.
46   class Observer {
47    public:
48       // Called when a new Runtime instance is added.
49       virtual void OnRuntimeAdded(Runtime* runtime) = 0;
50
51       // Called when a Runtime instance is removed.
52       virtual void OnRuntimeRemoved(Runtime* runtime) = 0;
53
54    protected:
55       virtual ~Observer() {}
56   };
57
58   void SetObserver(Observer* observer) { observer_ = observer; }
59
60   // Create a new Runtime instance which binds to a default app window.
61   static Runtime* CreateWithDefaultWindow(RuntimeContext*,
62                                           const GURL&, Observer* = NULL);
63   // Create a new Runtime instance with the given browsing context.
64   static Runtime* Create(RuntimeContext*,
65                          Observer* = NULL, content::SiteInstance* = NULL);
66
67   // Attach to a default app window.
68   void AttachDefaultWindow();
69   // Attach to a app window created with 'params'.
70   void AttachWindow(const NativeAppWindow::CreateParams& params);
71
72   void LoadURL(const GURL& url);
73   void Close();
74
75   content::WebContents* web_contents() const { return web_contents_.get(); }
76   NativeAppWindow* window() const { return window_; }
77   RuntimeContext* runtime_context() const { return runtime_context_; }
78   gfx::Image app_icon() const { return app_icon_; }
79
80   content::RenderProcessHost* GetRenderProcessHost();
81
82 #if defined(OS_TIZEN_MOBILE)
83   void CloseRootWindow();
84 #endif
85
86  protected:
87   Runtime(content::WebContents* web_contents, Observer* observer);
88   virtual ~Runtime();
89
90     // Overridden from content::WebContentsDelegate:
91   virtual content::WebContents* OpenURLFromTab(
92       content::WebContents* source,
93       const content::OpenURLParams& params) OVERRIDE;
94   virtual void LoadingStateChanged(content::WebContents* source,
95                                    bool to_different_document) OVERRIDE;
96   virtual void ToggleFullscreenModeForTab(content::WebContents* web_contents,
97                                           bool enter_fullscreen) OVERRIDE;
98   virtual bool IsFullscreenForTabOrPending(
99       const content::WebContents* web_contents) const OVERRIDE;
100   virtual void RequestToLockMouse(content::WebContents* web_contents,
101                                   bool user_gesture,
102                                   bool last_unlocked_by_target) OVERRIDE;
103   virtual void CloseContents(content::WebContents* source) OVERRIDE;
104   virtual void WebContentsCreated(content::WebContents* source_contents,
105                                   int opener_render_frame_id,
106                                   const base::string16& frame_name,
107                                   const GURL& target_url,
108                                   content::WebContents* new_contents) OVERRIDE;
109   virtual void DidNavigateMainFramePostCommit(
110       content::WebContents* web_contents) OVERRIDE;
111   virtual content::JavaScriptDialogManager*
112       GetJavaScriptDialogManager() OVERRIDE;
113   virtual void ActivateContents(content::WebContents* contents) OVERRIDE;
114   virtual void DeactivateContents(content::WebContents* contents) OVERRIDE;
115   virtual bool CanOverscrollContent() const OVERRIDE;
116   virtual bool PreHandleKeyboardEvent(
117       content::WebContents* source,
118       const content::NativeWebKeyboardEvent& event,
119       bool* is_keyboard_shortcut) OVERRIDE;
120   virtual void HandleKeyboardEvent(
121       content::WebContents* source,
122       const content::NativeWebKeyboardEvent& event) OVERRIDE;
123   virtual content::ColorChooser* OpenColorChooser(
124       content::WebContents* web_contents,
125       SkColor initial_color,
126       const std::vector<content::ColorSuggestion>& suggestions) OVERRIDE;
127   virtual void RunFileChooser(
128       content::WebContents* web_contents,
129       const content::FileChooserParams& params) OVERRIDE;
130   virtual void EnumerateDirectory(content::WebContents* web_contents,
131                                   int request_id,
132                                   const base::FilePath& path) OVERRIDE;
133   virtual void RequestMediaAccessPermission(
134       content::WebContents* web_contents,
135       const content::MediaStreamRequest& request,
136       const content::MediaResponseCallback& callback) OVERRIDE;
137
138   // Overridden from content::WebContentsObserver.
139   virtual void DidUpdateFaviconURL(
140       const std::vector<content::FaviconURL>& candidates) OVERRIDE;
141
142   // Callback method for WebContents::DownloadImage.
143   void DidDownloadFavicon(int id,
144                           int http_status_code,
145                           const GURL& image_url,
146                           const std::vector<SkBitmap>& bitmaps,
147                           const std::vector<gfx::Size>& sizes);
148
149   // NotificationObserver
150   virtual void Observe(int type,
151                        const content::NotificationSource& source,
152                        const content::NotificationDetails& details) OVERRIDE;
153
154   // NativeAppWindowDelegate implementation.
155   virtual void OnWindowDestroyed() OVERRIDE;
156
157   void ApplyWindowDefaultParams(NativeAppWindow::CreateParams* params);
158   void ApplyFullScreenParam(NativeAppWindow::CreateParams* params);
159
160 #if defined(OS_TIZEN_MOBILE)
161   void ApplyRootWindowParams(NativeAppWindow::CreateParams* params);
162   void SetRootWindow(NativeAppWindow* window);
163   void InitRootWindow();
164 #endif
165
166   // The browsing context.
167   xwalk::RuntimeContext* runtime_context_;
168
169   // Notification manager.
170   content::NotificationRegistrar registrar_;
171
172   // The WebContents owned by this runtime.
173   scoped_ptr<content::WebContents> web_contents_;
174
175   NativeAppWindow* window_;
176
177 #if defined(OS_TIZEN_MOBILE)
178   NativeAppWindow* root_window_;
179 #endif
180
181   gfx::Image app_icon_;
182
183   base::WeakPtrFactory<Runtime> weak_ptr_factory_;
184
185   // Fullscreen options.
186   enum FullscreenOptions {
187     NO_FULLSCREEN = 0,
188     // Fullscreen entered by launch with "--fullscreen".
189     FULLSCREEN_FOR_LAUNCH = 1,
190     // Fullscreen entered by HTML requestFullscreen.
191     FULLSCREEN_FOR_TAB = 1 << 1,
192   };
193
194   unsigned int fullscreen_options_;
195
196   Observer* observer_;
197 };
198
199 }  // namespace xwalk
200
201 #endif  // XWALK_RUNTIME_BROWSER_RUNTIME_H_