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_SCRIPT_CLASSIC_SCRIPT_H_
6 #define THIRD_PARTY_BLINK_RENDERER_CORE_SCRIPT_CLASSIC_SCRIPT_H_
7 
8 #include "third_party/blink/renderer/bindings/core/v8/sanitize_script_errors.h"
9 #include "third_party/blink/renderer/bindings/core/v8/script_controller.h"
10 #include "third_party/blink/renderer/bindings/core/v8/script_source_code.h"
11 #include "third_party/blink/renderer/core/core_export.h"
12 #include "third_party/blink/renderer/core/loader/resource/script_resource.h"
13 #include "third_party/blink/renderer/core/script/script.h"
14 #include "third_party/blink/renderer/platform/loader/fetch/script_fetch_options.h"
15 
16 namespace blink {
17 
18 class CORE_EXPORT ClassicScript final : public Script {
19  public:
20   // For scripts specified in the HTML spec.
21   // Please leave spec comments and spec links that explain given argument
22   // values at callers.
ClassicScript(const ScriptSourceCode & script_source_code,const KURL & base_url,const ScriptFetchOptions & fetch_options,SanitizeScriptErrors sanitize_script_errors)23   ClassicScript(const ScriptSourceCode& script_source_code,
24                 const KURL& base_url,
25                 const ScriptFetchOptions& fetch_options,
26                 SanitizeScriptErrors sanitize_script_errors)
27       : Script(fetch_options, base_url),
28         script_source_code_(script_source_code),
29         sanitize_script_errors_(sanitize_script_errors) {}
30 
31   // For scripts not specified in the HTML spec.
32   //
33   // New callers should use SanitizeScriptErrors::kSanitize as a safe default
34   // value, while some existing callers uses kDoNotSanitize to preserve existing
35   // behavior.
36   // TODO(crbug/1112266): Use kSanitize for all existing callers if possible, or
37   // otherwise add comments why kDoNotSanitize should be used.
38   static ClassicScript* CreateUnspecifiedScript(
39       const ScriptSourceCode&,
40       SanitizeScriptErrors = SanitizeScriptErrors::kSanitize);
41 
42   void Trace(Visitor*) const override;
43 
GetScriptSourceCode()44   const ScriptSourceCode& GetScriptSourceCode() const {
45     return script_source_code_;
46   }
47 
48   // TODO(crbug/1111134): The RunScript() with ExecuteScriptPolicy is declared
49   // and overloaded here to avoid modifying Script::RunScript(), because this is
50   // a tentative interface. When crbug/1111134 is done, this should be gone.
51   void RunScript(LocalDOMWindow*) override;
52   void RunScript(LocalDOMWindow*, ScriptController::ExecuteScriptPolicy);
53   bool RunScriptOnWorkerOrWorklet(WorkerOrWorkletGlobalScope&) override;
54 
55   // Unlike RunScript() and RunScriptOnWorkerOrWorklet(), callers of the
56   // following methods must enter a v8::HandleScope before calling.
57   v8::Local<v8::Value> RunScriptAndReturnValue(
58       LocalDOMWindow*,
59       ScriptController::ExecuteScriptPolicy = ScriptController::
60           ExecuteScriptPolicy::kDoNotExecuteScriptWhenScriptsDisabled);
61   v8::Local<v8::Value> RunScriptInIsolatedWorldAndReturnValue(LocalDOMWindow*,
62                                                               int32_t world_id);
63 
64  private:
GetScriptType()65   mojom::blink::ScriptType GetScriptType() const override {
66     return mojom::blink::ScriptType::kClassic;
67   }
68 
69   std::pair<size_t, size_t> GetClassicScriptSizes() const override;
70 
71   const ScriptSourceCode script_source_code_;
72   const SanitizeScriptErrors sanitize_script_errors_;
73 };
74 
75 }  // namespace blink
76 
77 #endif
78