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 RuntimeContext* runtime_context() const { return runtime_context_; }
78 gfx::Image app_icon() const { return app_icon_; }
80 content::RenderProcessHost* GetRenderProcessHost();
82 #if defined(OS_TIZEN_MOBILE)
83 void CloseRootWindow();
87 Runtime(content::WebContents* web_contents, Observer* observer);
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,
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,
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;
138 // Overridden from content::WebContentsObserver.
139 virtual void DidUpdateFaviconURL(
140 const std::vector<content::FaviconURL>& candidates) OVERRIDE;
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);
149 // NotificationObserver
150 virtual void Observe(int type,
151 const content::NotificationSource& source,
152 const content::NotificationDetails& details) OVERRIDE;
154 // NativeAppWindowDelegate implementation.
155 virtual void OnWindowDestroyed() OVERRIDE;
157 void ApplyWindowDefaultParams(NativeAppWindow::CreateParams* params);
158 void ApplyFullScreenParam(NativeAppWindow::CreateParams* params);
160 #if defined(OS_TIZEN_MOBILE)
161 void ApplyRootWindowParams(NativeAppWindow::CreateParams* params);
162 void SetRootWindow(NativeAppWindow* window);
163 void InitRootWindow();
166 // The browsing context.
167 xwalk::RuntimeContext* runtime_context_;
169 // Notification manager.
170 content::NotificationRegistrar registrar_;
172 // The WebContents owned by this runtime.
173 scoped_ptr<content::WebContents> web_contents_;
175 NativeAppWindow* window_;
177 #if defined(OS_TIZEN_MOBILE)
178 NativeAppWindow* root_window_;
181 gfx::Image app_icon_;
183 base::WeakPtrFactory<Runtime> weak_ptr_factory_;
185 // Fullscreen options.
186 enum FullscreenOptions {
188 // Fullscreen entered by launch with "--fullscreen".
189 FULLSCREEN_FOR_LAUNCH = 1,
190 // Fullscreen entered by HTML requestFullscreen.
191 FULLSCREEN_FOR_TAB = 1 << 1,
194 unsigned int fullscreen_options_;
201 #endif // XWALK_RUNTIME_BROWSER_RUNTIME_H_