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_FILE_URL_LOADER_H_
6 #define CONTENT_PUBLIC_BROWSER_FILE_URL_LOADER_H_
7 
8 #include <memory>
9 
10 #include "base/memory/ref_counted.h"
11 #include "content/common/content_export.h"
12 #include "mojo/public/cpp/bindings/pending_receiver.h"
13 #include "mojo/public/cpp/bindings/pending_remote.h"
14 #include "mojo/public/cpp/system/filtered_data_source.h"
15 #include "net/http/http_response_headers.h"
16 #include "services/network/public/mojom/url_loader.mojom.h"
17 
18 namespace network {
19 namespace mojom {
20 class URLLoaderFactory;
21 }
22 }  // namespace network
23 
24 namespace content {
25 
26 class SharedCorsOriginAccessList;
27 
28 class CONTENT_EXPORT FileURLLoaderObserver
29     : public mojo::FilteredDataSource::Filter {
30  public:
FileURLLoaderObserver()31   FileURLLoaderObserver() {}
~FileURLLoaderObserver()32   ~FileURLLoaderObserver() override {}
33 
OnStart()34   virtual void OnStart() {}
OnSeekComplete(int64_t result)35   virtual void OnSeekComplete(int64_t result) {}
36 
37  private:
38   DISALLOW_COPY_AND_ASSIGN(FileURLLoaderObserver);
39 };
40 
41 // Helper to create a self-owned URLLoader instance which fulfills |request|
42 // using the contents of the file at |path|. The URL in |request| must be a
43 // file:// URL. The *optionally* supplied |observer| will be called to report
44 // progress during the file loading.
45 //
46 // Note that this does not restrict filesystem access *in any way*, so if the
47 // file at |path| is accessible to the browser, it will be loaded and used to
48 // the request.
49 //
50 // The URLLoader created by this function does *not* automatically follow
51 // filesytem links (e.g. Windows shortcuts) or support directory listing.
52 // A directory path will always yield a FILE_NOT_FOUND network error.
53 CONTENT_EXPORT void CreateFileURLLoaderBypassingSecurityChecks(
54     const network::ResourceRequest& request,
55     mojo::PendingReceiver<network::mojom::URLLoader> loader,
56     mojo::PendingRemote<network::mojom::URLLoaderClient> client,
57     std::unique_ptr<FileURLLoaderObserver> observer,
58     bool allow_directory_listing,
59     scoped_refptr<net::HttpResponseHeaders> extra_response_headers = nullptr);
60 
61 // Helper to create a FileURLLoaderFactory instance. This exposes the ability
62 // to load file:// URLs through SimpleURLLoader to non-content classes.
63 //
64 // When non-empty, |profile_path| is used to whitelist specific directories on
65 // ChromeOS and Android. It is checked by
66 // ContentBrowserClient::IsFileAccessAllowed.
67 // |shared_cors_origin_access_list| can be specified if caller wants only
68 // listed access pattern to be permitted for CORS requests. If nullptr is
69 // passed, all file accesses are permitted even for CORS requests. This list
70 // does not affect no-cors requests.
71 CONTENT_EXPORT std::unique_ptr<network::mojom::URLLoaderFactory>
72 CreateFileURLLoaderFactory(
73     const base::FilePath& profile_path,
74     scoped_refptr<SharedCorsOriginAccessList> shared_cors_origin_access_list);
75 
76 }  // namespace content
77 
78 #endif  // CONTENT_PUBLIC_BROWSER_FILE_URL_LOADER_H_
79