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.
5 #ifndef XWALK_RUNTIME_BROWSER_RUNTIME_H_
6 #define XWALK_RUNTIME_BROWSER_RUNTIME_H_
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"
20 #include "ui/gfx/image/image.h"
24 struct FileChooserParams;
25 class RenderProcessHost;
32 class NativeAppWindow;
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 {
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.
48 // Called when a new Runtime instance is added.
49 virtual void OnRuntimeAdded(Runtime* runtime) = 0;
51 // Called when a Runtime instance is removed.
52 virtual void OnRuntimeRemoved(Runtime* runtime) = 0;
55 virtual ~Observer() {}
58 void SetObserver(Observer* observer) { observer_ = observer; }
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);
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);
72 void LoadURL(const GURL& url);
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_; }
79 content::RenderProcessHost* GetRenderProcessHost();
81 void set_remote_debugging_enabled(bool enable) {
82 remote_debugging_enabled_ = enable;
84 bool remote_debugging_enabled() const { return remote_debugging_enabled_; }
86 #if defined(OS_TIZEN_MOBILE)
87 void CloseRootWindow();
91 Runtime(content::WebContents* web_contents, Observer* observer);
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,
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,
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;
142 // Overridden from content::WebContentsObserver.
143 virtual void DidUpdateFaviconURL(
144 const std::vector<content::FaviconURL>& candidates) OVERRIDE;
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);
153 // NotificationObserver
154 virtual void Observe(int type,
155 const content::NotificationSource& source,
156 const content::NotificationDetails& details) OVERRIDE;
158 // NativeAppWindowDelegate implementation.
159 virtual void OnWindowDestroyed() OVERRIDE;
161 void ApplyWindowDefaultParams(NativeAppWindow::CreateParams* params);
162 void ApplyFullScreenParam(NativeAppWindow::CreateParams* params);
164 #if defined(OS_TIZEN_MOBILE)
165 void ApplyRootWindowParams(NativeAppWindow::CreateParams* params);
166 void SetRootWindow(NativeAppWindow* window);
167 void InitRootWindow();
170 // Notification manager.
171 content::NotificationRegistrar registrar_;
173 // The WebContents owned by this runtime.
174 scoped_ptr<content::WebContents> web_contents_;
176 NativeAppWindow* window_;
178 #if defined(OS_TIZEN_MOBILE)
179 NativeAppWindow* root_window_;
182 gfx::Image app_icon_;
184 base::WeakPtrFactory<Runtime> weak_ptr_factory_;
186 // Fullscreen options.
187 enum FullscreenOptions {
189 // Fullscreen entered by launch with "--fullscreen".
190 FULLSCREEN_FOR_LAUNCH = 1,
191 // Fullscreen entered by HTML requestFullscreen.
192 FULLSCREEN_FOR_TAB = 1 << 1,
195 unsigned int fullscreen_options_;
196 bool remote_debugging_enabled_;
203 #endif // XWALK_RUNTIME_BROWSER_RUNTIME_H_