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 CONTENT_BROWSER_DEVTOOLS_WORKER_DEVTOOLS_MANAGER_H_
      6 #define CONTENT_BROWSER_DEVTOOLS_WORKER_DEVTOOLS_MANAGER_H_
      7 
      8 #include <list>
      9 #include <map>
     10 #include <string>
     11 
     12 #include "base/basictypes.h"
     13 #include "base/memory/singleton.h"
     14 #include "content/browser/worker_host/worker_process_host.h"
     15 #include "content/common/content_export.h"
     16 
     17 namespace content {
     18 
     19 class DevToolsAgentHost;
     20 
     21 // All methods are supposed to be called on the IO thread.
     22 // This class is not used when "enable-embedded-shared-worker" flag is set.
     23 class WorkerDevToolsManager {
     24  public:
     25   typedef std::pair<int, int> WorkerId;
     26   class WorkerDevToolsAgentHost;
     27 
     28   // Returns the WorkerDevToolsManager singleton.
     29   static WorkerDevToolsManager* GetInstance();
     30 
     31   // Called on the UI thread.
     32   static DevToolsAgentHost* GetDevToolsAgentHostForWorker(
     33       int worker_process_id,
     34       int worker_route_id);
     35 
     36   void ForwardToDevToolsClient(int worker_process_id,
     37                                int worker_route_id,
     38                                const std::string& message);
     39   void SaveAgentRuntimeState(int worker_process_id,
     40                              int worker_route_id,
     41                              const std::string& state);
     42 
     43   // Called on the IO thread.
     44   // Returns true when the worker must be paused on start.
     45   bool WorkerCreated(WorkerProcessHost* process,
     46                      const WorkerProcessHost::WorkerInstance& instance);
     47   void WorkerDestroyed(WorkerProcessHost* process, int worker_route_id);
     48   void WorkerContextStarted(WorkerProcessHost* process, int worker_route_id);
     49 
     50  private:
     51   friend struct DefaultSingletonTraits<WorkerDevToolsManager>;
     52   class DetachedClientHosts;
     53   struct InspectedWorker;
     54   typedef std::list<InspectedWorker> InspectedWorkersList;
     55 
     56   WorkerDevToolsManager();
     57   virtual ~WorkerDevToolsManager();
     58 
     59   void RemoveInspectedWorkerData(const WorkerId& id);
     60   InspectedWorkersList::iterator FindInspectedWorker(int host_id, int route_id);
     61 
     62   void ConnectDevToolsAgentHostToWorker(int worker_process_id,
     63                                         int worker_route_id);
     64   void ForwardToWorkerDevToolsAgent(int worker_process_host_id,
     65                                     int worker_route_id,
     66                                     const IPC::Message& message);
     67   static void ForwardToDevToolsClientOnUIThread(
     68       int worker_process_id,
     69       int worker_route_id,
     70       const std::string& message);
     71   static void SaveAgentRuntimeStateOnUIThread(
     72       int worker_process_id,
     73       int worker_route_id,
     74       const std::string& state);
     75   static void NotifyConnectionFailedOnIOThread(int worker_process_id,
     76                                                int worker_route_id);
     77   static void NotifyConnectionFailedOnUIThread(int worker_process_id,
     78                                                int worker_route_id);
     79   static void SendResumeToWorker(const WorkerId& id);
     80 
     81   InspectedWorkersList inspected_workers_;
     82 
     83   struct TerminatedInspectedWorker;
     84   typedef std::list<TerminatedInspectedWorker> TerminatedInspectedWorkers;
     85   // List of terminated workers for which there may be a devtools client on
     86   // the UI thread. Worker entry is added into this list when inspected worker
     87   // is terminated and will be removed in one of two cases:
     88   // - shared worker with the same URL and name is started(in wich case we will
     89   // try to reattach existing DevTools client to the new worker).
     90   // - DevTools client which was inspecting terminated worker is closed on the
     91   // UI thread and and WorkerDevToolsManager is notified about that on the IO
     92   // thread.
     93   TerminatedInspectedWorkers terminated_workers_;
     94 
     95   typedef std::map<WorkerId, WorkerId> PausedWorkers;
     96   // Map from old to new worker id for the inspected workers that have been
     97   // terminated and started again in paused state. Worker data will be removed
     98   // from this list in one of two cases:
     99   // - DevTools client is closed on the UI thread, WorkerDevToolsManager was
    100   // notified about that on the IO thread and sent "resume" message to the
    101   // worker.
    102   // - Existing DevTools client was reattached to the new worker.
    103   PausedWorkers paused_workers_;
    104 
    105   DISALLOW_COPY_AND_ASSIGN(WorkerDevToolsManager);
    106 };
    107 
    108 }  // namespace content
    109 
    110 #endif  // CONTENT_BROWSER_DEVTOOLS_WORKER_DEVTOOLS_MANAGER_H_
    111