summaryrefslogtreecommitdiffstats
path: root/components/devtools_service/devtools_registry_impl.h
blob: 431f39a601c9a938b0cd091909edb1dc5f5477d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_DEVTOOLS_SERVICE_DEVTOOLS_REGISTRY_IMPL_H_
#define COMPONENTS_DEVTOOLS_SERVICE_DEVTOOLS_REGISTRY_IMPL_H_

#include <map>
#include <string>

#include "base/macros.h"
#include "base/memory/linked_ptr.h"
#include "components/devtools_service/public/interfaces/devtools_service.mojom.h"
#include "mojo/common/weak_binding_set.h"

namespace devtools_service {

class DevToolsAgentHost;
class DevToolsService;

class DevToolsRegistryImpl : public DevToolsRegistry {
 public:
  class Iterator {
   public:
    // |registry| must outlive this object.
    explicit Iterator(DevToolsRegistryImpl* registry);
    ~Iterator();

    bool IsAtEnd() const { return iter_ == registry_->agents_.end(); }
    void Advance() { ++iter_; }

    DevToolsAgentHost* value() { return iter_->second.get(); }

   private:
    DevToolsRegistryImpl* const registry_;
    std::map<std::string, linked_ptr<DevToolsAgentHost>>::const_iterator iter_;
  };

  // |service| must outlive this object.
  explicit DevToolsRegistryImpl(DevToolsService* service);
  ~DevToolsRegistryImpl() override;

  void BindToRegistryRequest(mojo::InterfaceRequest<DevToolsRegistry> request);

  DevToolsAgentHost* GetAgentById(const std::string& id);

 private:
  // DevToolsRegistry implementation.
  void RegisterAgent(DevToolsAgentPtr agent) override;

  void OnAgentConnectionError(const std::string& id);

  DevToolsService* const service_;

  mojo::WeakBindingSet<DevToolsRegistry> bindings_;

  std::map<std::string, linked_ptr<DevToolsAgentHost>> agents_;

  DISALLOW_COPY_AND_ASSIGN(DevToolsRegistryImpl);
};

}  // namespace devtools_service

#endif  // COMPONENTS_DEVTOOLS_SERVICE_DEVTOOLS_REGISTRY_IMPL_H_