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