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

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: Switch to RefCountedMemory. 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
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 const base::StringPiece resource_as_string_piece =
79 rb.GetRawDataResource(resource_id_); 80 rb.GetRawDataResource(resource_id_);
mmenke 2014/11/17 15:54:10 Can't you just replace this with rb.LoadDataResour
Jeffrey Yasskin 2014/11/17 17:16:23 This winds up forcing a couple extra virtual calls
80 81
81 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422489 is fixed. 82 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422489 is fixed.
82 tracked_objects::ScopedTracker tracking_profile15( 83 tracked_objects::ScopedTracker tracking_profile15(
83 FROM_HERE_WITH_EXPLICIT_FUNCTION( 84 FROM_HERE_WITH_EXPLICIT_FUNCTION(
84 "422489 URLRequestResourceBundleJob::GetData 1.5")); 85 "422489 URLRequestResourceBundleJob::GetData 1.5"));
85 86
86 *data = resource_as_string_piece.as_string(); 87 *data = new base::RefCountedStaticMemory(resource_as_string_piece.data(),
88 resource_as_string_piece.size());
87 89
88 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422489 is fixed. 90 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422489 is fixed.
89 tracked_objects::ScopedTracker tracking_profile2( 91 tracked_objects::ScopedTracker tracking_profile2(
90 FROM_HERE_WITH_EXPLICIT_FUNCTION( 92 FROM_HERE_WITH_EXPLICIT_FUNCTION(
91 "422489 URLRequestResourceBundleJob::GetData 2")); 93 "422489 URLRequestResourceBundleJob::GetData 2"));
92 94
93 // Add the Content-Length header now that we know the resource length. 95 // Add the Content-Length header now that we know the resource length.
94 response_info_.headers->AddHeader( 96 response_info_.headers->AddHeader(base::StringPrintf(
95 base::StringPrintf("%s: %s", 97 "%s: %s", net::HttpRequestHeaders::kContentLength,
96 net::HttpRequestHeaders::kContentLength, 98 base::UintToString(resource_as_string_piece.size()).c_str()));
97 base::UintToString(data->size()).c_str()));
98 99
99 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422489 is fixed. 100 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422489 is fixed.
100 tracked_objects::ScopedTracker tracking_profile3( 101 tracked_objects::ScopedTracker tracking_profile3(
101 FROM_HERE_WITH_EXPLICIT_FUNCTION( 102 FROM_HERE_WITH_EXPLICIT_FUNCTION(
102 "422489 URLRequestResourceBundleJob::GetData 3")); 103 "422489 URLRequestResourceBundleJob::GetData 3"));
103 104
104 std::string* read_mime_type = new std::string; 105 std::string* read_mime_type = new std::string;
105 bool posted = base::PostTaskAndReplyWithResult( 106 bool posted = base::PostTaskAndReplyWithResult(
106 BrowserThread::GetBlockingPool(), 107 BrowserThread::GetBlockingPool(), FROM_HERE,
107 FROM_HERE, 108 base::Bind(&net::GetMimeTypeFromFile, filename_,
108 base::Bind(&net::GetMimeTypeFromFile,
109 filename_,
110 base::Unretained(read_mime_type)), 109 base::Unretained(read_mime_type)),
111 base::Bind(&URLRequestResourceBundleJob::OnMimeTypeRead, 110 base::Bind(&URLRequestResourceBundleJob::OnMimeTypeRead,
112 weak_factory_.GetWeakPtr(), 111 weak_factory_.GetWeakPtr(), mime_type, charset,
113 mime_type, 112 resource_as_string_piece, base::Owned(read_mime_type),
114 charset,
115 data,
116 base::Owned(read_mime_type),
117 callback)); 113 callback));
118 DCHECK(posted); 114 DCHECK(posted);
119 115
120 return net::ERR_IO_PENDING; 116 return net::ERR_IO_PENDING;
121 } 117 }
122 118
123 void GetResponseInfo(net::HttpResponseInfo* info) override { 119 void GetResponseInfo(net::HttpResponseInfo* info) override {
124 *info = response_info_; 120 *info = response_info_;
125 } 121 }
126 122
127 private: 123 private:
128 ~URLRequestResourceBundleJob() override {} 124 ~URLRequestResourceBundleJob() override {}
129 125
130 void OnMimeTypeRead(std::string* out_mime_type, 126 void OnMimeTypeRead(std::string* out_mime_type,
131 std::string* charset, 127 std::string* charset,
132 std::string* data, 128 base::StringPiece data,
133 std::string* read_mime_type, 129 std::string* read_mime_type,
134 const net::CompletionCallback& callback, 130 const net::CompletionCallback& callback,
135 bool read_result) { 131 bool read_result) {
136 *out_mime_type = *read_mime_type; 132 *out_mime_type = *read_mime_type;
137 if (StartsWithASCII(*read_mime_type, "text/", false)) { 133 if (StartsWithASCII(*read_mime_type, "text/", false)) {
138 // All of our HTML files should be UTF-8 and for other resource types 134 // All of our HTML files should be UTF-8 and for other resource types
139 // (like images), charset doesn't matter. 135 // (like images), charset doesn't matter.
140 DCHECK(base::IsStringUTF8(*data)); 136 DCHECK(base::IsStringUTF8(data));
141 *charset = "utf-8"; 137 *charset = "utf-8";
142 } 138 }
143 int result = read_result ? net::OK : net::ERR_INVALID_URL; 139 int result = read_result ? net::OK : net::ERR_INVALID_URL;
144 callback.Run(result); 140 callback.Run(result);
145 } 141 }
146 142
147 // We need the filename of the resource to determine the mime type. 143 // We need the filename of the resource to determine the mime type.
148 base::FilePath filename_; 144 base::FilePath filename_;
149 145
150 // The resource bundle id to load. 146 // The resource bundle id to load.
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 resource_id, 209 resource_id,
214 content_security_policy, 210 content_security_policy,
215 send_cors_header); 211 send_cors_header);
216 } 212 }
217 } 213 }
218 return NULL; 214 return NULL;
219 } 215 }
220 216
221 } // namespace chrome_url_request_util 217 } // namespace chrome_url_request_util
222 } // namespace extensions 218 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698