Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / content / shell / browser / shell.h
1 // Copyright 2013 The Chromium Authors. 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 #ifndef CONTENT_SHELL_BROWSER_SHELL_H_
5 #define CONTENT_SHELL_BROWSER_SHELL_H_
6
7
8 #include <vector>
9
10 #include "base/basictypes.h"
11 #include "base/callback_forward.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/strings/string_piece.h"
14 #include "build/build_config.h"
15 #include "content/public/browser/web_contents_delegate.h"
16 #include "content/public/browser/web_contents_observer.h"
17 #include "ipc/ipc_channel.h"
18 #include "ui/gfx/native_widget_types.h"
19 #include "ui/gfx/size.h"
20
21 #if defined(TOOLKIT_GTK)
22 #include <gtk/gtk.h>
23 #include "ui/base/gtk/gtk_signal.h"
24
25 typedef struct _GtkToolItem GtkToolItem;
26 #elif defined(OS_ANDROID)
27 #include "base/android/scoped_java_ref.h"
28 #elif defined(USE_AURA)
29 #if defined(OS_CHROMEOS)
30 namespace wm {
31 class WMTestHelper;
32 }
33 #endif  // defined(OS_CHROMEOS)
34 namespace views {
35 class Widget;
36 class ViewsDelegate;
37 }
38 #endif  // defined(USE_AURA)
39
40 class GURL;
41 namespace content {
42
43 #if defined(USE_AURA)
44 class ShellPlatformDataAura;
45 #endif
46
47 class BrowserContext;
48 class ShellDevToolsFrontend;
49 class ShellJavaScriptDialogManager;
50 class SiteInstance;
51 class WebContents;
52
53 // This represents one window of the Content Shell, i.e. all the UI including
54 // buttons and url bar, as well as the web content area.
55 class Shell : public WebContentsDelegate,
56               public WebContentsObserver {
57  public:
58   static const int kDefaultTestWindowWidthDip;
59   static const int kDefaultTestWindowHeightDip;
60
61   virtual ~Shell();
62
63   void LoadURL(const GURL& url);
64   void LoadURLForFrame(const GURL& url, const std::string& frame_name);
65   void GoBackOrForward(int offset);
66   void Reload();
67   void Stop();
68   void UpdateNavigationControls();
69   void Close();
70   void ShowDevTools();
71   void CloseDevTools();
72 #if defined(TOOLKIT_GTK) || defined(OS_MACOSX)
73   // Resizes the main window to the given dimensions.
74   void SizeTo(const gfx::Size& content_size);
75 #endif
76
77   // Do one time initialization at application startup.
78   static void Initialize();
79
80   static Shell* CreateNewWindow(BrowserContext* browser_context,
81                                 const GURL& url,
82                                 SiteInstance* site_instance,
83                                 int routing_id,
84                                 const gfx::Size& initial_size);
85
86   // Returns the Shell object corresponding to the given RenderViewHost.
87   static Shell* FromRenderViewHost(RenderViewHost* rvh);
88
89   // Returns the currently open windows.
90   static std::vector<Shell*>& windows() { return windows_; }
91
92   // Closes all windows and returns. This runs a message loop.
93   static void CloseAllWindows();
94
95   // Used for content_browsertests. Called once.
96   static void SetShellCreatedCallback(
97       base::Callback<void(Shell*)> shell_created_callback);
98
99   WebContents* web_contents() const { return web_contents_.get(); }
100   gfx::NativeWindow window() { return window_; }
101
102 #if defined(OS_MACOSX)
103   // Public to be called by an ObjC bridge object.
104   void ActionPerformed(int control);
105   void URLEntered(std::string url_string);
106 #elif defined(OS_ANDROID)
107   // Registers the Android Java to native methods.
108   static bool Register(JNIEnv* env);
109 #endif
110
111   // WebContentsDelegate
112   virtual WebContents* OpenURLFromTab(WebContents* source,
113                                       const OpenURLParams& params) OVERRIDE;
114   virtual void AddNewContents(WebContents* source,
115                               WebContents* new_contents,
116                               WindowOpenDisposition disposition,
117                               const gfx::Rect& initial_pos,
118                               bool user_gesture,
119                               bool* was_blocked) OVERRIDE;
120   virtual void LoadingStateChanged(WebContents* source) OVERRIDE;
121 #if defined(OS_ANDROID)
122   virtual void LoadProgressChanged(WebContents* source,
123                                    double progress) OVERRIDE;
124 #endif
125   virtual void ToggleFullscreenModeForTab(WebContents* web_contents,
126                                           bool enter_fullscreen) OVERRIDE;
127   virtual bool IsFullscreenForTabOrPending(
128       const WebContents* web_contents) const OVERRIDE;
129   virtual void RequestToLockMouse(WebContents* web_contents,
130                                   bool user_gesture,
131                                   bool last_unlocked_by_target) OVERRIDE;
132   virtual void CloseContents(WebContents* source) OVERRIDE;
133   virtual bool CanOverscrollContent() const OVERRIDE;
134   virtual void DidNavigateMainFramePostCommit(
135       WebContents* web_contents) OVERRIDE;
136   virtual JavaScriptDialogManager* GetJavaScriptDialogManager() OVERRIDE;
137 #if defined(OS_MACOSX)
138   virtual void HandleKeyboardEvent(
139       WebContents* source,
140       const NativeWebKeyboardEvent& event) OVERRIDE;
141 #endif
142   virtual bool AddMessageToConsole(WebContents* source,
143                                    int32 level,
144                                    const base::string16& message,
145                                    int32 line_no,
146                                    const base::string16& source_id) OVERRIDE;
147   virtual void RendererUnresponsive(WebContents* source) OVERRIDE;
148   virtual void ActivateContents(WebContents* contents) OVERRIDE;
149   virtual void DeactivateContents(WebContents* contents) OVERRIDE;
150   virtual void WorkerCrashed(WebContents* source) OVERRIDE;
151
152  private:
153   enum UIControl {
154     BACK_BUTTON,
155     FORWARD_BUTTON,
156     STOP_BUTTON
157   };
158
159   class DevToolsWebContentsObserver;
160
161   explicit Shell(WebContents* web_contents);
162
163   // Helper to create a new Shell given a newly created WebContents.
164   static Shell* CreateShell(WebContents* web_contents,
165                             const gfx::Size& initial_size);
166
167   // Helper for one time initialization of application
168   static void PlatformInitialize(const gfx::Size& default_window_size);
169   // Helper for one time deinitialization of platform specific state.
170   static void PlatformExit();
171
172   // Adjust the size when Blink sends 0 for width and/or height.
173   // This happens when Blink requests a default-sized window.
174   static gfx::Size AdjustWindowSize(const gfx::Size& initial_size);
175
176   // All the methods that begin with Platform need to be implemented by the
177   // platform specific Shell implementation.
178   // Called from the destructor to let each platform do any necessary cleanup.
179   void PlatformCleanUp();
180   // Creates the main window GUI.
181   void PlatformCreateWindow(int width, int height);
182   // Links the WebContents into the newly created window.
183   void PlatformSetContents();
184   // Resize the content area and GUI.
185   void PlatformResizeSubViews();
186   // Enable/disable a button.
187   void PlatformEnableUIControl(UIControl control, bool is_enabled);
188   // Updates the url in the url bar.
189   void PlatformSetAddressBarURL(const GURL& url);
190   // Sets whether the spinner is spinning.
191   void PlatformSetIsLoading(bool loading);
192   // Set the title of shell window
193   void PlatformSetTitle(const base::string16& title);
194 #if defined(OS_ANDROID)
195   void PlatformToggleFullscreenModeForTab(WebContents* web_contents,
196                                           bool enter_fullscreen);
197   bool PlatformIsFullscreenForTabOrPending(
198       const WebContents* web_contents) const;
199 #endif
200
201   gfx::NativeView GetContentView();
202
203   // WebContentsObserver
204   virtual void TitleWasSet(NavigationEntry* entry, bool explicit_set) OVERRIDE;
205
206   void OnDevToolsWebContentsDestroyed();
207
208 #if defined(TOOLKIT_GTK)
209   CHROMEGTK_CALLBACK_0(Shell, void, OnBackButtonClicked);
210   CHROMEGTK_CALLBACK_0(Shell, void, OnForwardButtonClicked);
211   CHROMEGTK_CALLBACK_0(Shell, void, OnReloadButtonClicked);
212   CHROMEGTK_CALLBACK_0(Shell, void, OnStopButtonClicked);
213   CHROMEGTK_CALLBACK_0(Shell, void, OnURLEntryActivate);
214   CHROMEGTK_CALLBACK_0(Shell, gboolean, OnWindowDestroyed);
215
216   CHROMEG_CALLBACK_3(Shell, gboolean, OnCloseWindowKeyPressed, GtkAccelGroup*,
217                      GObject*, guint, GdkModifierType);
218   CHROMEG_CALLBACK_3(Shell, gboolean, OnNewWindowKeyPressed, GtkAccelGroup*,
219                      GObject*, guint, GdkModifierType);
220   CHROMEG_CALLBACK_3(Shell, gboolean, OnHighlightURLView, GtkAccelGroup*,
221                      GObject*, guint, GdkModifierType);
222   CHROMEG_CALLBACK_3(Shell, gboolean, OnReloadKeyPressed, GtkAccelGroup*,
223                      GObject*, guint, GdkModifierType);
224 #endif
225
226   scoped_ptr<ShellJavaScriptDialogManager> dialog_manager_;
227
228   scoped_ptr<WebContents> web_contents_;
229
230   scoped_ptr<DevToolsWebContentsObserver> devtools_observer_;
231   ShellDevToolsFrontend* devtools_frontend_;
232
233   bool is_fullscreen_;
234
235   gfx::NativeWindow window_;
236   gfx::NativeEditView url_edit_view_;
237
238   gfx::Size content_size_;
239
240 #if defined(TOOLKIT_GTK)
241   GtkWidget* vbox_;
242
243   GtkToolItem* back_button_;
244   GtkToolItem* forward_button_;
245   GtkToolItem* reload_button_;
246   GtkToolItem* stop_button_;
247
248   GtkWidget* spinner_;
249   GtkToolItem* spinner_item_;
250
251   int ui_elements_height_; // height of menubar, toolbar, etc.
252 #elif defined(OS_ANDROID)
253   base::android::ScopedJavaGlobalRef<jobject> java_object_;
254 #elif defined(USE_AURA)
255 #if defined(OS_CHROMEOS)
256   static wm::WMTestHelper* wm_test_helper_;
257 #endif
258 #if defined(TOOLKIT_VIEWS)
259   static views::ViewsDelegate* views_delegate_;
260
261   views::Widget* window_widget_;
262 #endif // defined(TOOLKIT_VIEWS)
263   static ShellPlatformDataAura* platform_;
264 #endif  // defined(USE_AURA)
265
266   bool headless_;
267
268   // A container of all the open windows. We use a vector so we can keep track
269   // of ordering.
270   static std::vector<Shell*> windows_;
271
272   static base::Callback<void(Shell*)> shell_created_callback_;
273
274   // True if the destructur of Shell should post a quit closure on the current
275   // message loop if the destructed Shell object was the last one.
276   static bool quit_message_loop_;
277 };
278
279 }  // namespace content
280
281 #endif  // CONTENT_SHELL_BROWSER_SHELL_H_