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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_ASH_FILEAPI_RECENT_SOURCE_H_
#define CHROME_BROWSER_ASH_FILEAPI_RECENT_SOURCE_H_
#include <memory>
#include <vector>
#include "base/functional/callback_forward.h"
#include "base/i18n/string_search.h"
#include "base/memory/ref_counted.h"
#include "base/time/time.h"
#include "chrome/common/extensions/api/file_manager_private.h"
#include "storage/browser/file_system/file_system_context.h"
#include "storage/browser/file_system/file_system_url.h"
#include "url/gurl.h"
namespace ash {
class RecentFile;
// Interface class for a source of recent files.
//
// Recent file system retrieves recent files from several sources such as
// local directories and cloud storages. To provide files to Recent file
// system, this interface should be implemented for each source.
//
// Note: If a source implementation depends on KeyedServices, remember to add
// dependencies in RecentModelFactory.
//
// All member functions must be called on the UI thread.
class RecentSource {
public:
typedef base::OnceCallback<void(std::vector<RecentFile> files)>
GetRecentFilesCallback;
// File types to filter the results of GetRecentFiles().
enum class FileType {
kAll,
kAudio,
kDocument,
kImage,
kVideo,
};
// Parameters passed to GetRecentFiles(). May be copied.
class Params {
public:
Params(storage::FileSystemContext* file_system_context,
int32_t call_id,
const GURL& origin,
const std::string& query,
const size_t max_files,
const base::Time& cutoff_time,
const base::TimeTicks& end_time,
FileType file_type);
Params(const Params& params);
~Params();
// FileSystemContext that can be used for file system operations.
storage::FileSystemContext* file_system_context() const {
return file_system_context_.get();
}
int32_t call_id() const { return call_id_; }
// Origin of external file system URLs.
// E.g. "chrome-extension://<extension-ID>/"
const GURL& origin() const { return origin_; }
// The query to be applied to recent files to further narrow the returned
// matches.
const std::string& query() const { return query_; }
// The maximum number of files to be returned by this source.
size_t max_files() const { return max_files_; }
// Cut-off last modified time. RecentSource is expected to return files
// modified at this time or later. It is fine to return older files than
// requested here, but they will be filtered out by RecentModel.
const base::Time& cutoff_time() const { return cutoff_time_; }
// File type to filter the results from RecentSource. RecentSource is
// expected to return files which matches the specified file type.
FileType file_type() const { return file_type_; }
// Returns the time by which recent scan operation should terminate (with or
// without results).
base::TimeTicks end_time() const { return end_time_; }
// If maximum duration was set, this method checks if a recent source is
// late with delivery of recent files or still on schedule. If maximum
// duration was never set, this method always returns false.
bool IsLate() const;
private:
scoped_refptr<storage::FileSystemContext> file_system_context_;
const int32_t call_id_;
const GURL origin_;
const std::string query_;
const size_t max_files_;
const base::Time cutoff_time_;
const FileType file_type_;
const base::TimeTicks end_time_;
};
virtual ~RecentSource();
// Retrieves a list of recent files from this source.
//
// You can assume that, once this function is called, it is not called again
// until the callback is invoked. This means that you can safely save internal
// states to compute recent files in member variables.
virtual void GetRecentFiles(const Params& params,
GetRecentFilesCallback callback) = 0;
// Called by the RecentModel if it wants to interrupt search for recent files.
// The recent source may return whatever recent files it has collected so far
// as the response to this call. If the Stop method is called, the callback
// passed to GetRecentFiles is NEVER called. The `call_id` corresponds to one
// of the `call_id` passed in the `params` of the GetRecentFiles` method.
virtual std::vector<RecentFile> Stop(const int32_t call_id) = 0;
// Returns the volume type that is serviced by this recent source.
extensions::api::file_manager_private::VolumeType volume_type() const {
return volume_type_;
}
protected:
// Creates a new recent source that handles a volume of the given type.
explicit RecentSource(
extensions::api::file_manager_private::VolumeType volume_type);
private:
extensions::api::file_manager_private::VolumeType volume_type_;
};
// A common to all recent sources function for checking a file name against the
// query. This function returns true if the query is contained in the given file
// name. This function does case-insensitive, accent-insensitive comparison.
inline bool FileNameMatches(const std::u16string& file_name,
const std::u16string& query) {
return query.empty() || base::i18n::StringSearchIgnoringCaseAndAccents(
query, file_name, nullptr, nullptr);
}
} // namespace ash
#endif // CHROME_BROWSER_ASH_FILEAPI_RECENT_SOURCE_H_
|