summaryrefslogtreecommitdiffstats
path: root/chromium/third_party/blink/renderer/bindings/core/v8/source_location.h
blob: 2e324f2875d85e287ace68707c819c7c7ff39154 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef THIRD_PARTY_BLINK_RENDERER_BINDINGS_CORE_V8_SOURCE_LOCATION_H_
#define THIRD_PARTY_BLINK_RENDERER_BINDINGS_CORE_V8_SOURCE_LOCATION_H_

#include <v8-inspector-protocol.h>
#include <memory>
#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/platform/wtf/forward.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"

namespace blink {

class ExecutionContext;
class TracedValue;

class CORE_EXPORT SourceLocation {
 public:
  // Zero lineNumber and columnNumber mean unknown. Captures current stack
  // trace.
  static std::unique_ptr<SourceLocation> Capture(const String& url,
                                                 unsigned line_number,
                                                 unsigned column_number);

  // Shortcut when location is unknown. Tries to capture call stack or parsing
  // location if available.
  static std::unique_ptr<SourceLocation> Capture(ExecutionContext* = nullptr);

  static std::unique_ptr<SourceLocation> FromMessage(v8::Isolate*,
                                                     v8::Local<v8::Message>,
                                                     ExecutionContext*);

  static std::unique_ptr<SourceLocation> FromFunction(v8::Local<v8::Function>);

  // Forces full stack trace.
  static std::unique_ptr<SourceLocation> CaptureWithFullStackTrace();

  static std::unique_ptr<SourceLocation> Create(
      const String& url,
      unsigned line_number,
      unsigned column_number,
      std::unique_ptr<v8_inspector::V8StackTrace>,
      int script_id = 0);
  ~SourceLocation();

  bool IsUnknown() const {
    return url_.IsNull() && !script_id_ && !line_number_;
  }
  const String& Url() const { return url_; }
  unsigned LineNumber() const { return line_number_; }
  unsigned ColumnNumber() const { return column_number_; }
  int ScriptId() const { return script_id_; }
  std::unique_ptr<v8_inspector::V8StackTrace> TakeStackTrace() {
    return std::move(stack_trace_);
  }

  std::unique_ptr<SourceLocation> Clone()
      const;  // Safe to pass between threads.

  // No-op when stack trace is unknown.
  void ToTracedValue(TracedValue*, const char* name) const;

  // Could be null string when stack trace is unknown.
  String ToString() const;

  // Could be null when stack trace is unknown.
  std::unique_ptr<v8_inspector::protocol::Runtime::API::StackTrace>
  BuildInspectorObject() const;

 private:
  SourceLocation(const String& url,
                 unsigned line_number,
                 unsigned column_number,
                 std::unique_ptr<v8_inspector::V8StackTrace>,
                 int script_id);
  static std::unique_ptr<SourceLocation> CreateFromNonEmptyV8StackTrace(
      std::unique_ptr<v8_inspector::V8StackTrace>,
      int script_id);

  String url_;
  unsigned line_number_;
  unsigned column_number_;
  std::unique_ptr<v8_inspector::V8StackTrace> stack_trace_;
  int script_id_;
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_BINDINGS_CORE_V8_SOURCE_LOCATION_H_