Commit 5848d090 authored by tby's avatar tby Committed by Commit Bot

[Suggested files] Create new file result class.

The suggested files v2 and soon-to-be file search projects will share
code. This CL makes a new directory that we can gradually migrate all
file-related search providers into, and adds a common result class for
all files.

Bug: 1034842
Change-Id: I29026120ec47382155125d1cf225388d233b4117
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2407675Reviewed-by: default avatarRachel Wong <wrong@chromium.org>
Commit-Queue: Tony Yeoman <tby@chromium.org>
Cr-Commit-Position: refs/heads/master@{#806530}
parent 3351e59b
......@@ -1745,6 +1745,8 @@ static_library("ui") {
"app_list/search/drive_quick_access_result.h",
"app_list/search/file_chip_result.cc",
"app_list/search/file_chip_result.h",
"app_list/search/files/file_result.cc",
"app_list/search/files/file_result.h",
"app_list/search/launcher_search/launcher_search_icon_image_loader.cc",
"app_list/search/launcher_search/launcher_search_icon_image_loader.h",
"app_list/search/launcher_search/launcher_search_icon_image_loader_impl.cc",
......
// Copyright 2020 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 "chrome/browser/ui/app_list/search/files/file_result.h"
#include <string>
#include <utility>
#include <vector>
#include "ash/public/cpp/app_list/app_list_types.h"
#include "ash/public/cpp/file_icon_util.h"
#include "base/bind.h"
#include "base/files/file_path.h"
#include "base/i18n/rtl.h"
#include "base/macros.h"
#include "base/no_destructor.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/platform_util.h"
#include "chrome/grit/generated_resources.h"
#include "ui/base/l10n/l10n_util.h"
namespace app_list {
namespace {
std::string StripHostedFileExtensions(const std::string& filename) {
static const base::NoDestructor<std::vector<std::string>> hosted_extensions(
{".GDOC", ".GSHEET", ".GSLIDES", ".GDRAW", ".GTABLE", ".GLINK", ".GFORM",
".GMAPS", ".GSITE"});
for (const auto& extension : *hosted_extensions) {
if (EndsWith(filename, extension, base::CompareCase::INSENSITIVE_ASCII)) {
return filename.substr(0, filename.size() - extension.size());
}
}
return filename;
}
} // namespace
FileResult::FileResult(const std::string& schema,
const base::FilePath& filepath,
ResultType result_type,
DisplayType display_type,
float relevance,
Profile* profile)
: filepath_(filepath), profile_(profile) {
DCHECK(profile);
set_id(schema + filepath.value());
set_relevance(relevance);
// TODO(crbug.com/1034842): Rename or replace the DriveQuickAccess and
// ZeroStateFile enum values.
SetResultType(result_type);
switch (result_type) {
case ResultType::kDriveQuickAccess:
SetMetricsType(ash::DRIVE_QUICK_ACCESS);
break;
case ResultType::kZeroStateFile:
SetMetricsType(ash::ZERO_STATE_FILE);
break;
default:
NOTREACHED();
}
SetDisplayType(display_type);
switch (display_type) {
case DisplayType::kChip:
SetChipIcon(ash::GetChipIconForPath(filepath));
break;
case DisplayType::kList:
SetIcon(ash::GetIconForPath(filepath));
break;
default:
NOTREACHED();
}
// Set the details to the display name of the Files app.
base::string16 sanitized_name = base::CollapseWhitespace(
l10n_util::GetStringUTF16(IDS_FILEMANAGER_APP_NAME), true);
base::i18n::SanitizeUserSuppliedString(&sanitized_name);
SetDetails(sanitized_name);
SetTitle(base::UTF8ToUTF16(
StripHostedFileExtensions(filepath.BaseName().value())));
}
FileResult::~FileResult() = default;
void FileResult::Open(int event_flags) {
platform_util::OpenItem(profile_, filepath_,
platform_util::OpenItemType::OPEN_FILE,
platform_util::OpenOperationCallback());
}
::std::ostream& operator<<(::std::ostream& os, const FileResult& result) {
return os << "{" << result.title() << ", " << result.relevance() << "}";
}
} // namespace app_list
// Copyright 2020 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 CHROME_BROWSER_UI_APP_LIST_SEARCH_FILES_FILE_RESULT_H_
#define CHROME_BROWSER_UI_APP_LIST_SEARCH_FILES_FILE_RESULT_H_
#include <iosfwd>
#include "base/files/file_path.h"
#include "chrome/browser/ui/app_list/search/chrome_search_result.h"
class Profile;
namespace app_list {
class FileResult : public ChromeSearchResult {
public:
FileResult(const std::string& schema,
const base::FilePath& filepath,
ResultType result_type,
DisplayType display_type,
float relevance,
Profile* profile);
~FileResult() override;
FileResult(const FileResult&) = delete;
FileResult& operator=(const FileResult&) = delete;
// ChromeSearchResult overrides:
void Open(int event_flags) override;
private:
const base::FilePath filepath_;
Profile* const profile_;
};
::std::ostream& operator<<(::std::ostream& os, const FileResult& result);
} // namespace app_list
#endif // CHROME_BROWSER_UI_APP_LIST_SEARCH_FILES_FILE_RESULT_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment