Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / extensions / browser / api / networking_private / networking_private_service_client.h
blobecaf22038eed3e44fcb84d4d0b3856417251dfe1
1 // Copyright 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.
5 #ifndef EXTENSIONS_BROWSER_API_NETWORKING_PRIVATE_NETWORKING_PRIVATE_SERVICE_CLIENT_H_
6 #define EXTENSIONS_BROWSER_API_NETWORKING_PRIVATE_NETWORKING_PRIVATE_SERVICE_CLIENT_H_
8 #include <string>
9 #include <vector>
11 #include "base/basictypes.h"
12 #include "base/id_map.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/observer_list.h"
16 #include "base/strings/string16.h"
17 #include "base/supports_user_data.h"
18 #include "base/threading/sequenced_worker_pool.h"
19 #include "base/values.h"
20 #include "components/keyed_service/core/keyed_service.h"
21 #include "components/wifi/wifi_service.h"
22 #include "content/public/browser/utility_process_host.h"
23 #include "content/public/browser/utility_process_host_client.h"
24 #include "extensions/browser/api/networking_private/networking_private_delegate.h"
25 #include "net/base/network_change_notifier.h"
27 namespace base {
28 class SequencedTaskRunner;
31 namespace extensions {
33 // Windows / Mac NetworkingPrivateApi implementation. This implements
34 // NetworkingPrivateDelegate, making WiFiService calls on the worker thead.
35 // It also observes |OnNetworkChanged| notifications and posts them to
36 // WiFiService on the worker thread. Created and called from the UI thread.
37 class NetworkingPrivateServiceClient
38 : public NetworkingPrivateDelegate,
39 net::NetworkChangeNotifier::NetworkChangeObserver {
40 public:
41 // Takes ownership of |wifi_service| which is accessed and deleted on the
42 // worker thread. The deletion task is posted from the destructor.
43 // |verify_delegate| is passed to NetworkingPrivateDelegate and may be NULL.
44 NetworkingPrivateServiceClient(scoped_ptr<wifi::WiFiService> wifi_service,
45 scoped_ptr<VerifyDelegate> verify_delegate);
47 // KeyedService
48 void Shutdown() override;
50 // NetworkingPrivateDelegate
51 void GetProperties(const std::string& guid,
52 const DictionaryCallback& success_callback,
53 const FailureCallback& failure_callback) override;
54 void GetManagedProperties(const std::string& guid,
55 const DictionaryCallback& success_callback,
56 const FailureCallback& failure_callback) override;
57 void GetState(const std::string& guid,
58 const DictionaryCallback& success_callback,
59 const FailureCallback& failure_callback) override;
60 void SetProperties(const std::string& guid,
61 scoped_ptr<base::DictionaryValue> properties_dict,
62 const VoidCallback& success_callback,
63 const FailureCallback& failure_callback) override;
64 void CreateNetwork(bool shared,
65 scoped_ptr<base::DictionaryValue> properties_dict,
66 const StringCallback& success_callback,
67 const FailureCallback& failure_callback) override;
68 void ForgetNetwork(const std::string& guid,
69 const VoidCallback& success_callback,
70 const FailureCallback& failure_callback) override;
71 void GetNetworks(const std::string& network_type,
72 bool configured_only,
73 bool visible_only,
74 int limit,
75 const NetworkListCallback& success_callback,
76 const FailureCallback& failure_callback) override;
77 void StartConnect(const std::string& guid,
78 const VoidCallback& success_callback,
79 const FailureCallback& failure_callback) override;
80 void StartDisconnect(const std::string& guid,
81 const VoidCallback& success_callback,
82 const FailureCallback& failure_callback) override;
83 void SetWifiTDLSEnabledState(
84 const std::string& ip_or_mac_address,
85 bool enabled,
86 const StringCallback& success_callback,
87 const FailureCallback& failure_callback) override;
88 void GetWifiTDLSStatus(const std::string& ip_or_mac_address,
89 const StringCallback& success_callback,
90 const FailureCallback& failure_callback) override;
91 void GetCaptivePortalStatus(const std::string& guid,
92 const StringCallback& success_callback,
93 const FailureCallback& failure_callback) override;
94 void UnlockCellularSim(const std::string& guid,
95 const std::string& pin,
96 const std::string& puk,
97 const VoidCallback& success_callback,
98 const FailureCallback& failure_callback) override;
99 void SetCellularSimState(const std::string& guid,
100 bool require_pin,
101 const std::string& current_pin,
102 const std::string& new_pin,
103 const VoidCallback& success_callback,
104 const FailureCallback& failure_callback) override;
105 scoped_ptr<base::ListValue> GetEnabledNetworkTypes() override;
106 scoped_ptr<DeviceStateList> GetDeviceStateList() override;
107 bool EnableNetworkType(const std::string& type) override;
108 bool DisableNetworkType(const std::string& type) override;
109 bool RequestScan() override;
110 void AddObserver(NetworkingPrivateDelegateObserver* observer) override;
111 void RemoveObserver(NetworkingPrivateDelegateObserver* observer) override;
113 // NetworkChangeNotifier::NetworkChangeObserver implementation.
114 void OnNetworkChanged(
115 net::NetworkChangeNotifier::ConnectionType type) override;
117 private:
118 // Callbacks to extension api function objects. Keep reference to API object
119 // and are released in ShutdownOnUIThread. Run when WiFiService calls back
120 // into NetworkingPrivateServiceClient's callback wrappers.
121 typedef int32 ServiceCallbacksID;
122 struct ServiceCallbacks {
123 ServiceCallbacks();
124 ~ServiceCallbacks();
126 DictionaryCallback get_properties_callback;
127 VoidCallback start_connect_callback;
128 VoidCallback start_disconnect_callback;
129 VoidCallback set_properties_callback;
130 StringCallback create_network_callback;
131 NetworkListCallback get_visible_networks_callback;
132 FailureCallback failure_callback;
134 ServiceCallbacksID id;
136 typedef IDMap<ServiceCallbacks, IDMapOwnPointer> ServiceCallbacksMap;
138 ~NetworkingPrivateServiceClient() override;
140 // Callback wrappers.
141 void AfterGetProperties(ServiceCallbacksID callback_id,
142 const std::string& network_guid,
143 scoped_ptr<base::DictionaryValue> properties,
144 const std::string* error);
145 void AfterSetProperties(ServiceCallbacksID callback_id,
146 const std::string* error);
147 void AfterCreateNetwork(ServiceCallbacksID callback_id,
148 const std::string* network_guid,
149 const std::string* error);
150 void AfterGetVisibleNetworks(ServiceCallbacksID callback_id,
151 scoped_ptr<base::ListValue> networks);
152 void AfterStartConnect(ServiceCallbacksID callback_id,
153 const std::string* error);
154 void AfterStartDisconnect(ServiceCallbacksID callback_id,
155 const std::string* error);
157 void OnNetworksChangedEventOnUIThread(
158 const wifi::WiFiService::NetworkGuidList& network_guid_list);
159 void OnNetworkListChangedEventOnUIThread(
160 const wifi::WiFiService::NetworkGuidList& network_guid_list);
162 // Add new |ServiceCallbacks| to |callbacks_map_|.
163 ServiceCallbacks* AddServiceCallbacks();
164 // Removes ServiceCallbacks for |callback_id| from |callbacks_map_|.
165 void RemoveServiceCallbacks(ServiceCallbacksID callback_id);
167 // Callbacks to run when callback is called from WiFiService.
168 ServiceCallbacksMap callbacks_map_;
169 // Observers to Network Events.
170 base::ObserverList<NetworkingPrivateDelegateObserver>
171 network_events_observers_;
172 // Interface to WiFiService. Used and deleted on the worker thread.
173 scoped_ptr<wifi::WiFiService> wifi_service_;
174 // Sequence token associated with wifi tasks.
175 base::SequencedWorkerPool::SequenceToken sequence_token_;
176 // Task runner for worker tasks.
177 scoped_refptr<base::SequencedTaskRunner> task_runner_;
178 // Use WeakPtrs for callbacks from |wifi_service_|.
179 base::WeakPtrFactory<NetworkingPrivateServiceClient> weak_factory_;
181 DISALLOW_COPY_AND_ASSIGN(NetworkingPrivateServiceClient);
184 } // namespace extensions
186 #endif // EXTENSIONS_BROWSER_API_NETWORKING_PRIVATE_NETWORKING_PRIVATE_SERVICE_CLIENT_H_