• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2019 The Chromium Embedded Framework Authors. Portions
2 // Copyright (c) 2018 The Chromium Authors. All rights reserved. Use of this
3 // source code is governed by a BSD-style license that can be found in the
4 // LICENSE file.
5 
6 #ifndef CEF_LIBCEF_BROWSER_NET_SERVICE_URL_LOADER_FACTORY_GETTER_H_
7 #define CEF_LIBCEF_BROWSER_NET_SERVICE_URL_LOADER_FACTORY_GETTER_H_
8 
9 #include "base/memory/ref_counted.h"
10 #include "base/task/sequenced_task_runner.h"
11 #include "base/task/sequenced_task_runner_helpers.h"
12 #include "services/network/public/mojom/url_loader_factory.mojom.h"
13 
14 namespace content {
15 class BrowserContext;
16 class RenderFrameHost;
17 }  // namespace content
18 
19 namespace network {
20 class SharedURLLoaderFactory;
21 class PendingSharedURLLoaderFactory;
22 }  // namespace network
23 
24 namespace net_service {
25 
26 struct URLLoaderFactoryGetterDeleter;
27 
28 // Helper class for retrieving a URLLoaderFactory that can be bound on any
29 // thread, and that correctly handles proxied requests.
30 class URLLoaderFactoryGetter
31     : public base::RefCountedThreadSafe<URLLoaderFactoryGetter,
32                                         URLLoaderFactoryGetterDeleter> {
33  public:
34   URLLoaderFactoryGetter(const URLLoaderFactoryGetter&) = delete;
35   URLLoaderFactoryGetter& operator=(const URLLoaderFactoryGetter&) = delete;
36 
37   // Create a URLLoaderFactoryGetter on the UI thread.
38   // |render_frame_host| may be nullptr.
39   static scoped_refptr<URLLoaderFactoryGetter> Create(
40       content::RenderFrameHost* render_frame_host,
41       content::BrowserContext* browser_context);
42 
43   // Create a SharedURLLoaderFactory on the current thread. All future calls
44   // to this method must be on the same thread.
45   scoped_refptr<network::SharedURLLoaderFactory> GetURLLoaderFactory();
46 
47  private:
48   friend class base::DeleteHelper<URLLoaderFactoryGetter>;
49   friend class base::RefCountedThreadSafe<URLLoaderFactoryGetter,
50                                           URLLoaderFactoryGetterDeleter>;
51   friend struct URLLoaderFactoryGetterDeleter;
52 
53   URLLoaderFactoryGetter(
54       std::unique_ptr<network::PendingSharedURLLoaderFactory>
55           loader_factory_info,
56       network::mojom::URLLoaderFactoryPtrInfo proxy_factory_ptr_info,
57       network::mojom::URLLoaderFactoryRequest proxy_factory_request);
58   ~URLLoaderFactoryGetter();
59 
60   void DeleteOnCorrectThread() const;
61 
62   std::unique_ptr<network::PendingSharedURLLoaderFactory> loader_factory_info_;
63   scoped_refptr<network::SharedURLLoaderFactory> lazy_factory_;
64   network::mojom::URLLoaderFactoryPtrInfo proxy_factory_ptr_info_;
65   network::mojom::URLLoaderFactoryRequest proxy_factory_request_;
66   scoped_refptr<base::SequencedTaskRunner> task_runner_;
67 };
68 
69 struct URLLoaderFactoryGetterDeleter {
DestructURLLoaderFactoryGetterDeleter70   static void Destruct(const URLLoaderFactoryGetter* factory_getter) {
71     factory_getter->DeleteOnCorrectThread();
72   }
73 };
74 
75 }  // namespace net_service
76 
77 #endif  // CEF_LIBCEF_BROWSER_NET_SERVICE_URL_LOADER_FACTORY_GETTER_H_
78