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_
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"
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
{
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
);
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 GetNetworks(const std::string
& network_type
,
72 const NetworkListCallback
& success_callback
,
73 const FailureCallback
& failure_callback
) override
;
74 void StartConnect(const std::string
& guid
,
75 const VoidCallback
& success_callback
,
76 const FailureCallback
& failure_callback
) override
;
77 void StartDisconnect(const std::string
& guid
,
78 const VoidCallback
& success_callback
,
79 const FailureCallback
& failure_callback
) override
;
80 void SetWifiTDLSEnabledState(
81 const std::string
& ip_or_mac_address
,
83 const StringCallback
& success_callback
,
84 const FailureCallback
& failure_callback
) override
;
85 void GetWifiTDLSStatus(const std::string
& ip_or_mac_address
,
86 const StringCallback
& success_callback
,
87 const FailureCallback
& failure_callback
) override
;
88 void GetCaptivePortalStatus(const std::string
& guid
,
89 const StringCallback
& success_callback
,
90 const FailureCallback
& failure_callback
) override
;
91 scoped_ptr
<base::ListValue
> GetEnabledNetworkTypes() override
;
92 bool EnableNetworkType(const std::string
& type
) override
;
93 bool DisableNetworkType(const std::string
& type
) override
;
94 bool RequestScan() override
;
95 void AddObserver(NetworkingPrivateDelegateObserver
* observer
) override
;
96 void RemoveObserver(NetworkingPrivateDelegateObserver
* observer
) override
;
98 // NetworkChangeNotifier::NetworkChangeObserver implementation.
99 void OnNetworkChanged(
100 net::NetworkChangeNotifier::ConnectionType type
) override
;
103 // Callbacks to extension api function objects. Keep reference to API object
104 // and are released in ShutdownOnUIThread. Run when WiFiService calls back
105 // into NetworkingPrivateServiceClient's callback wrappers.
106 typedef int32 ServiceCallbacksID
;
107 struct ServiceCallbacks
{
111 DictionaryCallback get_properties_callback
;
112 VoidCallback start_connect_callback
;
113 VoidCallback start_disconnect_callback
;
114 VoidCallback set_properties_callback
;
115 StringCallback create_network_callback
;
116 NetworkListCallback get_visible_networks_callback
;
117 FailureCallback failure_callback
;
119 ServiceCallbacksID id
;
121 typedef IDMap
<ServiceCallbacks
, IDMapOwnPointer
> ServiceCallbacksMap
;
123 ~NetworkingPrivateServiceClient() override
;
125 // Callback wrappers.
126 void AfterGetProperties(ServiceCallbacksID callback_id
,
127 const std::string
& network_guid
,
128 scoped_ptr
<base::DictionaryValue
> properties
,
129 const std::string
* error
);
130 void AfterSetProperties(ServiceCallbacksID callback_id
,
131 const std::string
* error
);
132 void AfterCreateNetwork(ServiceCallbacksID callback_id
,
133 const std::string
* network_guid
,
134 const std::string
* error
);
135 void AfterGetVisibleNetworks(ServiceCallbacksID callback_id
,
136 scoped_ptr
<base::ListValue
> networks
);
137 void AfterStartConnect(ServiceCallbacksID callback_id
,
138 const std::string
* error
);
139 void AfterStartDisconnect(ServiceCallbacksID callback_id
,
140 const std::string
* error
);
142 void OnNetworksChangedEventOnUIThread(
143 const wifi::WiFiService::NetworkGuidList
& network_guid_list
);
144 void OnNetworkListChangedEventOnUIThread(
145 const wifi::WiFiService::NetworkGuidList
& network_guid_list
);
147 // Add new |ServiceCallbacks| to |callbacks_map_|.
148 ServiceCallbacks
* AddServiceCallbacks();
149 // Removes ServiceCallbacks for |callback_id| from |callbacks_map_|.
150 void RemoveServiceCallbacks(ServiceCallbacksID callback_id
);
152 // Callbacks to run when callback is called from WiFiService.
153 ServiceCallbacksMap callbacks_map_
;
154 // Observers to Network Events.
155 ObserverList
<NetworkingPrivateDelegateObserver
> network_events_observers_
;
156 // Interface to WiFiService. Used and deleted on the worker thread.
157 scoped_ptr
<wifi::WiFiService
> wifi_service_
;
158 // Sequence token associated with wifi tasks.
159 base::SequencedWorkerPool::SequenceToken sequence_token_
;
160 // Task runner for worker tasks.
161 scoped_refptr
<base::SequencedTaskRunner
> task_runner_
;
162 // Use WeakPtrs for callbacks from |wifi_service_|.
163 base::WeakPtrFactory
<NetworkingPrivateServiceClient
> weak_factory_
;
165 DISALLOW_COPY_AND_ASSIGN(NetworkingPrivateServiceClient
);
168 } // namespace extensions
170 #endif // EXTENSIONS_BROWSER_API_NETWORKING_PRIVATE_NETWORKING_PRIVATE_SERVICE_CLIENT_H_