5b48ab5b79649efae7513ccf9c64d6cf2fc613c5
[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   gfx::Image app_icon() const { return app_icon_; }
78
79   content::RenderProcessHost* GetRenderProcessHost();
80
81   void set_remote_debugging_enabled(bool enable) {
82     remote_debugging_enabled_ = enable;
83   }
84   bool remote_debugging_enabled() const { return remote_debugging_enabled_; }
85
86 #if defined(OS_TIZEN_MOBILE)
87   void CloseRootWindow();
88 #endif
89
90  protected:
91   Runtime(content::WebContents* web_contents, Observer* observer);
92   virtual ~Runtime();
93
94     // Overridden from content::WebContentsDelegate:
95   virtual content::WebContents* OpenURLFromTab(
96       content::WebContents* source,
97       const content::OpenURLParams& params) OVERRIDE;
98   virtual void LoadingStateChanged(content::WebContents* source,
99                                    bool to_different_document) OVERRIDE;
100   virtual void ToggleFullscreenModeForTab(content::WebContents* web_contents,
101                                           bool enter_fullscreen) OVERRIDE;
102   virtual bool IsFullscreenForTabOrPending(
103       const content::WebContents* web_contents) const OVERRIDE;
104   virtual void RequestToLockMouse(content::WebContents* web_contents,
105                                   bool user_gesture,
106                                   bool last_unlocked_by_target) OVERRIDE;
107   virtual void CloseContents(content::WebContents* source) OVERRIDE;
108   virtual void WebContentsCreated(content::WebContents* source_contents,
109                                   int opener_render_frame_id,
110                                   const base::string16& frame_name,
111                                   const GURL& target_url,
112                                   content::WebContents* new_contents) OVERRIDE;
113   virtual void DidNavigateMainFramePostCommit(
114       content::WebContents* web_contents) OVERRIDE;
115   virtual content::JavaScriptDialogManager*
116       GetJavaScriptDialogManager() OVERRIDE;
117   virtual void ActivateContents(content::WebContents* contents) OVERRIDE;
118   virtual void DeactivateContents(content::WebContents* contents) OVERRIDE;
119   virtual bool CanOverscrollContent() const OVERRIDE;
120   virtual bool PreHandleKeyboardEvent(
121       content::WebContents* source,
122       const content::NativeWebKeyboardEvent& event,
123       bool* is_keyboard_shortcut) OVERRIDE;
124   virtual void HandleKeyboardEvent(
125       content::WebContents* source,
126       const content::NativeWebKeyboardEvent& event) OVERRIDE;
127   virtual content::ColorChooser* OpenColorChooser(
128       content::WebContents* web_contents,
129       SkColor initial_color,
130       const std::vector<content::ColorSuggestion>& suggestions) OVERRIDE;
131   virtual void RunFileChooser(
132       content::WebContents* web_contents,
133       const content::FileChooserParams& params) OVERRIDE;
134   virtual void EnumerateDirectory(content::WebContents* web_contents,
135                                   int request_id,
136                                   const base::FilePath& path) OVERRIDE;
137   virtual void RequestMediaAccessPermission(
138       content::WebContents* web_contents,
139       const content::MediaStreamRequest& request,
140       const content::MediaResponseCallback& callback) OVERRIDE;
141
142   // Overridden from content::WebContentsObserver.
143   virtual void DidUpdateFaviconURL(
144       const std::vector<content::FaviconURL>& candidates) OVERRIDE;
145
146   // Callback method for WebContents::DownloadImage.
147   void DidDownloadFavicon(int id,
148                           int http_status_code,
149                           const GURL& image_url,
150                           const std::vector<SkBitmap>& bitmaps,
151                           const std::vector<gfx::Size>& sizes);
152
153   // NotificationObserver
154   virtual void Observe(int type,
155                        const content::NotificationSource& source,
156                        const content::NotificationDetails& details) OVERRIDE;
157
158   // NativeAppWindowDelegate implementation.
159   virtual void OnWindowDestroyed() OVERRIDE;
160
161   void ApplyWindowDefaultParams(NativeAppWindow::CreateParams* params);
162   void ApplyFullScreenParam(NativeAppWindow::CreateParams* params);
163
164 #if defined(OS_TIZEN_MOBILE)
165   void ApplyRootWindowParams(NativeAppWindow::CreateParams* params);
166   void SetRootWindow(NativeAppWindow* window);
167   void InitRootWindow();
168 #endif
169
170   // Notification manager.
171   content::NotificationRegistrar registrar_;
172
173   // The WebContents owned by this runtime.
174   scoped_ptr<content::WebContents> web_contents_;
175
176   NativeAppWindow* window_;
177
178 #if defined(OS_TIZEN_MOBILE)
179   NativeAppWindow* root_window_;
180 #endif
181
182   gfx::Image app_icon_;
183
184   base::WeakPtrFactory<Runtime> weak_ptr_factory_;
185
186   // Fullscreen options.
187   enum FullscreenOptions {
188     NO_FULLSCREEN = 0,
189     // Fullscreen entered by launch with "--fullscreen".
190     FULLSCREEN_FOR_LAUNCH = 1,
191     // Fullscreen entered by HTML requestFullscreen.
192     FULLSCREEN_FOR_TAB = 1 << 1,
193   };
194
195   unsigned int fullscreen_options_;
196   bool remote_debugging_enabled_;
197
198   Observer* observer_;
199 };
200
201 }  // namespace xwalk
202
203 #endif  // XWALK_RUNTIME_BROWSER_RUNTIME_H_