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