Home | History | Annotate | Download | only in devtools
      1 // Copyright (c) 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 
      5 #ifndef CHROME_BROWSER_DEVTOOLS_TETHERING_ADB_FILTER_H_
      6 #define CHROME_BROWSER_DEVTOOLS_TETHERING_ADB_FILTER_H_
      7 
      8 #include <map>
      9 #include <string>
     10 
     11 #include "base/basictypes.h"
     12 #include "base/memory/ref_counted.h"
     13 #include "base/prefs/pref_change_registrar.h"
     14 #include "chrome/browser/devtools/adb_web_socket.h"
     15 #include "chrome/browser/devtools/devtools_adb_bridge.h"
     16 
     17 class PrefService;
     18 
     19 namespace base {
     20 class MessageLoop;
     21 }
     22 
     23 class TetheringAdbFilter : public base::RefCountedThreadSafe<
     24     TetheringAdbFilter,
     25     content::BrowserThread::DeleteOnUIThread> {
     26  public:
     27   typedef DevToolsAdbBridge::RemoteDevice::PortStatus PortStatus;
     28   typedef DevToolsAdbBridge::RemoteDevice::PortStatusMap PortStatusMap;
     29 
     30   TetheringAdbFilter(scoped_refptr<DevToolsAdbBridge::AndroidDevice> device,
     31                      base::MessageLoop* adb_message_loop,
     32                      PrefService* pref_service,
     33                      scoped_refptr<AdbWebSocket> web_socket);
     34 
     35   const PortStatusMap& GetPortStatusMap();
     36 
     37   bool ProcessIncomingMessage(const std::string& message);
     38 
     39  private:
     40   friend struct content::BrowserThread::DeleteOnThread<
     41       content::BrowserThread::UI>;
     42   friend class base::DeleteHelper<TetheringAdbFilter>;
     43 
     44   virtual ~TetheringAdbFilter();
     45 
     46   typedef std::map<int, std::string> ForwardingMap;
     47 
     48   typedef base::Callback<void(PortStatus)> CommandCallback;
     49   typedef std::map<int, CommandCallback> CommandCallbackMap;
     50 
     51   void OnPrefsChange();
     52 
     53   void ChangeForwardingMap(ForwardingMap map);
     54 
     55   void SerializeChanges(const std::string& method,
     56                         const ForwardingMap& old_map,
     57                         const ForwardingMap& new_map);
     58 
     59   void SendCommand(const std::string& method, int port);
     60   bool ProcessResponse(const std::string& json);
     61 
     62   void ProcessBindResponse(int port, PortStatus status);
     63   void ProcessUnbindResponse(int port, PortStatus status);
     64   void UpdateSocketCount(int port, int increment);
     65   void UpdatePortStatusMap();
     66   void UpdatePortStatusMapOnUIThread(const PortStatusMap& status_map);
     67 
     68   scoped_refptr<DevToolsAdbBridge::AndroidDevice> device_;
     69   base::MessageLoop* adb_message_loop_;
     70   PrefChangeRegistrar pref_change_registrar_;
     71   scoped_refptr<AdbWebSocket> web_socket_;
     72   int command_id_;
     73   ForwardingMap forwarding_map_;
     74   CommandCallbackMap pending_responses_;
     75   PortStatusMap port_status_;
     76   PortStatusMap port_status_on_ui_thread_;
     77 
     78   DISALLOW_COPY_AND_ASSIGN(TetheringAdbFilter);
     79 };
     80 
     81 class PortForwardingController {
     82  public:
     83   PortForwardingController(
     84       base::MessageLoop* adb_message_loop,
     85       PrefService* pref_service);
     86 
     87   virtual ~PortForwardingController();
     88 
     89   void UpdateDeviceList(const DevToolsAdbBridge::RemoteDevices& devices);
     90 
     91  private:
     92   class Connection;
     93   typedef std::map<std::string, Connection*> Registry;
     94 
     95   base::MessageLoop* adb_message_loop_;
     96   PrefService* pref_service_;
     97   Registry registry_;
     98 };
     99 
    100 #endif  // CHROME_BROWSER_DEVTOOLS_TETHERING_ADB_FILTER_H_
    101