File: wallpaper_enumerator.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (98 lines) | stat: -rw-r--r-- 3,348 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ash/wallpaper/wallpaper_enumerator.h"

#include <algorithm>
#include <string>
#include <vector>

#include "base/files/file_enumerator.h"
#include "base/files/file_path.h"
#include "base/functional/callback.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "chrome/browser/ash/file_manager/path_util.h"
#include "chrome/browser/ash/file_manager/trash_common_util.h"

namespace {

constexpr char kPngFilePattern[] = "*.[pP][nN][gG]";
constexpr char kJpgFilePattern[] = "*.[jJ][pP][gG]";
constexpr char kJpegFilePattern[] = "*.[jJ][pP][eE][gG]";

constexpr int kMaximumImageCount = 1000;

void EnumerateFiles(const base::FilePath& path,
                    const std::vector<base::FilePath>& trash_paths,
                    const std::string& pattern,
                    std::vector<base::FilePath>* out) {
  base::FileEnumerator image_enumerator(
      path, /*recursive=*/true, base::FileEnumerator::FILES,
      FILE_PATH_LITERAL(pattern),
      base::FileEnumerator::FolderSearchPolicy::ALL);

  for (base::FilePath image_path = image_enumerator.Next();
       !image_path.empty() && out->size() < kMaximumImageCount;
       image_path = image_enumerator.Next()) {
    if (std::ranges::any_of(
            trash_paths, [&image_path](const base::FilePath& trash_path) {
              // Equivalent to
              // image_path.value().starts_with(trash_path.value()).
              return image_path.value().rfind(trash_path.value(), 0) == 0;
            })) {
      continue;
    }
    out->push_back(image_path);
  }
}

// Looks up all the images in the |search_path| and excludes the ones that
// overlapse with |trash_paths|.
std::vector<base::FilePath> EnumerateAllImages(
    const base::FilePath& search_path,
    const std::vector<base::FilePath>& trash_paths,
    const std::vector<std::string>& patterns) {
  std::vector<base::FilePath> image_paths;

  for (const auto& pattern : patterns) {
    EnumerateFiles(search_path, trash_paths, pattern, &image_paths);
  }

  return image_paths;
}

}  // namespace

namespace ash {

void EnumerateLocalWallpaperFiles(
    Profile* profile,
    base::OnceCallback<void(const std::vector<base::FilePath>&)> callback) {
  const base::FilePath search_path =
      file_manager::util::GetMyFilesFolderForProfile(profile);
  const std::vector<std::string> search_patterns = {
      kPngFilePattern, kJpgFilePattern, kJpegFilePattern};

  std::vector<base::FilePath> trash_paths;
  if (file_manager::trash::IsTrashEnabledForProfile(profile)) {
    auto enabled_trash_locations =
        file_manager::trash::GenerateEnabledTrashLocationsForProfile(profile);
    for (const auto& it : enabled_trash_locations) {
      base::FilePath trash_path =
          it.first.Append(it.second.relative_folder_path);
      trash_paths.emplace_back(trash_path);
    }
  }

  base::ThreadPool::PostTaskAndReplyWithResult(
      FROM_HERE,
      {base::MayBlock(), base::TaskPriority::BEST_EFFORT,
       base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN},
      base::BindOnce(&EnumerateAllImages, search_path, trash_paths,
                     search_patterns),
      std::move(callback));
}

}  // namespace ash