Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(207)

Side by Side Diff: third_party/WebKit/Source/core/loader/resource/ScriptResourceData.cpp

Issue 2724673002: [WIP] Introduce ScriptResourceData
Patch Set: Fix Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 Copyright (C) 1998 Lars Knoll (knoll@mpi-hd.mpg.de) 2 // Use of this source code is governed by a BSD-style license that can be
3 Copyright (C) 2001 Dirk Mueller (mueller@kde.org) 3 // found in the LICENSE file.
4 Copyright (C) 2002 Waldo Bastian (bastian@kde.org)
5 Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com)
6 Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
7 4
8 This library is free software; you can redistribute it and/or 5 #include "core/loader/resource/ScriptResourceData.h"
9 modify it under the terms of the GNU Library General Public
10 License as published by the Free Software Foundation; either
11 version 2 of the License, or (at your option) any later version.
12 6
13 This library is distributed in the hope that it will be useful, 7 #include "platform/HTTPNames.h"
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Library General Public License for more details.
17
18 You should have received a copy of the GNU Library General Public License
19 along with this library; see the file COPYING.LIB. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA.
22
23 This class provides all functionality needed for loading images, style
24 sheets and html pages from the web. It has a memory cache for these objects.
25 */
26
27 #include "core/loader/resource/ScriptResource.h"
28
29 #include "platform/SharedBuffer.h"
30 #include "platform/instrumentation/tracing/web_memory_allocator_dump.h"
31 #include "platform/instrumentation/tracing/web_process_memory_dump.h"
32 #include "platform/loader/fetch/CrossOriginAccessControl.h" 8 #include "platform/loader/fetch/CrossOriginAccessControl.h"
33 #include "platform/loader/fetch/FetchParameters.h"
34 #include "platform/loader/fetch/IntegrityMetadata.h"
35 #include "platform/loader/fetch/ResourceClientWalker.h"
36 #include "platform/loader/fetch/ResourceFetcher.h"
37 #include "platform/network/mime/MIMETypeRegistry.h" 9 #include "platform/network/mime/MIMETypeRegistry.h"
38 10
39 namespace blink { 11 namespace blink {
40 12
41 ScriptResource* ScriptResource::Fetch(FetchParameters& params,
42 ResourceFetcher* fetcher) {
43 DCHECK_EQ(params.GetResourceRequest().GetFrameType(),
44 WebURLRequest::kFrameTypeNone);
45 params.SetRequestContext(WebURLRequest::kRequestContextScript);
46 ScriptResource* resource = ToScriptResource(
47 fetcher->RequestResource(params, ScriptResourceFactory()));
48 if (resource && !params.IntegrityMetadata().IsEmpty())
49 resource->SetIntegrityMetadata(params.IntegrityMetadata());
50 return resource;
51 }
52
53 ScriptResource::ScriptResource(const ResourceRequest& resource_request,
54 const ResourceLoaderOptions& options,
55 const String& charset)
56 : TextResource(resource_request,
57 kScript,
58 options,
59 "application/javascript",
60 charset) {}
61
62 ScriptResource::~ScriptResource() {}
63
64 void ScriptResource::DidAddClient(ResourceClient* client) {
65 DCHECK(ScriptResourceClient::IsExpectedType(client));
66 Resource::DidAddClient(client);
67 }
68
69 void ScriptResource::AppendData(const char* data, size_t length) {
70 Resource::AppendData(data, length);
71 ResourceClientWalker<ScriptResourceClient> walker(Clients());
72 while (ScriptResourceClient* client = walker.Next())
73 client->NotifyAppendData(this);
74 }
75
76 void ScriptResource::OnMemoryDump(WebMemoryDumpLevelOfDetail level_of_detail,
77 WebProcessMemoryDump* memory_dump) const {
78 Resource::OnMemoryDump(level_of_detail, memory_dump);
79 const String name = GetMemoryDumpName() + "/decoded_script";
80 auto dump = memory_dump->CreateMemoryAllocatorDump(name);
81 dump->AddScalar("size", "bytes", source_text_.CharactersSizeInBytes());
82 memory_dump->AddSuballocation(
83 dump->Guid(), String(WTF::Partitions::kAllocatedObjectPoolName));
84 }
85
86 const String& ScriptResource::SourceText() {
87 DCHECK(IsLoaded());
88
89 if (source_text_.IsNull() && Data()) {
90 String source_text = DecodedText();
91 ClearData();
92 SetDecodedSize(source_text.CharactersSizeInBytes());
93 source_text_ = AtomicString(source_text);
94 }
95
96 return source_text_;
97 }
98
99 void ScriptResource::DestroyDecodedDataForFailedRevalidation() {
100 source_text_ = AtomicString();
101 }
102
103 // static 13 // static
104 bool ScriptResource::MimeTypeAllowedByNosniff( 14 bool ScriptResourceData::MimeTypeAllowedByNosniff(
105 const ResourceResponse& response) { 15 const ResourceResponse& response) {
106 return ParseContentTypeOptionsHeader( 16 return ParseContentTypeOptionsHeader(
107 response.HttpHeaderField(HTTPNames::X_Content_Type_Options)) != 17 response.HttpHeaderField(HTTPNames::X_Content_Type_Options)) !=
108 kContentTypeOptionsNosniff || 18 kContentTypeOptionsNosniff ||
109 MIMETypeRegistry::IsSupportedJavaScriptMIMEType( 19 MIMETypeRegistry::IsSupportedJavaScriptMIMEType(
110 response.HttpContentType()); 20 response.HttpContentType());
111 } 21 }
112 22
113 AccessControlStatus ScriptResource::CalculateAccessControlStatus( 23 AccessControlStatus ScriptResourceData::CalculateAccessControlStatus(
114 const SecurityOrigin* security_origin) const { 24 const SecurityOrigin* security_origin) const {
115 if (GetResponse().WasFetchedViaServiceWorker()) { 25 if (GetResponse().WasFetchedViaServiceWorker()) {
116 if (GetResponse().ServiceWorkerResponseType() == 26 if (GetResponse().ServiceWorkerResponseType() ==
117 kWebServiceWorkerResponseTypeOpaque) { 27 kWebServiceWorkerResponseTypeOpaque) {
118 return kOpaqueResource; 28 return kOpaqueResource;
119 } 29 }
120 30
121 return kSharableCrossOrigin; 31 return kSharableCrossOrigin;
122 } 32 }
123 33
124 if (CrossOriginAccessControl::CheckAccess( 34 if (CrossOriginAccessControl::CheckAccess(
125 GetResponse(), GetStoredCredentials(), security_origin) == 35 GetResponse(), GetStoredCredentials(), security_origin) ==
126 CrossOriginAccessControl::kAccessAllowed) { 36 CrossOriginAccessControl::kAccessAllowed) {
127 return kSharableCrossOrigin; 37 return kSharableCrossOrigin;
128 } 38 }
129 39
130 return kNotSharableCrossOrigin; 40 return kNotSharableCrossOrigin;
131 } 41 }
132 42
133 } // namespace blink 43 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698