forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This utils will handle all the conversions between offline file path and URLs. BUG=664124 Review-Url: https://codereview.chromium.org/2506993002 Cr-Commit-Position: refs/heads/master@{#432541}
- Loading branch information
olivierrobin
authored and
Commit bot
committed
Nov 16, 2016
1 parent
97337ee
commit 5ab336c
Showing
8 changed files
with
243 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright 2016 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "ios/chrome/browser/reading_list/offline_url_utils.h" | ||
|
||
#include "base/md5.h" | ||
#include "base/strings/stringprintf.h" | ||
#include "ios/chrome/browser/chrome_url_constants.h" | ||
|
||
namespace { | ||
const char kOfflineDirectory[] = "Offline"; | ||
const char kMainPageFileName[] = "page.html"; | ||
} // namespace | ||
|
||
namespace reading_list { | ||
|
||
base::FilePath OfflineRootDirectoryPath(const base::FilePath& profile_path) { | ||
return profile_path.Append(FILE_PATH_LITERAL(kOfflineDirectory)); | ||
} | ||
|
||
std::string OfflineURLDirectoryID(const GURL& url) { | ||
return base::MD5String(url.spec()); | ||
} | ||
|
||
base::FilePath OfflinePagePath(const GURL& url) { | ||
base::FilePath directory(OfflineURLDirectoryID(url)); | ||
return directory.Append(FILE_PATH_LITERAL(kMainPageFileName)); | ||
} | ||
|
||
base::FilePath OfflineURLDirectoryAbsolutePath( | ||
const base::FilePath& profile_path, | ||
const GURL& url) { | ||
return OfflineRootDirectoryPath(profile_path) | ||
.Append(OfflineURLDirectoryID(url)); | ||
} | ||
|
||
base::FilePath OfflinePageAbsolutePath(const base::FilePath& profile_path, | ||
const GURL& url) { | ||
return OfflineRootDirectoryPath(profile_path).Append(OfflinePagePath(url)); | ||
} | ||
|
||
GURL DistilledURLForPath(const base::FilePath& distilled_path) { | ||
if (distilled_path.empty()) { | ||
return GURL(); | ||
} | ||
return GURL(kChromeUIOfflineURL + distilled_path.value()); | ||
} | ||
|
||
GURL FileURLForDistilledURL(const GURL& distilled_url, | ||
const base::FilePath& profile_path, | ||
GURL* resources_root_url) { | ||
if (!distilled_url.is_valid()) { | ||
return GURL(); | ||
} | ||
DCHECK(distilled_url.SchemeIs(kChromeUIScheme)); | ||
base::FilePath offline_path = profile_path.AppendASCII(kOfflineDirectory); | ||
|
||
GURL file_url(base::StringPrintf("%s%s", url::kFileScheme, | ||
url::kStandardSchemeSeparator) + | ||
offline_path.value() + distilled_url.path()); | ||
if (resources_root_url) { | ||
*resources_root_url = file_url.Resolve("."); | ||
} | ||
return file_url; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright 2016 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef IOS_CHROME_BROWSER_READING_LIST_OFFLINE_URL_UTILS_H_ | ||
#define IOS_CHROME_BROWSER_READING_LIST_OFFLINE_URL_UTILS_H_ | ||
|
||
#include <string> | ||
|
||
#include "base/files/file_path.h" | ||
#include "url/gurl.h" | ||
|
||
namespace reading_list { | ||
|
||
// The absolute path of the directory where offline URLs are saved. | ||
// |profile_path| is the path to the profile directory that contain the offline | ||
// directory. | ||
base::FilePath OfflineRootDirectoryPath(const base::FilePath& profile_path); | ||
|
||
// The absolute path of the directory where a |url| is saved offline. | ||
// Contains the page and supporting files (images). | ||
// |profile_path| is the path to the profile directory that contain the offline | ||
// directory. | ||
// The directory may not exist. | ||
base::FilePath OfflineURLDirectoryAbsolutePath( | ||
const base::FilePath& profile_path, | ||
const GURL& url); | ||
|
||
// The absolute path of the offline webpage for the |url|. The file may not | ||
// exist. | ||
// |profile_path| is the path to the profile directory that contain the offline | ||
// directory. | ||
base::FilePath OfflinePageAbsolutePath(const base::FilePath& profile_path, | ||
const GURL& url); | ||
|
||
// The relative path to the offline webpage for the |url|. The result is | ||
// relative to |OfflineRootDirectoryPath()|. | ||
// The file may not exist. | ||
base::FilePath OfflinePagePath(const GURL& url); | ||
|
||
// The name of the directory containing offline data for |url|. | ||
std::string OfflineURLDirectoryID(const GURL& url); | ||
|
||
// The distilled URL chrome://offline/... that will load the file at |path|. | ||
GURL DistilledURLForPath(const base::FilePath& path); | ||
|
||
// The file URL pointing to the local file to load to display |distilled_url|. | ||
// If |resources_root_url| is not nullptr, it is set to a file URL to the | ||
// directory conatining all the resources needed by |distilled_url|. | ||
// |profile_path| is the path to the profile directory. | ||
GURL FileURLForDistilledURL(const GURL& distilled_url, | ||
const base::FilePath& profile_path, | ||
GURL* resources_root_url); | ||
|
||
} // namespace reading_list | ||
|
||
#endif // IOS_CHROME_BROWSER_READING_LIST_OFFLINE_URL_UTILS_H_ |
89 changes: 89 additions & 0 deletions
89
ios/chrome/browser/reading_list/offline_url_utils_unittest.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// Copyright 2016 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "ios/chrome/browser/reading_list/offline_url_utils.h" | ||
|
||
#include <string> | ||
|
||
#include "base/files/file_path.h" | ||
#include "testing/gtest/include/gtest/gtest.h" | ||
#include "url/gurl.h" | ||
|
||
// Checks the root directory of offline pages. | ||
TEST(OfflineURLUtilsTest, OfflineRootDirectoryPathTest) { | ||
base::FilePath profile_path("/profile_path"); | ||
base::FilePath offline_directory = | ||
reading_list::OfflineRootDirectoryPath(profile_path); | ||
EXPECT_EQ("/profile_path/Offline", offline_directory.value()); | ||
} | ||
|
||
// Checks the offline page directory is the MD5 of the URL | ||
TEST(OfflineURLUtilsTest, OfflineURLDirectoryIDTest) { | ||
GURL url("http://www.google.com/test"); | ||
// MD5 of "http://www.google.com/test" | ||
std::string md5 = "0090071ef710946a1263c276284bb3b8"; | ||
std::string directory_id = reading_list::OfflineURLDirectoryID(url); | ||
EXPECT_EQ(md5, directory_id); | ||
} | ||
|
||
// Checks the offline page directory is | ||
// |profile_path|/Offline/OfflineURLDirectoryID; | ||
TEST(OfflineURLUtilsTest, OfflineURLDirectoryAbsolutePathTest) { | ||
base::FilePath profile_path("/profile_path"); | ||
GURL url("http://www.google.com/test"); | ||
base::FilePath offline_directory = | ||
reading_list::OfflineURLDirectoryAbsolutePath(profile_path, url); | ||
EXPECT_EQ("/profile_path/Offline/0090071ef710946a1263c276284bb3b8", | ||
offline_directory.value()); | ||
} | ||
|
||
// Checks the offline page absolute path is | ||
// |profile_path|/Offline/OfflineURLDirectoryID/page.html; | ||
TEST(OfflineURLUtilsTest, OfflinePageAbsolutePathTest) { | ||
base::FilePath profile_path("/profile_path"); | ||
GURL url("http://www.google.com/test"); | ||
base::FilePath offline_page = | ||
reading_list::OfflinePageAbsolutePath(profile_path, url); | ||
EXPECT_EQ("/profile_path/Offline/0090071ef710946a1263c276284bb3b8/page.html", | ||
offline_page.value()); | ||
} | ||
|
||
// Checks the offline page path is OfflineURLDirectoryID/page.html; | ||
TEST(OfflineURLUtilsTest, OfflinePagePathTest) { | ||
GURL url("http://www.google.com/test"); | ||
base::FilePath offline_page = reading_list::OfflinePagePath(url); | ||
EXPECT_EQ("0090071ef710946a1263c276284bb3b8/page.html", offline_page.value()); | ||
} | ||
|
||
// Checks the distilled URL for the page is chrome://offline/MD5/page.html; | ||
TEST(OfflineURLUtilsTest, DistilledURLForPathTest) { | ||
base::FilePath page_path("MD5/page.html"); | ||
GURL distilled_url = reading_list::DistilledURLForPath(page_path); | ||
EXPECT_EQ("chrome://offline/MD5/page.html", distilled_url.spec()); | ||
} | ||
|
||
// Checks the file path for chrome://offline/MD5/page.html is | ||
// file://profile_path/Offline/MD5/page.html. | ||
// Checks the resource root for chrome://offline/MD5/page.html is | ||
// file://profile_path/Offline/MD5 | ||
TEST(OfflineURLUtilsTest, FileURLForDistilledURLTest) { | ||
base::FilePath profile_path("/profile_path"); | ||
GURL file_url = | ||
reading_list::FileURLForDistilledURL(GURL(), profile_path, nullptr); | ||
EXPECT_FALSE(file_url.is_valid()); | ||
|
||
GURL distilled_url("chrome://offline/MD5/page.html"); | ||
file_url = reading_list::FileURLForDistilledURL(distilled_url, profile_path, | ||
nullptr); | ||
EXPECT_TRUE(file_url.is_valid()); | ||
EXPECT_TRUE(file_url.SchemeIsFile()); | ||
EXPECT_EQ("/profile_path/Offline/MD5/page.html", file_url.path()); | ||
|
||
GURL resource_url; | ||
file_url = reading_list::FileURLForDistilledURL(distilled_url, profile_path, | ||
&resource_url); | ||
EXPECT_TRUE(resource_url.is_valid()); | ||
EXPECT_TRUE(resource_url.SchemeIsFile()); | ||
EXPECT_EQ("/profile_path/Offline/MD5/", resource_url.path()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.