Home | History | Annotate | Download | only in debugger
      1 // Copyright (c) 2011 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_DEBUGGER_DEVTOOLS_MANAGER_H_
      6 #define CHROME_BROWSER_DEBUGGER_DEVTOOLS_MANAGER_H_
      7 #pragma once
      8 
      9 #include <map>
     10 #include <string>
     11 
     12 #include "base/memory/ref_counted.h"
     13 #include "chrome/browser/debugger/devtools_client_host.h"
     14 #include "chrome/browser/debugger/devtools_toggle_action.h"
     15 #include "content/common/notification_observer.h"
     16 #include "content/common/notification_registrar.h"
     17 #include "webkit/glue/resource_loader_bridge.h"
     18 
     19 namespace IPC {
     20 class Message;
     21 }
     22 
     23 class DevToolsNetLogObserver;
     24 class GURL;
     25 class IOThread;
     26 class PrefService;
     27 class RenderViewHost;
     28 class TabContentsWraper;
     29 
     30 using webkit_glue::ResourceLoaderBridge;
     31 
     32 typedef std::map<std::string, std::string> DevToolsRuntimeProperties;
     33 
     34 // This class is a singleton that manages DevToolsClientHost instances and
     35 // routes messages between developer tools clients and agents.
     36 class DevToolsManager : public DevToolsClientHost::CloseListener,
     37                         public NotificationObserver,
     38                         public base::RefCounted<DevToolsManager> {
     39  public:
     40   static DevToolsManager* GetInstance();
     41 
     42   static void RegisterUserPrefs(PrefService* prefs);
     43 
     44   DevToolsManager();
     45 
     46   // Returns DevToolsClientHost registered for |inspected_rvh| or NULL if
     47   // there is no alive DevToolsClientHost registered for |inspected_rvh|.
     48   DevToolsClientHost* GetDevToolsClientHostFor(RenderViewHost* inspected_rvh);
     49 
     50   // Registers new DevToolsClientHost for |inspected_rvh|. There must be no
     51   // other DevToolsClientHosts registered for the RenderViewHost at the moment.
     52   void RegisterDevToolsClientHostFor(RenderViewHost* inspected_rvh,
     53                                      DevToolsClientHost* client_host);
     54   void UnregisterDevToolsClientHostFor(RenderViewHost* inspected_rvh);
     55 
     56   void ForwardToDevToolsAgent(RenderViewHost* client_rvh,
     57                               const IPC::Message& message);
     58   void ForwardToDevToolsAgent(DevToolsClientHost* from,
     59                               const IPC::Message& message);
     60   void ForwardToDevToolsClient(RenderViewHost* inspected_rvh,
     61                                const IPC::Message& message);
     62 
     63   void ActivateWindow(RenderViewHost* client_rvn);
     64   void CloseWindow(RenderViewHost* client_rvn);
     65   void RequestDockWindow(RenderViewHost* client_rvn);
     66   void RequestUndockWindow(RenderViewHost* client_rvn);
     67 
     68   void OpenDevToolsWindow(RenderViewHost* inspected_rvh);
     69   void ToggleDevToolsWindow(RenderViewHost* inspected_rvh,
     70                             DevToolsToggleAction action);
     71   void RuntimePropertyChanged(RenderViewHost* inspected_rvh,
     72                               const std::string& name,
     73                               const std::string& value);
     74 
     75   // Starts element inspection in the devtools client.
     76   // Creates one by means of OpenDevToolsWindow if no client
     77   // exists.
     78   void InspectElement(RenderViewHost* inspected_rvh, int x, int y);
     79 
     80   // Sends 'Attach' message to the agent using |dest_rvh| in case
     81   // there is a DevToolsClientHost registered for the |inspected_rvh|.
     82   void OnNavigatingToPendingEntry(RenderViewHost* inspected_rvh,
     83                                   RenderViewHost* dest_rvh,
     84                                   const GURL& gurl);
     85 
     86   // Invoked when a tab is replaced by another tab. This is triggered by
     87   // TabStripModel::ReplaceTabContentsAt.
     88   void TabReplaced(TabContentsWrapper* old_tab, TabContentsWrapper* new_tab);
     89 
     90   // Detaches client host and returns cookie that can be used in
     91   // AttachClientHost.
     92   int DetachClientHost(RenderViewHost* from_rvh);
     93 
     94   // Attaches orphan client host to new render view host.
     95   void AttachClientHost(int client_host_cookie,
     96                         RenderViewHost* to_rvh);
     97 
     98   // Closes all open developer tools windows.
     99   void CloseAllClientHosts();
    100 
    101  private:
    102   friend class base::RefCounted<DevToolsManager>;
    103 
    104   virtual ~DevToolsManager();
    105 
    106   // DevToolsClientHost::CloseListener override.
    107   // This method will remove all references from the manager to the
    108   // DevToolsClientHost and unregister all listeners related to the
    109   // DevToolsClientHost.
    110   virtual void ClientHostClosing(DevToolsClientHost* host);
    111 
    112   // Overridden from NotificationObserver:
    113   virtual void Observe(NotificationType type,
    114                        const NotificationSource& source,
    115                        const NotificationDetails& details);
    116 
    117   // Returns RenderViewHost for the tab that is inspected by devtools
    118   // client hosted by DevToolsClientHost.
    119   RenderViewHost* GetInspectedRenderViewHost(DevToolsClientHost* client_host);
    120 
    121   void SendAttachToAgent(RenderViewHost* inspected_rvh);
    122   void SendDetachToAgent(RenderViewHost* inspected_rvh);
    123 
    124   void ForceReopenWindow();
    125 
    126   DevToolsClientHost* FindOwnerDevToolsClientHost(RenderViewHost* client_rvh);
    127 
    128   void ToggleDevToolsWindow(RenderViewHost* inspected_rvh,
    129                             bool force_open,
    130                             DevToolsToggleAction action);
    131 
    132   void ReopenWindow(RenderViewHost* client_rvh, bool docked);
    133 
    134   void BindClientHost(RenderViewHost* inspected_rvh,
    135                       DevToolsClientHost* client_host,
    136                       const DevToolsRuntimeProperties& runtime_properties);
    137 
    138   void UnbindClientHost(RenderViewHost* inspected_rvh,
    139                         DevToolsClientHost* client_host);
    140 
    141   // These two maps are for tracking dependencies between inspected tabs and
    142   // their DevToolsClientHosts. They are useful for routing devtools messages
    143   // and allow us to have at most one devtools client host per tab.
    144   //
    145   // DevToolsManager start listening to DevToolsClientHosts when they are put
    146   // into these maps and removes them when they are closing.
    147   typedef std::map<RenderViewHost*, DevToolsClientHost*>
    148       InspectedRvhToClientHostMap;
    149   InspectedRvhToClientHostMap inspected_rvh_to_client_host_;
    150 
    151   typedef std::map<DevToolsClientHost*, RenderViewHost*>
    152       ClientHostToInspectedRvhMap;
    153   ClientHostToInspectedRvhMap client_host_to_inspected_rvh_;
    154 
    155   typedef std::map<RenderViewHost*, DevToolsRuntimeProperties>
    156       RuntimePropertiesMap;
    157   RuntimePropertiesMap runtime_properties_map_;
    158 
    159   RenderViewHost* inspected_rvh_for_reopen_;
    160   bool in_initial_show_;
    161 
    162   typedef std::map<int,
    163                    std::pair<DevToolsClientHost*, DevToolsRuntimeProperties> >
    164       OrphanClientHosts;
    165   OrphanClientHosts orphan_client_hosts_;
    166   int last_orphan_cookie_;
    167 
    168   NotificationRegistrar registrar_;
    169 
    170   DISALLOW_COPY_AND_ASSIGN(DevToolsManager);
    171 };
    172 
    173 #endif  // CHROME_BROWSER_DEBUGGER_DEVTOOLS_MANAGER_H_
    174