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

Side by Side Diff: chrome/browser/extensions/chrome_url_request_util.cc

Issue 725443003: Avoid a string copy in URLRequestResourceBundleJob::GetData(). (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@lkcr
Patch Set: Tidy up Created 6 years, 1 month 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
« no previous file with comments | « base/strings/string_util.cc ('k') | net/url_request/url_request_simple_job.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/extensions/chrome_url_request_util.h" 5 #include "chrome/browser/extensions/chrome_url_request_util.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/files/file_path.h" 9 #include "base/files/file_path.h"
10 #include "base/memory/weak_ptr.h" 10 #include "base/memory/weak_ptr.h"
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
52 : net::URLRequestSimpleJob(request, network_delegate), 52 : net::URLRequestSimpleJob(request, network_delegate),
53 filename_(filename), 53 filename_(filename),
54 resource_id_(resource_id), 54 resource_id_(resource_id),
55 weak_factory_(this) { 55 weak_factory_(this) {
56 // Leave cache headers out of resource bundle requests. 56 // Leave cache headers out of resource bundle requests.
57 response_info_.headers = extensions::BuildHttpHeaders( 57 response_info_.headers = extensions::BuildHttpHeaders(
58 content_security_policy, send_cors_header, base::Time()); 58 content_security_policy, send_cors_header, base::Time());
59 } 59 }
60 60
61 // Overridden from URLRequestSimpleJob: 61 // Overridden from URLRequestSimpleJob:
62 int GetData(std::string* mime_type, 62 int GetRefCountedData(
63 std::string* charset, 63 std::string* mime_type,
64 std::string* data, 64 std::string* charset,
65 const net::CompletionCallback& callback) const override { 65 scoped_refptr<base::RefCountedMemory>* data,
66 const net::CompletionCallback& callback) const override {
66 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422489 is fixed. 67 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422489 is fixed.
67 tracked_objects::ScopedTracker tracking_profile( 68 tracked_objects::ScopedTracker tracking_profile(
68 FROM_HERE_WITH_EXPLICIT_FUNCTION( 69 FROM_HERE_WITH_EXPLICIT_FUNCTION(
69 "422489 URLRequestResourceBundleJob::GetData")); 70 "422489 URLRequestResourceBundleJob::GetData"));
70 71
71 const ResourceBundle& rb = ResourceBundle::GetSharedInstance(); 72 const ResourceBundle& rb = ResourceBundle::GetSharedInstance();
72 73
73 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422489 is fixed. 74 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422489 is fixed.
74 tracked_objects::ScopedTracker tracking_profile1( 75 tracked_objects::ScopedTracker tracking_profile1(
75 FROM_HERE_WITH_EXPLICIT_FUNCTION( 76 FROM_HERE_WITH_EXPLICIT_FUNCTION(
76 "422489 URLRequestResourceBundleJob::GetData 1")); 77 "422489 URLRequestResourceBundleJob::GetData 1"));
77 78
78 const base::StringPiece resource_as_string_piece = 79 *data = rb.LoadDataResourceBytes(resource_id_);
79 rb.GetRawDataResource(resource_id_);
80
81 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422489 is fixed.
82 tracked_objects::ScopedTracker tracking_profile15(
83 FROM_HERE_WITH_EXPLICIT_FUNCTION(
84 "422489 URLRequestResourceBundleJob::GetData 1.5"));
85
86 *data = resource_as_string_piece.as_string();
87 80
88 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422489 is fixed. 81 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422489 is fixed.
89 tracked_objects::ScopedTracker tracking_profile2( 82 tracked_objects::ScopedTracker tracking_profile2(
90 FROM_HERE_WITH_EXPLICIT_FUNCTION( 83 FROM_HERE_WITH_EXPLICIT_FUNCTION(
91 "422489 URLRequestResourceBundleJob::GetData 2")); 84 "422489 URLRequestResourceBundleJob::GetData 2"));
92 85
93 // Add the Content-Length header now that we know the resource length. 86 // Add the Content-Length header now that we know the resource length.
94 response_info_.headers->AddHeader( 87 response_info_.headers->AddHeader(
95 base::StringPrintf("%s: %s", 88 base::StringPrintf("%s: %s", net::HttpRequestHeaders::kContentLength,
96 net::HttpRequestHeaders::kContentLength, 89 base::UintToString((*data)->size()).c_str()));
97 base::UintToString(data->size()).c_str()));
98 90
99 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422489 is fixed. 91 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422489 is fixed.
100 tracked_objects::ScopedTracker tracking_profile3( 92 tracked_objects::ScopedTracker tracking_profile3(
101 FROM_HERE_WITH_EXPLICIT_FUNCTION( 93 FROM_HERE_WITH_EXPLICIT_FUNCTION(
102 "422489 URLRequestResourceBundleJob::GetData 3")); 94 "422489 URLRequestResourceBundleJob::GetData 3"));
103 95
104 std::string* read_mime_type = new std::string; 96 std::string* read_mime_type = new std::string;
105 bool posted = base::PostTaskAndReplyWithResult( 97 bool posted = base::PostTaskAndReplyWithResult(
106 BrowserThread::GetBlockingPool(), 98 BrowserThread::GetBlockingPool(), FROM_HERE,
107 FROM_HERE, 99 base::Bind(&net::GetMimeTypeFromFile, filename_,
108 base::Bind(&net::GetMimeTypeFromFile,
109 filename_,
110 base::Unretained(read_mime_type)), 100 base::Unretained(read_mime_type)),
111 base::Bind(&URLRequestResourceBundleJob::OnMimeTypeRead, 101 base::Bind(&URLRequestResourceBundleJob::OnMimeTypeRead,
112 weak_factory_.GetWeakPtr(), 102 weak_factory_.GetWeakPtr(), mime_type, charset, *data,
113 mime_type, 103 base::Owned(read_mime_type), callback));
114 charset,
115 data,
116 base::Owned(read_mime_type),
117 callback));
118 DCHECK(posted); 104 DCHECK(posted);
119 105
120 return net::ERR_IO_PENDING; 106 return net::ERR_IO_PENDING;
121 } 107 }
122 108
123 void GetResponseInfo(net::HttpResponseInfo* info) override { 109 void GetResponseInfo(net::HttpResponseInfo* info) override {
124 *info = response_info_; 110 *info = response_info_;
125 } 111 }
126 112
127 private: 113 private:
128 ~URLRequestResourceBundleJob() override {} 114 ~URLRequestResourceBundleJob() override {}
129 115
130 void OnMimeTypeRead(std::string* out_mime_type, 116 void OnMimeTypeRead(std::string* out_mime_type,
131 std::string* charset, 117 std::string* charset,
132 std::string* data, 118 scoped_refptr<base::RefCountedMemory> data,
133 std::string* read_mime_type, 119 std::string* read_mime_type,
134 const net::CompletionCallback& callback, 120 const net::CompletionCallback& callback,
135 bool read_result) { 121 bool read_result) {
136 *out_mime_type = *read_mime_type; 122 *out_mime_type = *read_mime_type;
137 if (StartsWithASCII(*read_mime_type, "text/", false)) { 123 if (StartsWithASCII(*read_mime_type, "text/", false)) {
138 // All of our HTML files should be UTF-8 and for other resource types 124 // All of our HTML files should be UTF-8 and for other resource types
139 // (like images), charset doesn't matter. 125 // (like images), charset doesn't matter.
140 DCHECK(base::IsStringUTF8(*data)); 126 DCHECK(base::IsStringUTF8(base::StringPiece(
127 reinterpret_cast<const char*>(data->front()), data->size())));
141 *charset = "utf-8"; 128 *charset = "utf-8";
142 } 129 }
143 int result = read_result ? net::OK : net::ERR_INVALID_URL; 130 int result = read_result ? net::OK : net::ERR_INVALID_URL;
144 callback.Run(result); 131 callback.Run(result);
145 } 132 }
146 133
147 // We need the filename of the resource to determine the mime type. 134 // We need the filename of the resource to determine the mime type.
148 base::FilePath filename_; 135 base::FilePath filename_;
149 136
150 // The resource bundle id to load. 137 // The resource bundle id to load.
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 resource_id, 200 resource_id,
214 content_security_policy, 201 content_security_policy,
215 send_cors_header); 202 send_cors_header);
216 } 203 }
217 } 204 }
218 return NULL; 205 return NULL;
219 } 206 }
220 207
221 } // namespace chrome_url_request_util 208 } // namespace chrome_url_request_util
222 } // namespace extensions 209 } // namespace extensions
OLDNEW
« no previous file with comments | « base/strings/string_util.cc ('k') | net/url_request/url_request_simple_job.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698