Skip to content

Commit

Permalink
Clean-up ScriptSourceCode
Browse files Browse the repository at this point in the history
Makes most of the members of ScriptSourceCode const.

Bug: 788828, 686281
Change-Id: I112ed0a2dd8e6a6e84647f49dc61ad4a840d9413
Reviewed-on: https://chromium-review.googlesource.com/666242
Commit-Queue: Kouhei Ueno <kouhei@chromium.org>
Reviewed-by: Hiroshige Hayashizaki <hiroshige@chromium.org>
Cr-Commit-Position: refs/heads/master@{#522580}
  • Loading branch information
nyaxt authored and Commit Bot committed Dec 7, 2017
1 parent 4942f56 commit 7f4ce6e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 37 deletions.
73 changes: 43 additions & 30 deletions third_party/WebKit/Source/bindings/core/v8/ScriptSourceCode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,57 @@

namespace blink {

namespace {

String TreatNullSourceAsEmpty(const String& source) {
// ScriptSourceCode allows for the representation of the null/not-there-really
// ScriptSourceCode value. Encoded by way of a m_source.isNull() being true,
// with the nullary constructor to be used to construct such a value.
//
// Should the other constructors be passed a null string, that is interpreted
// as representing the empty script. Consequently, we need to disambiguate
// between such null string occurrences. Do that by converting the latter
// case's null strings into empty ones.
if (source.IsNull())
return "";

return source;
}

KURL StripFragmentIdentifier(const KURL& url) {
if (url.IsEmpty())
return KURL();

if (!url.HasFragmentIdentifier())
return url;

KURL copy = url;
copy.RemoveFragmentIdentifier();
return copy;
}

} // namespace

ScriptSourceCode::ScriptSourceCode(
const String& source,
ScriptSourceLocationType source_location_type,
const KURL& url,
const TextPosition& start_position)
: source_(source),
url_(url),
: source_(TreatNullSourceAsEmpty(source)),
url_(StripFragmentIdentifier(url)),
start_position_(start_position),
source_location_type_(source_location_type) {
// External files should use a ScriptResource.
DCHECK(source_location_type != ScriptSourceLocationType::kExternalFile);

TreatNullSourceAsEmpty();
if (!url_.IsEmpty())
url_.RemoveFragmentIdentifier();
}

ScriptSourceCode::ScriptSourceCode(ScriptStreamer* streamer,
ScriptResource* resource)
: source_(resource->SourceText()),
: source_(TreatNullSourceAsEmpty(resource->SourceText())),
resource_(resource),
streamer_(streamer),
start_position_(TextPosition::MinimumPosition()),
source_location_type_(ScriptSourceLocationType::kExternalFile) {
TreatNullSourceAsEmpty();
}
source_location_type_(ScriptSourceLocationType::kExternalFile) {}

ScriptSourceCode::~ScriptSourceCode() {}

Expand All @@ -40,13 +65,14 @@ void ScriptSourceCode::Trace(blink::Visitor* visitor) {
visitor->Trace(streamer_);
}

const KURL& ScriptSourceCode::Url() const {
if (url_.IsEmpty() && resource_) {
url_ = resource_->GetResponse().Url();
if (!url_.IsEmpty())
url_.RemoveFragmentIdentifier();
}
return url_;
KURL ScriptSourceCode::Url() const {
if (!url_.IsEmpty())
return url_;

if (resource_)
return StripFragmentIdentifier(resource_->GetResponse().Url());

return KURL();
}

String ScriptSourceCode::SourceMapUrl() const {
Expand All @@ -61,17 +87,4 @@ String ScriptSourceCode::SourceMapUrl() const {
return source_map_url;
}

void ScriptSourceCode::TreatNullSourceAsEmpty() {
// ScriptSourceCode allows for the representation of the null/not-there-really
// ScriptSourceCode value. Encoded by way of a m_source.isNull() being true,
// with the nullary constructor to be used to construct such a value.
//
// Should the other constructors be passed a null string, that is interpreted
// as representing the empty script. Consequently, we need to disambiguate
// between such null string occurrences. Do that by converting the latter
// case's null strings into empty ones.
if (source_.IsNull())
source_ = "";
}

} // namespace blink
12 changes: 5 additions & 7 deletions third_party/WebKit/Source/bindings/core/v8/ScriptSourceCode.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class CORE_EXPORT ScriptSourceCode final {

const String& Source() const { return source_; }
ScriptResource* GetResource() const { return resource_; }
const KURL& Url() const;
KURL Url() const;
int StartLine() const { return start_position_.line_.OneBasedInt(); }
const TextPosition& StartPosition() const { return start_position_; }
ScriptSourceLocationType SourceLocationType() const {
Expand All @@ -79,14 +79,12 @@ class CORE_EXPORT ScriptSourceCode final {
ScriptStreamer* Streamer() const { return streamer_; }

private:
void TreatNullSourceAsEmpty();

String source_;
const String source_;
Member<ScriptResource> resource_;
Member<ScriptStreamer> streamer_;
mutable KURL url_;
TextPosition start_position_;
ScriptSourceLocationType source_location_type_;
const KURL url_;
const TextPosition start_position_;
const ScriptSourceLocationType source_location_type_;
};

} // namespace blink
Expand Down

0 comments on commit 7f4ce6e

Please sign in to comment.