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

Side by Side Diff: ios/chrome/browser/reading_list/url_downloader.cc

Issue 2496553002: Use relative address for images. (Closed)
Patch Set: feedback Created 4 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 | « ios/chrome/browser/reading_list/url_downloader.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "ios/chrome/browser/reading_list/url_downloader.h" 5 #include "ios/chrome/browser/reading_list/url_downloader.h"
6 6
7 #include <string> 7 #include <string>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/files/file_path.h" 10 #include "base/files/file_path.h"
11 #include "base/files/file_util.h" 11 #include "base/files/file_util.h"
12 #include "base/md5.h" 12 #include "base/md5.h"
13 #include "base/memory/ptr_util.h" 13 #include "base/memory/ptr_util.h"
14 #include "base/path_service.h" 14 #include "base/path_service.h"
15 #include "ios/chrome/browser/chrome_paths.h" 15 #include "ios/chrome/browser/chrome_paths.h"
16 #include "ios/chrome/browser/dom_distiller/distiller_viewer.h" 16 #include "ios/chrome/browser/dom_distiller/distiller_viewer.h"
17 #include "ios/web/public/web_thread.h" 17 #include "ios/web/public/web_thread.h"
18 #include "net/base/escape.h"
18 #include "url/gurl.h" 19 #include "url/gurl.h"
19 20
20 namespace { 21 namespace {
21 char const kOfflineDirectory[] = "Offline"; 22 char const kOfflineDirectory[] = "Offline";
22 } // namespace 23 } // namespace
23 24
24 // URLDownloader 25 // URLDownloader
25 26
26 URLDownloader::URLDownloader( 27 URLDownloader::URLDownloader(
27 dom_distiller::DomDistillerService* distiller_service, 28 dom_distiller::DomDistillerService* distiller_service,
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after
194 base::FilePath path = OfflineURLDirectoryPath(url); 195 base::FilePath path = OfflineURLDirectoryPath(url);
195 if (!DirectoryExists(path)) { 196 if (!DirectoryExists(path)) {
196 return CreateDirectoryAndGetError(path, nil); 197 return CreateDirectoryAndGetError(path, nil);
197 } 198 }
198 return true; 199 return true;
199 } 200 }
200 201
201 bool URLDownloader::SaveImage(const GURL& url, 202 bool URLDownloader::SaveImage(const GURL& url,
202 const GURL& image_url, 203 const GURL& image_url,
203 const std::string& data, 204 const std::string& data,
204 base::FilePath& path) { 205 std::string* image_name) {
205 path = OfflineURLDirectoryPath(url).Append(base::MD5String(image_url.spec())); 206 std::string image_hash = base::MD5String(image_url.spec());
207 *image_name = image_hash;
208 base::FilePath path = OfflineURLDirectoryPath(url).Append(image_hash);
206 if (!base::PathExists(path)) { 209 if (!base::PathExists(path)) {
207 return base::WriteFile(path, data.c_str(), data.length()) > 0; 210 return base::WriteFile(path, data.c_str(), data.length()) > 0;
208 } 211 }
209 return true; 212 return true;
210 } 213 }
211 214
212 std::string URLDownloader::SaveAndReplaceImagesInHTML( 215 std::string URLDownloader::SaveAndReplaceImagesInHTML(
213 const GURL& url, 216 const GURL& url,
214 const std::string& html, 217 const std::string& html,
215 const std::vector<dom_distiller::DistillerViewerInterface::ImageInfo>& 218 const std::vector<dom_distiller::DistillerViewerInterface::ImageInfo>&
216 images) { 219 images) {
217 std::string mutable_html = html; 220 std::string mutable_html = html;
218 for (size_t i = 0; i < images.size(); i++) { 221 for (size_t i = 0; i < images.size(); i++) {
219 base::FilePath local_image_path; 222 base::FilePath local_image_path;
220 if (!SaveImage(url, images[i].url, images[i].data, local_image_path)) { 223 std::string local_image_name;
224 if (!SaveImage(url, images[i].url, images[i].data, &local_image_name)) {
221 return std::string(); 225 return std::string();
222 } 226 }
223 const std::string& image_url = images[i].url.spec(); 227 std::string image_url = net::EscapeForHTML(images[i].url.spec());
224 size_t image_url_size = image_url.size(); 228 size_t image_url_size = image_url.size();
225 size_t pos = mutable_html.find(image_url, 0); 229 size_t pos = mutable_html.find(image_url, 0);
226 while (pos != std::string::npos) { 230 while (pos != std::string::npos) {
227 mutable_html.replace(pos, image_url_size, 231 mutable_html.replace(pos, image_url_size, local_image_name);
228 local_image_path.AsUTF8Unsafe()); 232 pos = mutable_html.find(image_url, pos + local_image_name.size());
229 pos = mutable_html.find(image_url, pos + image_url_size);
230 } 233 }
231 } 234 }
232 return mutable_html; 235 return mutable_html;
233 } 236 }
234 237
235 bool URLDownloader::SaveHTMLForURL(std::string html, const GURL& url) { 238 bool URLDownloader::SaveHTMLForURL(std::string html, const GURL& url) {
236 if (html.empty()) { 239 if (html.empty()) {
237 return false; 240 return false;
238 } 241 }
239 base::FilePath path = OfflineURLPagePath(url); 242 base::FilePath path = OfflineURLPagePath(url);
240 return base::WriteFile(path, html.c_str(), html.length()) > 0; 243 return base::WriteFile(path, html.c_str(), html.length()) > 0;
241 } 244 }
OLDNEW
« no previous file with comments | « ios/chrome/browser/reading_list/url_downloader.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698