Skip to content

Commit

Permalink
recent: Add a parameter to restrict sources for getRecentFiles().
Browse files Browse the repository at this point in the history
      'native_source', 'native_or_drive_source' and 'any_source'

Bug: 742722
Test: browser_tests
Test: Manually called chrome.fileManagerPrivate.getRecentFiles() with
Cq-Include-Trybots: master.tryserver.chromium.linux:closure_compilation
Change-Id: Ie105bb3a3c6bcfbdd69174c165f3f68bf448e31c
Reviewed-on: https://chromium-review.googlesource.com/625640
Commit-Queue: Shuhei Takahashi <[email protected]>
Reviewed-by: Tomasz Mikolajewski <[email protected]>
Reviewed-by: Naoki Fukino <[email protected]>
Cr-Commit-Position: refs/heads/master@{#496644}
  • Loading branch information
nya3jp authored and Commit Bot committed Aug 23, 2017
1 parent 6c2da57 commit 0c37678
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
#include "chrome/browser/signin/signin_manager_factory.h"
#include "chrome/browser/ui/ash/multi_user/multi_user_util.h"
#include "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h"
#include "chrome/common/extensions/api/file_manager_private.h"
#include "chrome/common/extensions/api/file_manager_private_internal.h"
#include "chrome/common/extensions/api/manifest_types.h"
#include "chrome/common/pref_names.h"
Expand All @@ -56,6 +55,7 @@
#include "extensions/browser/app_window/app_window.h"
#include "extensions/browser/app_window/app_window_registry.h"
#include "google_apis/drive/auth_service.h"
#include "storage/common/fileapi/file_system_types.h"
#include "ui/base/webui/web_ui_util.h"
#include "url/gurl.h"

Expand Down Expand Up @@ -154,6 +154,25 @@ bool ConvertURLsToProvidedInfo(
return true;
}

bool IsAllowedSource(storage::FileSystemType type,
api::file_manager_private::SourceRestriction restriction) {
switch (restriction) {
case api::file_manager_private::SOURCE_RESTRICTION_NONE:
NOTREACHED();
return false;

case api::file_manager_private::SOURCE_RESTRICTION_ANY_SOURCE:
return true;

case api::file_manager_private::SOURCE_RESTRICTION_NATIVE_SOURCE:
return type == storage::kFileSystemTypeNativeLocal;

case api::file_manager_private::SOURCE_RESTRICTION_NATIVE_OR_DRIVE_SOURCE:
return type == storage::kFileSystemTypeNativeLocal ||
type == storage::kFileSystemTypeDrive;
}
}

} // namespace

ExtensionFunction::ResponseAction
Expand Down Expand Up @@ -707,6 +726,10 @@ FileManagerPrivateInternalGetRecentFilesFunction::

ExtensionFunction::ResponseAction
FileManagerPrivateInternalGetRecentFilesFunction::Run() {
using extensions::api::file_manager_private_internal::GetRecentFiles::Params;
const std::unique_ptr<Params> params(Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params);

const scoped_refptr<storage::FileSystemContext> file_system_context =
file_manager::util::GetFileSystemContextForRenderFrameHost(
chrome_details_.GetProfile(), render_frame_host());
Expand All @@ -719,11 +742,12 @@ FileManagerPrivateInternalGetRecentFilesFunction::Run() {
Extension::GetBaseURLFromExtensionId(extension_id()),
base::BindOnce(
&FileManagerPrivateInternalGetRecentFilesFunction::OnGetRecentFiles,
this));
this, params->restriction));
return RespondLater();
}

