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