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 THIRD_PARTY_BLINK_RENDERER_CORE_WORKERS_INSTALLED_SCRIPTS_MANAGER_H_
6 #define THIRD_PARTY_BLINK_RENDERER_CORE_WORKERS_INSTALLED_SCRIPTS_MANAGER_H_
7 
8 #include "base/optional.h"
9 #include "services/network/public/mojom/ip_address_space.mojom-blink-forward.h"
10 #include "third_party/blink/renderer/core/core_export.h"
11 #include "third_party/blink/renderer/platform/network/content_security_policy_response_headers.h"
12 #include "third_party/blink/renderer/platform/network/http_header_map.h"
13 #include "third_party/blink/renderer/platform/weborigin/kurl.h"
14 #include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
15 #include "third_party/blink/renderer/platform/wtf/vector.h"
16 
17 namespace blink {
18 
19 // InstalledScriptsManager provides the scripts of workers that have been
20 // installed. Currently it is only used for installed service workers.
21 class InstalledScriptsManager {
22   USING_FAST_MALLOC(InstalledScriptsManager);
23 
24  public:
25   InstalledScriptsManager() = default;
26 
27   class CORE_EXPORT ScriptData {
28     USING_FAST_MALLOC(ScriptData);
29 
30    public:
31     ScriptData() = default;
32     ScriptData(const KURL& script_url,
33                String source_text,
34                std::unique_ptr<Vector<uint8_t>> meta_data,
35                std::unique_ptr<CrossThreadHTTPHeaderMapData>);
36     ScriptData(ScriptData&& other) = default;
37     ScriptData& operator=(ScriptData&& other) = default;
38 
TakeSourceText()39     String TakeSourceText() { return std::move(source_text_); }
TakeMetaData()40     std::unique_ptr<Vector<uint8_t>> TakeMetaData() {
41       return std::move(meta_data_);
42     }
43 
44     ContentSecurityPolicyResponseHeaders
45     GetContentSecurityPolicyResponseHeaders();
46     String GetReferrerPolicy();
47     String GetHttpContentType();
GetResponseAddressSpace()48     network::mojom::IPAddressSpace GetResponseAddressSpace() const {
49       return response_address_space_;
50     }
51     std::unique_ptr<Vector<String>> CreateOriginTrialTokens();
52 
53    private:
54     KURL script_url_;
55     String source_text_;
56     std::unique_ptr<Vector<uint8_t>> meta_data_;
57     HTTPHeaderMap headers_;
58     network::mojom::IPAddressSpace response_address_space_;
59 
60     DISALLOW_COPY_AND_ASSIGN(ScriptData);
61   };
62 
63   // Used on the main or worker thread. Returns true if the script has been
64   // installed.
65   virtual bool IsScriptInstalled(const KURL& script_url) const = 0;
66 
67   // Used on the worker thread. Returning nullptr indicates an error
68   // happened while receiving the script from the browser process.
69   // This can block if the script has not been received from the browser process
70   // yet.
71   virtual std::unique_ptr<ScriptData> GetScriptData(const KURL& script_url) = 0;
72 };
73 
74 }  // namespace blink
75 
76 #endif  // THIRD_PARTY_BLINK_RENDERER_CORE_WORKERS_INSTALLED_SCRIPTS_MANAGER_H_
77