void FileManagerPrivateInternalGetRecentFilesFunction::OnGetRecentFiles(
api::file_manager_private::SourceRestriction restriction,
const std::vector<storage::FileSystemURL>& urls) {
scoped_refptr<storage::FileSystemContext> file_system_context =
file_manager::util::GetFileSystemContextForRenderFrameHost(
Expand All @@ -737,13 +761,23 @@ void FileManagerPrivateInternalGetRecentFilesFunction::OnGetRecentFiles(
file_manager::util::FileDefinitionList file_definition_list;
for (const storage::FileSystemURL& url : urls) {
DCHECK(external_backend->CanHandleType(url.type()));

// Filter out files from non-allowed sources.
// We do this filtering here rather than in RecentModel so that the set of
// files returned with some restriction is a subset of what would be
// returned without restriction. Anyway, the maximum number of files
// returned from RecentModel is large enough.
if (!IsAllowedSource(url.type(), restriction))
continue;

file_manager::util::FileDefinition file_definition;
const bool result =
file_manager::util::ConvertAbsoluteFilePathToRelativeFileSystemPath(
chrome_details_.GetProfile(), extension_id(), url.path(),
&file_definition.virtual_path);
if (!result)
continue;

// Recent file system only lists regular files, not directories.
file_definition.is_directory = false;
file_definition_list.emplace_back(std::move(file_definition));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
#include "chrome/browser/chromeos/file_system_provider/provided_file_system_interface.h"
#include "chrome/browser/extensions/chrome_extension_function.h"
#include "chrome/browser/extensions/chrome_extension_function_details.h"
#include "chrome/common/extensions/api/file_manager_private.h"
#include "google_apis/drive/drive_api_error_codes.h"
#include "storage/browser/fileapi/file_system_url.h"

namespace file_manager {
namespace util {
Expand Down Expand Up @@ -290,7 +292,9 @@ class FileManagerPrivateInternalGetRecentFilesFunction

private:
ResponseAction Run() override;
void OnGetRecentFiles(const std::vector<storage::FileSystemURL>& urls);
void OnGetRecentFiles(
api::file_manager_private::SourceRestriction restriction,
const std::vector<storage::FileSystemURL>& urls);
void OnConvertFileDefinitionListToEntryDefinitionList(
std::unique_ptr<file_manager::util::EntryDefinitionList>
entry_definition_list);
Expand Down
17 changes: 16 additions & 1 deletion chrome/common/extensions/api/file_manager_private.idl
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,18 @@ enum Verb {
share_with
};

// Recent file sources allowed in getRecentFiles().
enum SourceRestriction {
// Allows any source.
any_source,

// Allows source with native local file system only.
native_source,

// Allows native source and Drive source.
native_or_drive_source
};

// A file task represents an action that the file manager can perform over the
// currently selected files. See
// chrome/browser/chromeos/extensions/file_manager/file_tasks.h for details
Expand Down Expand Up @@ -997,8 +1009,11 @@ interface Functions {
GetDirectorySizeCallback callback);

// Gets recently modified files across file systems.
// |restriction| Flag to restrict sources of recent files.
// |callback|
[nocompile]
static void getRecentFiles(GetRecentFilesCallback callback);
static void getRecentFiles(SourceRestriction restriction,
GetRecentFilesCallback callback);
};

interface Events {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ namespace fileManagerPrivateInternal {
ValidatePathNameLengthCallback callback);
static void getDirectorySize(DOMString url,
GetDirectorySizeCallback callback);
static void getRecentFiles(GetRecentFilesCallback callback);
static void getRecentFiles(fileManagerPrivate.SourceRestriction restriction,
GetRecentFilesCallback callback);
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,10 @@ binding.registerCustomHook(function(bindingsAPI) {
fileManagerPrivateInternal.getDirectorySize(url, callback);
});

apiFunctions.setHandleRequest('getRecentFiles', function(callback) {
fileManagerPrivateInternal.getRecentFiles(function(entryDescriptions) {
apiFunctions.setHandleRequest('getRecentFiles', function(
restriction, callback) {
fileManagerPrivateInternal.getRecentFiles(restriction, function(
entryDescriptions) {
callback(entryDescriptions.map(function(description) {
return GetExternalFileEntry(description);
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ requestAllFileSystems().then(function() {
chrome.test.runTests([
function testGetRecentFiles() {
chrome.fileManagerPrivate.getRecentFiles(
chrome.test.callbackPass(function(entries) {
'native_source', chrome.test.callbackPass(function(entries) {
var found = false;
for (var i = 0; i < entries.length; ++i) {
if (entries[i].name === 'all-justice.jpg') {
Expand Down
10 changes: 9 additions & 1 deletion third_party/closure_compiler/externs/file_manager_private.js
Original file line number Diff line number Diff line change
Expand Up @@ -669,9 +669,10 @@ chrome.fileManagerPrivate.getDirectorySize = function(entry, callback) {};

/**
* Gets recently modified files across file systems.
* @param {string} restriction
* @param {function((!Array<!FileEntry>))} callback
*/
chrome.fileManagerPrivate.getRecentFiles = function(callback) {};
chrome.fileManagerPrivate.getRecentFiles = function(restriction, callback) {};

/**
* Executes the action on the specified set of entries. If not possible, then
Expand Down Expand Up @@ -717,3 +718,10 @@ chrome.fileManagerPrivate.Verb = {
PACK_WITH: 'pack_with',
SHARE_WITH: 'share_with',
};

/** @enum {string} */
chrome.fileManagerPrivate.SourceRestriction = {
ANY_SOURCE: 'any_source',
NATIVE_SOURCE: 'native_source',
NATIVE_OR_DRIVE_SOURCE: 'native_or_drive_source',
};

0 comments on commit 0c37678

Please sign in to comment.