offline_url_utils.cc 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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
  3. // found in the LICENSE file.
  4. #include "components/reading_list/core/offline_url_utils.h"
  5. #include "base/hash/md5.h"
  6. #include "base/notreached.h"
  7. namespace {
  8. const base::FilePath::CharType kOfflineDirectory[] =
  9. FILE_PATH_LITERAL("Offline");
  10. const base::FilePath::CharType kMainPageFileName[] =
  11. FILE_PATH_LITERAL("page.html");
  12. const base::FilePath::CharType kPDFFileName[] = FILE_PATH_LITERAL("file.pdf");
  13. } // namespace
  14. namespace reading_list {
  15. base::FilePath OfflineRootDirectoryPath(const base::FilePath& profile_path) {
  16. return profile_path.Append(kOfflineDirectory);
  17. }
  18. std::string OfflineURLDirectoryID(const GURL& url) {
  19. return base::MD5String(url.spec());
  20. }
  21. base::FilePath OfflineURLDirectoryAbsolutePath(
  22. const base::FilePath& profile_path,
  23. const GURL& url) {
  24. return OfflineURLAbsolutePathFromRelativePath(
  25. profile_path, base::FilePath::FromUTF8Unsafe(OfflineURLDirectoryID(url)));
  26. }
  27. base::FilePath OfflinePagePath(const GURL& url, OfflineFileType type) {
  28. base::FilePath directory =
  29. base::FilePath::FromUTF8Unsafe(OfflineURLDirectoryID(url));
  30. switch (type) {
  31. case OFFLINE_TYPE_HTML:
  32. return directory.Append(kMainPageFileName);
  33. case OFFLINE_TYPE_PDF:
  34. return directory.Append(kPDFFileName);
  35. }
  36. NOTREACHED();
  37. return base::FilePath();
  38. }
  39. base::FilePath OfflineURLAbsolutePathFromRelativePath(
  40. const base::FilePath& profile_path,
  41. const base::FilePath& relative_path) {
  42. return OfflineRootDirectoryPath(profile_path).Append(relative_path);
  43. }
  44. }