Home | History | Annotate | Download | only in devtools
      1 // Copyright (c) 2012 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 
      5 #ifndef CHROME_BROWSER_DEVTOOLS_DEVTOOLS_WINDOW_H_
      6 #define CHROME_BROWSER_DEVTOOLS_DEVTOOLS_WINDOW_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/basictypes.h"
     12 #include "base/memory/scoped_ptr.h"
     13 #include "base/memory/weak_ptr.h"
     14 #include "base/strings/string16.h"
     15 #include "chrome/browser/devtools/devtools_file_helper.h"
     16 #include "chrome/browser/devtools/devtools_file_system_indexer.h"
     17 #include "chrome/browser/devtools/devtools_toggle_action.h"
     18 #include "content/public/browser/devtools_client_host.h"
     19 #include "content/public/browser/devtools_frontend_host_delegate.h"
     20 #include "content/public/browser/notification_observer.h"
     21 #include "content/public/browser/notification_registrar.h"
     22 #include "content/public/browser/web_contents_delegate.h"
     23 
     24 class Browser;
     25 class BrowserWindow;
     26 class DevToolsControllerTest;
     27 class Profile;
     28 
     29 namespace base {
     30 class Value;
     31 }
     32 
     33 namespace content {
     34 class DevToolsAgentHost;
     35 class DevToolsClientHost;
     36 struct FileChooserParams;
     37 class RenderViewHost;
     38 class WebContents;
     39 }
     40 
     41 namespace IPC {
     42 class Message;
     43 }
     44 
     45 namespace user_prefs {
     46 class PrefRegistrySyncable;
     47 }
     48 
     49 enum DevToolsDockSide {
     50   DEVTOOLS_DOCK_SIDE_UNDOCKED = 0,
     51   DEVTOOLS_DOCK_SIDE_BOTTOM,
     52   DEVTOOLS_DOCK_SIDE_RIGHT,
     53   DEVTOOLS_DOCK_SIDE_MINIMIZED
     54 };
     55 
     56 class DevToolsWindow : private content::NotificationObserver,
     57                        private content::WebContentsDelegate,
     58                        private content::DevToolsFrontendHostDelegate {
     59  public:
     60   typedef base::Callback<void(bool)> InfoBarCallback;
     61 
     62   static const char kDevToolsApp[];
     63 
     64   virtual ~DevToolsWindow();
     65 
     66   static std::string GetDevToolsWindowPlacementPrefKey();
     67   static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
     68   static DevToolsWindow* GetDockedInstanceForInspectedTab(
     69       content::WebContents* inspected_tab);
     70   static bool IsDevToolsWindow(content::RenderViewHost* window_rvh);
     71   static DevToolsWindow* OpenDevToolsWindowForWorker(
     72       Profile* profile,
     73       content::DevToolsAgentHost* worker_agent);
     74   static DevToolsWindow* CreateDevToolsWindowForWorker(Profile* profile);
     75   static DevToolsWindow* OpenDevToolsWindow(
     76       content::RenderViewHost* inspected_rvh);
     77   static DevToolsWindow* ToggleDevToolsWindow(
     78       Browser* browser,
     79       DevToolsToggleAction action);
     80   static void OpenExternalFrontend(Profile* profile,
     81                                    const std::string& frontend_uri,
     82                                    content::DevToolsAgentHost* agent_host);
     83 
     84   // Exposed for testing, normal clients should not use this method.
     85   static DevToolsWindow* ToggleDevToolsWindow(
     86       content::RenderViewHost* inspected_rvh,
     87       bool force_open,
     88       DevToolsToggleAction action);
     89   static void InspectElement(
     90       content::RenderViewHost* inspected_rvh, int x, int y);
     91 
     92   static int GetMinimumWidth();
     93   static int GetMinimumHeight();
     94   static int GetMinimizedHeight();
     95 
     96   // content::DevToolsFrontendHostDelegate:
     97   virtual void InspectedContentsClosing() OVERRIDE;
     98 
     99   content::WebContents* web_contents() { return web_contents_; }
    100   Browser* browser() { return browser_; }  // For tests.
    101   DevToolsDockSide dock_side() const { return dock_side_; }
    102 
    103   content::RenderViewHost* GetRenderViewHost();
    104   content::DevToolsClientHost* GetDevToolsClientHostForTest();
    105 
    106   // Returns preferred devtools window width for given |container_width|. It
    107   // tries to use the saved window width, or, if none exists, 1/3 of the
    108   // container width, then clamps to try and ensure both devtools and content
    109   // are at least somewhat visible.
    110   // Called only for the case when devtools window is docked to the side.
    111   int GetWidth(int container_width);
    112 
    113   // Returns preferred devtools window height for given |container_height|.
    114   // Uses the same logic as GetWidth.
    115   // Called only for the case when devtools window is docked to bottom.
    116   int GetHeight(int container_height);
    117 
    118   // Stores preferred devtools window width for this instance.
    119   void SetWidth(int width);
    120 
    121   // Stores preferred devtools window height for this instance.
    122   void SetHeight(int height);
    123 
    124   void Show(DevToolsToggleAction action);
    125 
    126  private:
    127   friend class DevToolsControllerTest;
    128 
    129   DevToolsWindow(Profile* profile,
    130                  const GURL& frontend_url,
    131                  content::RenderViewHost* inspected_rvh,
    132                  DevToolsDockSide dock_side);
    133 
    134   static DevToolsWindow* Create(Profile* profile,
    135                                 const GURL& frontend_url,
    136                                 content::RenderViewHost* inspected_rvh,
    137                                 DevToolsDockSide dock_side,
    138                                 bool shared_worker_frontend);
    139   static GURL GetDevToolsURL(Profile* profile,
    140                              const GURL& base_url,
    141                              DevToolsDockSide dock_side,
    142                              bool shared_worker_frontend);
    143   static DevToolsWindow* FindDevToolsWindow(content::DevToolsAgentHost*);
    144   static DevToolsWindow* AsDevToolsWindow(content::RenderViewHost*);
    145   static DevToolsDockSide GetDockSideFromPrefs(Profile* profile);
    146   static std::string SideToString(DevToolsDockSide dock_side);
    147   static DevToolsDockSide SideFromString(const std::string& dock_side);
    148 
    149   // content::NotificationObserver:
    150   virtual void Observe(int type,
    151                        const content::NotificationSource& source,
    152                        const content::NotificationDetails& details) OVERRIDE;
    153 
    154   // content::WebContentsDelegate:
    155   virtual content::WebContents* OpenURLFromTab(
    156       content::WebContents* source,
    157       const content::OpenURLParams& params) OVERRIDE;
    158   virtual void AddNewContents(content::WebContents* source,
    159                               content::WebContents* new_contents,
    160                               WindowOpenDisposition disposition,
    161                               const gfx::Rect& initial_pos,
    162                               bool user_gesture,
    163                               bool* was_blocked) OVERRIDE;
    164   virtual void CloseContents(content::WebContents* source) OVERRIDE;
    165   virtual bool PreHandleKeyboardEvent(
    166       content::WebContents* source,
    167       const content::NativeWebKeyboardEvent& event,
    168       bool* is_keyboard_shortcut) OVERRIDE;
    169   virtual void HandleKeyboardEvent(
    170       content::WebContents* source,
    171       const content::NativeWebKeyboardEvent& event) OVERRIDE;
    172   virtual content::JavaScriptDialogManager*
    173       GetJavaScriptDialogManager() OVERRIDE;
    174   virtual content::ColorChooser* OpenColorChooser(
    175       content::WebContents* web_contents,
    176       SkColor color) OVERRIDE;
    177   virtual void RunFileChooser(
    178       content::WebContents* web_contents,
    179       const content::FileChooserParams& params) OVERRIDE;
    180   virtual void WebContentsFocused(content::WebContents* contents) OVERRIDE;
    181 
    182   // content::DevToolsFrontendHostDelegate:
    183   virtual void ActivateWindow() OVERRIDE;
    184   virtual void ChangeAttachedWindowHeight(unsigned height) OVERRIDE;
    185   virtual void CloseWindow() OVERRIDE;
    186   virtual void MoveWindow(int x, int y) OVERRIDE;
    187   virtual void SetDockSide(const std::string& side) OVERRIDE;
    188   virtual void OpenInNewTab(const std::string& url) OVERRIDE;
    189   virtual void SaveToFile(const std::string& url,
    190                           const std::string& content,
    191                           bool save_as) OVERRIDE;
    192   virtual void AppendToFile(const std::string& url,
    193                             const std::string& content) OVERRIDE;
    194   virtual void RequestFileSystems() OVERRIDE;
    195   virtual void AddFileSystem() OVERRIDE;
    196   virtual void RemoveFileSystem(const std::string& file_system_path) OVERRIDE;
    197   virtual void IndexPath(int request_id,
    198                          const std::string& file_system_path) OVERRIDE;
    199   virtual void StopIndexing(int request_id) OVERRIDE;
    200   virtual void SearchInPath(int request_id,
    201                             const std::string& file_system_path,
    202                             const std::string& query) OVERRIDE;
    203 
    204   // DevToolsFileHelper callbacks.
    205   void FileSavedAs(const std::string& url);
    206   void AppendedTo(const std::string& url);
    207   void FileSystemsLoaded(
    208       const std::vector<DevToolsFileHelper::FileSystem>& file_systems);
    209   void FileSystemAdded(const DevToolsFileHelper::FileSystem& file_system);
    210   void IndexingTotalWorkCalculated(int request_id,
    211                                    const std::string& file_system_path,
    212                                    int total_work);
    213   void IndexingWorked(int request_id,
    214                       const std::string& file_system_path,
    215                       int worked);
    216   void IndexingDone(int request_id, const std::string& file_system_path);
    217   void SearchCompleted(int request_id,
    218                        const std::string& file_system_path,
    219                        const std::vector<std::string>& file_paths);
    220   void ShowDevToolsConfirmInfoBar(const string16& message,
    221                                   const InfoBarCallback& callback);
    222 
    223   void CreateDevToolsBrowser();
    224   bool FindInspectedBrowserAndTabIndex(Browser**, int* tab);
    225   BrowserWindow* GetInspectedBrowserWindow();
    226   bool IsInspectedBrowserPopup();
    227   void UpdateFrontendDockSide();
    228   void Hide();
    229   void ScheduleAction(DevToolsToggleAction action);
    230   void DoAction();
    231   void UpdateTheme();
    232   void AddDevToolsExtensionsToClient();
    233   void CallClientFunction(const std::string& function_name,
    234                           const base::Value* arg1,
    235                           const base::Value* arg2,
    236                           const base::Value* arg3);
    237   void UpdateBrowserToolbar();
    238   bool IsDocked();
    239   void Restore();
    240   content::WebContents* GetInspectedWebContents();
    241 
    242   class InspectedWebContentsObserver;
    243   scoped_ptr<InspectedWebContentsObserver> inspected_contents_observer_;
    244   class FrontendWebContentsObserver;
    245   scoped_ptr<FrontendWebContentsObserver> frontend_contents_observer_;
    246 
    247   Profile* profile_;
    248   content::WebContents* web_contents_;
    249   Browser* browser_;
    250   DevToolsDockSide dock_side_;
    251   bool is_loaded_;
    252   DevToolsToggleAction action_on_load_;
    253   content::NotificationRegistrar registrar_;
    254   scoped_ptr<content::DevToolsClientHost> frontend_host_;
    255   base::WeakPtrFactory<DevToolsWindow> weak_factory_;
    256   scoped_ptr<DevToolsFileHelper> file_helper_;
    257   scoped_refptr<DevToolsFileSystemIndexer> file_system_indexer_;
    258   typedef std::map<
    259       int,
    260       scoped_refptr<DevToolsFileSystemIndexer::FileSystemIndexingJob> >
    261       IndexingJobsMap;
    262   IndexingJobsMap indexing_jobs_;
    263   int width_;
    264   int height_;
    265   DevToolsDockSide dock_side_before_minimized_;
    266 
    267   DISALLOW_COPY_AND_ASSIGN(DevToolsWindow);
    268 };
    269 
    270 #endif  // CHROME_BROWSER_DEVTOOLS_DEVTOOLS_WINDOW_H_
    271