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