1 // Copyright 2017 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 CONTENT_PUBLIC_BROWSER_NETWORK_SERVICE_INSTANCE_H_ 6 #define CONTENT_PUBLIC_BROWSER_NETWORK_SERVICE_INSTANCE_H_ 7 8 #include <memory> 9 10 #include "base/callback.h" 11 #include "base/callback_list.h" 12 #include "build/build_config.h" 13 #include "content/common/content_export.h" 14 #include "services/network/public/cpp/network_connection_tracker.h" 15 16 namespace base { 17 class SequencedTaskRunner; 18 } 19 20 namespace net { 21 class NetworkChangeNotifier; 22 } // namespace net 23 24 namespace network { 25 class NetworkService; 26 namespace mojom { 27 class NetworkService; 28 } 29 } // namespace network 30 31 namespace content { 32 33 // Returns a pointer to the NetworkService, creating / re-creating it as needed. 34 // NetworkService will be running in-process if 35 // 1) kNetworkService feature is disabled, or 36 // 2) kNetworkService and kNetworkServiceInProcess are enabled 37 // Otherwise it runs out of process. 38 // This method can only be called on the UI thread. 39 CONTENT_EXPORT network::mojom::NetworkService* GetNetworkService(); 40 41 // Only on ChromeOS since it's only used there. 42 #if defined(OS_CHROMEOS) 43 // Returns the global NetworkChangeNotifier instance. 44 CONTENT_EXPORT net::NetworkChangeNotifier* GetNetworkChangeNotifier(); 45 #endif 46 47 // Call |FlushForTesting()| on cached |mojo::Remote<NetworkService>|. For 48 // testing only. Must only be called on the UI thread. 49 CONTENT_EXPORT void FlushNetworkServiceInstanceForTesting(); 50 51 // Returns a NetworkConnectionTracker that can be used to subscribe for 52 // network change events. 53 // Must only be called on the UI thread. 54 CONTENT_EXPORT network::NetworkConnectionTracker* GetNetworkConnectionTracker(); 55 56 // Asynchronously calls the given callback with a NetworkConnectionTracker that 57 // can be used to subscribe to network change events. 58 // 59 // This is a helper method for classes that can't easily call 60 // GetNetworkConnectionTracker from the UI thread. 61 CONTENT_EXPORT void GetNetworkConnectionTrackerFromUIThread( 62 base::OnceCallback<void(network::NetworkConnectionTracker*)> callback); 63 64 // Helper method to create a NetworkConnectionTrackerAsyncGetter. 65 CONTENT_EXPORT network::NetworkConnectionTrackerAsyncGetter 66 CreateNetworkConnectionTrackerAsyncGetter(); 67 68 // Sets the NetworkConnectionTracker instance to use. For testing only. 69 // Must be called on the UI thread. Must be called before the first call to 70 // GetNetworkConnectionTracker. 71 CONTENT_EXPORT void SetNetworkConnectionTrackerForTesting( 72 network::NetworkConnectionTracker* network_connection_tracker); 73 74 // Gets the task runner for the thread the network service will be running on 75 // when running in-process. Can only be called when network service is in 76 // process. 77 CONTENT_EXPORT const scoped_refptr<base::SequencedTaskRunner>& 78 GetNetworkTaskRunner(); 79 80 } // namespace content 81 82 #endif // CONTENT_PUBLIC_BROWSER_NETWORK_SERVICE_INSTANCE_H_ 83