system_network_context_manager.h 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright 2020 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. #ifndef WEBLAYER_BROWSER_SYSTEM_NETWORK_CONTEXT_MANAGER_H_
  5. #define WEBLAYER_BROWSER_SYSTEM_NETWORK_CONTEXT_MANAGER_H_
  6. #include "base/memory/scoped_refptr.h"
  7. #include "mojo/public/cpp/bindings/remote.h"
  8. #include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
  9. #include "services/network/public/mojom/network_context.mojom.h"
  10. #include "services/network/public/mojom/network_service.mojom.h"
  11. namespace network {
  12. class SharedURLLoaderFactory;
  13. } // namespace network
  14. namespace net_log {
  15. class NetExportFileWriter;
  16. }
  17. namespace weblayer {
  18. // Manages a system-wide network context that's not tied to a profile.
  19. class SystemNetworkContextManager {
  20. public:
  21. // Creates the global instance of SystemNetworkContextManager.
  22. static SystemNetworkContextManager* CreateInstance(
  23. const std::string& user_agent);
  24. // Checks if the global SystemNetworkContextManager has been created.
  25. static bool HasInstance();
  26. // Gets the global SystemNetworkContextManager instance or DCHECKs if there
  27. // isn't one..
  28. static SystemNetworkContextManager* GetInstance();
  29. // Destroys the global SystemNetworkContextManager instance.
  30. static void DeleteInstance();
  31. static network::mojom::NetworkContextParamsPtr
  32. CreateDefaultNetworkContextParams(const std::string& user_agent);
  33. static void ConfigureDefaultNetworkContextParams(
  34. network::mojom::NetworkContextParams* network_context_params,
  35. const std::string& user_agent);
  36. SystemNetworkContextManager(const SystemNetworkContextManager&) = delete;
  37. SystemNetworkContextManager& operator=(const SystemNetworkContextManager&) =
  38. delete;
  39. ~SystemNetworkContextManager();
  40. // Returns the System NetworkContext. Does any initialization of the
  41. // NetworkService that may be needed when first called.
  42. network::mojom::NetworkContext* GetSystemNetworkContext();
  43. // Called when content creates a NetworkService. Creates the
  44. // system NetworkContext, if the network service is enabled.
  45. void OnNetworkServiceCreated(network::mojom::NetworkService* network_service);
  46. // Returns a SharedURLLoaderFactory owned by the SystemNetworkContextManager
  47. // that is backed by the SystemNetworkContext.
  48. // NOTE: This factory assumes that the network service is running in the
  49. // browser process, which is a valid assumption for Android. If WebLayer is
  50. // productionized beyond Android, it will need to be extended to handle
  51. // network service crashes.
  52. scoped_refptr<network::SharedURLLoaderFactory> GetSharedURLLoaderFactory();
  53. // Returns a shared global NetExportFileWriter instance, used by net-export.
  54. // It lives here so it can outlive chrome://net-export/ if the tab is closed
  55. // or destroyed, and so that it's destroyed before Mojo is shut down.
  56. net_log::NetExportFileWriter* GetNetExportFileWriter();
  57. private:
  58. explicit SystemNetworkContextManager(const std::string& user_agent);
  59. network::mojom::NetworkContextParamsPtr
  60. CreateSystemNetworkContextManagerParams();
  61. std::string user_agent_;
  62. mojo::Remote<network::mojom::URLLoaderFactory> url_loader_factory_;
  63. scoped_refptr<network::WeakWrapperSharedURLLoaderFactory>
  64. shared_url_loader_factory_;
  65. mojo::Remote<network::mojom::NetworkContext> system_network_context_;
  66. // Initialized on first access.
  67. std::unique_ptr<net_log::NetExportFileWriter> net_export_file_writer_;
  68. };
  69. } // namespace weblayer
  70. #endif // WEBLAYER_BROWSER_SYSTEM_NETWORK_CONTEXT_MANAGER_H_