blob: dea1f774f82bc0958fc3ee5b9af6af7e6d4b2d55 [file] [log] [blame]
Darin Petkov50308cd2011-06-01 18:25:07 -07001// Copyright (c) 2011 The Chromium OS 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 SHILL_DHCP_PROVIDER_
6#define SHILL_DHCP_PROVIDER_
7
Darin Petkovd1b715b2011-06-02 21:21:22 -07008#include <map>
Darin Petkovaceede32011-07-18 15:32:38 -07009#include <string>
Darin Petkovd1b715b2011-06-02 21:21:22 -070010
Paul Stewart0d2ada32011-08-09 17:01:57 -070011#include <base/lazy_instance.h>
Darin Petkovd1b715b2011-06-02 21:21:22 -070012#include <base/memory/scoped_ptr.h>
Darin Petkovf7897bc2011-06-08 17:13:36 -070013#include <gtest/gtest_prod.h> // for FRIEND_TEST
Darin Petkovd1b715b2011-06-02 21:21:22 -070014
Chris Masone2b105542011-06-22 10:58:09 -070015#include "shill/refptr_types.h"
Darin Petkov50308cd2011-06-01 18:25:07 -070016
17namespace shill {
18
Chris Masone19e30402011-07-19 15:48:47 -070019class ControlInterface;
Darin Petkovaceede32011-07-18 15:32:38 -070020class DHCPCDListener;
Darin Petkova7b89492011-07-27 12:48:17 -070021class EventDispatcher;
Chris Masone2b105542011-06-22 10:58:09 -070022class GLib;
Darin Petkovab565bb2011-10-06 02:55:51 -070023class ProxyFactory;
Darin Petkovd1b715b2011-06-02 21:21:22 -070024
25// DHCPProvider is a singleton providing the main DHCP configuration
26// entrypoint. Once the provider is initialized through its Init method, DHCP
27// configurations for devices can be obtained through its CreateConfig
28// method. For example, a single DHCP configuration request can be initiated as:
29//
Darin Petkovf65e9282011-06-21 14:29:56 -070030// DHCPProvider::GetInstance()->CreateConfig(device_name)->Request();
Darin Petkov50308cd2011-06-01 18:25:07 -070031class DHCPProvider {
32 public:
Paul Stewart0d2ada32011-08-09 17:01:57 -070033 virtual ~DHCPProvider();
34
Darin Petkov50308cd2011-06-01 18:25:07 -070035 // This is a singleton -- use DHCPProvider::GetInstance()->Foo()
36 static DHCPProvider *GetInstance();
37
Darin Petkovd1b715b2011-06-02 21:21:22 -070038 // Initializes the provider singleton. This method hooks up a D-Bus signal
Darin Petkovaceede32011-07-18 15:32:38 -070039 // listener that catches signals from spawned DHCP clients and dispatches them
40 // to the appropriate DHCP configuration instance.
Darin Petkova7b89492011-07-27 12:48:17 -070041 void Init(ControlInterface *control_interface,
42 EventDispatcher *dispatcher,
43 GLib *glib);
Darin Petkovd1b715b2011-06-02 21:21:22 -070044
Darin Petkovf65e9282011-06-21 14:29:56 -070045 // Creates a new DHCPConfig for |device_name|. The DHCP configuration for the
Darin Petkovd1b715b2011-06-02 21:21:22 -070046 // device can then be initiated through DHCPConfig::Request and
47 // DHCPConfig::Renew.
Darin Petkov77cb6812011-08-15 16:19:41 -070048 virtual DHCPConfigRefPtr CreateConfig(const std::string &device_name);
Darin Petkovd1b715b2011-06-02 21:21:22 -070049
50 // Returns the DHCP configuration associated with DHCP client |pid|. Return
51 // NULL if |pid| is not bound to a configuration.
Darin Petkov98dd6a02011-06-10 15:12:57 -070052 DHCPConfigRefPtr GetConfig(int pid);
Darin Petkovd1b715b2011-06-02 21:21:22 -070053
54 // Binds a |pid| to a DHCP |config|. When a DHCP config spawns a new DHCP
55 // client, it binds itself to that client's |pid|.
Chris Masone2b105542011-06-22 10:58:09 -070056 void BindPID(int pid, const DHCPConfigRefPtr &config);
Darin Petkovd1b715b2011-06-02 21:21:22 -070057
58 // Unbinds a |pid|. This method is used by a DHCP config to signal the
59 // provider that the DHCP client has been terminated. This may result in
60 // destruction of the DHCP config instance if its reference count goes to 0.
Darin Petkov92c43902011-06-09 20:46:06 -070061 void UnbindPID(int pid);
Darin Petkovd1b715b2011-06-02 21:21:22 -070062
Paul Stewart0d2ada32011-08-09 17:01:57 -070063 protected:
64 DHCPProvider();
65
Darin Petkov50308cd2011-06-01 18:25:07 -070066 private:
Paul Stewart0d2ada32011-08-09 17:01:57 -070067 friend struct base::DefaultLazyInstanceTraits<DHCPProvider>;
Darin Petkov77cb6812011-08-15 16:19:41 -070068 friend class CellularTest;
Darin Petkov98dd6a02011-06-10 15:12:57 -070069 friend class DHCPProviderTest;
Darin Petkovafa6fc42011-06-21 16:21:08 -070070 friend class DeviceInfoTest;
71 friend class DeviceTest;
Darin Petkov98dd6a02011-06-10 15:12:57 -070072 FRIEND_TEST(DHCPProviderTest, CreateConfig);
Darin Petkovf7897bc2011-06-08 17:13:36 -070073
Darin Petkov92c43902011-06-09 20:46:06 -070074 typedef std::map<int, DHCPConfigRefPtr> PIDConfigMap;
Darin Petkov50308cd2011-06-01 18:25:07 -070075
Darin Petkovab565bb2011-10-06 02:55:51 -070076 // Store cached copies of singletons for speed/ease of testing.
77 ProxyFactory *proxy_factory_;
78
Darin Petkovd1b715b2011-06-02 21:21:22 -070079 // A single listener is used to catch signals from all DHCP clients and
Darin Petkovafa6fc42011-06-21 16:21:08 -070080 // dispatch them to the appropriate DHCP configuration instance.
Darin Petkovaceede32011-07-18 15:32:38 -070081 scoped_ptr<DHCPCDListener> listener_;
Darin Petkovd1b715b2011-06-02 21:21:22 -070082
83 // A map that binds PIDs to DHCP configuration instances.
84 PIDConfigMap configs_;
85
Chris Masone19e30402011-07-19 15:48:47 -070086 ControlInterface *control_interface_;
Darin Petkova7b89492011-07-27 12:48:17 -070087 EventDispatcher *dispatcher_;
88 GLib *glib_;
Darin Petkovf7897bc2011-06-08 17:13:36 -070089
Darin Petkov50308cd2011-06-01 18:25:07 -070090 DISALLOW_COPY_AND_ASSIGN(DHCPProvider);
91};
92
93} // namespace shill
94
95#endif // SHILL_DHCP_PROVIDER_