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;
30 class NativeAppWindow;
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 {
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.
46 // Called when a new Runtime instance is added.
47 virtual void OnRuntimeAdded(Runtime* runtime) = 0;
49 // Called when a Runtime instance is removed.
50 virtual void OnRuntimeRemoved(Runtime* runtime) = 0;
53 virtual ~Observer() {}
56 void SetObserver(Observer* observer) { observer_ = observer; }
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);
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);
69 void LoadURL(const GURL& url);
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_; }
78 void CloseRootWindow();
82 Runtime(content::WebContents* web_contents, Observer* observer);
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,
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,
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;
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;
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);
144 // NotificationObserver
145 virtual void Observe(int type,
146 const content::NotificationSource& source,
147 const content::NotificationDetails& details) OVERRIDE;
149 // NativeAppWindowDelegate implementation.
150 virtual void OnWindowDestroyed() OVERRIDE;
152 void ApplyWindowDefaultParams(NativeAppWindow::CreateParams* params);
153 void ApplyFullScreenParam(NativeAppWindow::CreateParams* params);
155 #if defined(OS_TIZEN)
156 void ApplyRootWindowParams(NativeAppWindow::CreateParams* params);
157 void SetRootWindow(NativeAppWindow* window);
158 void InitRootWindow();
161 // The browsing context.
162 xwalk::RuntimeContext* runtime_context_;
164 // Notification manager.
165 content::NotificationRegistrar registrar_;
167 // The WebContents owned by this runtime.
168 scoped_ptr<content::WebContents> web_contents_;
170 NativeAppWindow* window_;
172 #if defined(OS_TIZEN)
173 NativeAppWindow* root_window_;
176 gfx::Image app_icon_;
178 base::WeakPtrFactory<Runtime> weak_ptr_factory_;
180 // Fullscreen options.
181 enum FullscreenOptions {
183 // Fullscreen entered by launch with "--fullscreen".
184 FULLSCREEN_FOR_LAUNCH = 1,
185 // Fullscreen entered by HTML requestFullscreen.
186 FULLSCREEN_FOR_TAB = 1 << 1,
189 unsigned int fullscreen_options_;
196 #endif // XWALK_RUNTIME_BROWSER_RUNTIME_H_