Skip to content

Commit

Permalink
Get rid of DriveEntryKind.
Browse files Browse the repository at this point in the history
Basically DriveEntryKind is used for clasifying files into one of {Folder, Hosted document, Normal file}.
We can classify them without DriveEntryKind by using FileResource::IsDirectory() and their mime type.
Utility functions for clasification are now indepenent with ResourceEntry, and placed in drive_api_util.h.
ResourceEntryKind is defined to provide users of ResourceEntry (e.g. sync file system) with similar accessor to file kind.

BUG=357038
TBR=rogerta@chromium.org for one line removal in .gyp file and .gn file.  
TEST=run google_apis_unittests and unit_tests

Review URL: https://codereview.chromium.org/384543004

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@282941 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
fukino@chromium.org committed Jul 14, 2014
1 parent 15c7939 commit 51ed7bd
Show file tree
Hide file tree
Showing 24 changed files with 183 additions and 575 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include "chrome/browser/chromeos/drive/test_util.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "google_apis/drive/drive_api_parser.h"
#include "google_apis/drive/gdata_wapi_parser.h"
#include "google_apis/drive/test_util.h"
#include "testing/gtest/include/gtest/gtest.h"

Expand Down
7 changes: 3 additions & 4 deletions chrome/browser/chromeos/drive/file_system_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "chrome/browser/chromeos/drive/write_on_cache_file.h"
#include "chrome/browser/chromeos/profiles/profile_helper.h"
#include "chrome/browser/chromeos/profiles/profile_util.h"
#include "chrome/browser/drive/drive_api_util.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/common/chrome_constants.h"
Expand All @@ -37,7 +38,6 @@
#include "chrome/common/url_constants.h"
#include "chromeos/chromeos_constants.h"
#include "content/public/browser/browser_thread.h"
#include "google_apis/drive/gdata_wapi_parser.h"
#include "net/base/escape.h"
#include "webkit/browser/fileapi/file_system_url.h"

Expand Down Expand Up @@ -345,9 +345,8 @@ bool CreateGDocFile(const base::FilePath& file_path,
}

bool HasGDocFileExtension(const base::FilePath& file_path) {
return google_apis::ResourceEntry::ClassifyEntryKindByFileExtension(
file_path) &
google_apis::ResourceEntry::KIND_OF_HOSTED_DOCUMENT;
std::string extension = base::FilePath(file_path.Extension()).AsUTF8Unsafe();
return IsHostedDocumentByExtension(extension);
}

GURL ReadUrlFromGDocFile(const base::FilePath& file_path) {
Expand Down
7 changes: 0 additions & 7 deletions chrome/browser/chromeos/drive/file_system_util_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -284,13 +284,6 @@ TEST(FileSystemUtilTest, GDocFile) {
EXPECT_EQ(url, ReadUrlFromGDocFile(file));
EXPECT_EQ(resource_id, ReadResourceIdFromGDocFile(file));

// Read and write glink.
file = temp_dir.path().AppendASCII("test.glink");
EXPECT_TRUE(CreateGDocFile(file, url, resource_id));
EXPECT_TRUE(HasGDocFileExtension(file));
EXPECT_EQ(url, ReadUrlFromGDocFile(file));
EXPECT_EQ(resource_id, ReadResourceIdFromGDocFile(file));

// Non GDoc file.
file = temp_dir.path().AppendASCII("test.txt");
std::string data = "Hello world!";
Expand Down
31 changes: 8 additions & 23 deletions chrome/browser/chromeos/drive/resource_entry_conversion.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include "chrome/browser/chromeos/drive/file_system_util.h"
#include "chrome/browser/drive/drive_api_util.h"
#include "google_apis/drive/drive_api_parser.h"
#include "google_apis/drive/gdata_wapi_parser.h"

namespace drive {

Expand Down Expand Up @@ -77,41 +76,33 @@ bool ConvertFileResourceToResourceEntry(
input.last_viewed_by_me_date().ToInternalValue());
file_info->set_creation_time(input.created_date().ToInternalValue());

// TODO(hashimoto): Get rid of WAPI stuff. crbug.com/357038
const google_apis::DriveEntryKind entry_kind = util::GetKind(input);
const int entry_kind_class =
google_apis::ResourceEntry::ClassifyEntryKind(entry_kind);
const bool is_file = entry_kind_class &
google_apis::ResourceEntry::KIND_OF_FILE;
const bool is_hosted_document = entry_kind_class &
google_apis::ResourceEntry::KIND_OF_HOSTED_DOCUMENT;
const bool is_folder = entry_kind_class &
google_apis::ResourceEntry::KIND_OF_FOLDER;

if (is_file || is_hosted_document) {
if (input.IsDirectory()) {
file_info->set_is_directory(true);
} else {
FileSpecificInfo* file_specific_info =
converted.mutable_file_specific_info();
if (is_file) {
if (!drive::util::IsHostedDocument(input.mime_type())) {
file_info->set_size(input.file_size());
file_specific_info->set_md5(input.md5_checksum());
} else if (is_hosted_document) {
file_specific_info->set_is_hosted_document(false);
} else {
// Attach .g<something> extension to hosted documents so we can special
// case their handling in UI.
// TODO(satorux): Figure out better way how to pass input info like kind
// to UI through the File API stack.
const std::string document_extension =
google_apis::ResourceEntry::GetHostedDocumentExtension(entry_kind);
drive::util::GetHostedDocumentExtension(input.mime_type());
file_specific_info->set_document_extension(document_extension);
converted.set_base_name(
util::NormalizeFileName(converted.title() + document_extension));

// We don't know the size of hosted docs and it does not matter since
// it has no effect on the quota.
file_info->set_size(0);
file_specific_info->set_is_hosted_document(true);
}
file_info->set_is_directory(false);
file_specific_info->set_content_mime_type(input.mime_type());
file_specific_info->set_is_hosted_document(is_hosted_document);

if (!input.alternate_link().is_empty())
file_specific_info->set_alternate_url(input.alternate_link().spec());
Expand All @@ -127,12 +118,6 @@ bool ConvertFileResourceToResourceEntry(
const int64 image_rotation = input.image_media_metadata().rotation();
if (image_rotation != -1)
file_specific_info->set_image_rotation(image_rotation);
} else if (is_folder) {
file_info->set_is_directory(true);
} else {
// The entry is something that doesn't map into files (i.e. sites).
// We don't handle these kind of entries hence return false.
return false;
}

out_entry->Swap(&converted);
Expand Down
18 changes: 7 additions & 11 deletions chrome/browser/chromeos/drive/search_metadata.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "chrome/browser/chromeos/drive/file_system_util.h"
#include "chrome/browser/drive/drive_api_util.h"
#include "content/public/browser/browser_thread.h"
#include "google_apis/drive/gdata_wapi_parser.h"
#include "net/base/escape.h"

using content::BrowserThread;
Expand Down Expand Up @@ -166,16 +166,12 @@ bool IsEligibleEntry(const ResourceEntry& entry, int options) {
if (entry.file_specific_info().is_hosted_document()) {
// Not all hosted documents are cached by Drive offline app.
// http://support.google.com/drive/bin/answer.py?hl=en&answer=1628467
switch (google_apis::ResourceEntry::GetEntryKindFromExtension(
entry.file_specific_info().document_extension())) {
case google_apis::ENTRY_KIND_DOCUMENT:
case google_apis::ENTRY_KIND_SPREADSHEET:
case google_apis::ENTRY_KIND_PRESENTATION:
case google_apis::ENTRY_KIND_DRAWING:
return true;
default:
return false;
}
std::string mime_type = drive::util::GetHostedDocumentMimeType(
entry.file_specific_info().document_extension());
return mime_type == drive::util::kGoogleDocumentMimeType ||
mime_type == drive::util::kGoogleSpreadsheetMimeType ||
mime_type == drive::util::kGooglePresentationMimeType ||
mime_type == drive::util::kGoogleDrawingMimeType;
} else {
return entry.file_specific_info().cache_state().is_present();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include "chrome/browser/chromeos/drive/sync/remove_performer.h"
#include "content/public/browser/browser_thread.h"
#include "google_apis/drive/drive_api_parser.h"
#include "google_apis/drive/gdata_wapi_parser.h"

using content::BrowserThread;

Expand Down
9 changes: 4 additions & 5 deletions chrome/browser/chromeos/file_manager/file_tasks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "chrome/browser/chromeos/file_manager/fileapi_util.h"
#include "chrome/browser/chromeos/file_manager/open_util.h"
#include "chrome/browser/chromeos/fileapi/file_system_backend.h"
#include "chrome/browser/drive/drive_api_util.h"
#include "chrome/browser/drive/drive_app_registry.h"
#include "chrome/browser/extensions/extension_tab_util.h"
#include "chrome/browser/extensions/extension_util.h"
Expand All @@ -32,7 +33,6 @@
#include "extensions/browser/extension_util.h"
#include "extensions/common/constants.h"
#include "extensions/common/extension_set.h"
#include "google_apis/drive/gdata_wapi_parser.h"
#include "webkit/browser/fileapi/file_system_context.h"
#include "webkit/browser/fileapi/file_system_url.h"

Expand Down Expand Up @@ -90,11 +90,10 @@ const size_t kDriveTaskExtensionPrefixLength =
bool ContainsGoogleDocument(const PathAndMimeTypeSet& path_mime_set) {
for (PathAndMimeTypeSet::const_iterator iter = path_mime_set.begin();
iter != path_mime_set.end(); ++iter) {
if (google_apis::ResourceEntry::ClassifyEntryKindByFileExtension(
iter->first) &
google_apis::ResourceEntry::KIND_OF_GOOGLE_DOCUMENT) {
std::string extension =
base::FilePath(iter->first.Extension()).AsUTF8Unsafe();
if (drive::util::IsHostedDocumentByExtension(extension))
return true;
}
}
return false;
}
Expand Down
163 changes: 52 additions & 111 deletions chrome/browser/drive/drive_api_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,23 @@ namespace drive {
namespace util {
namespace {

std::string GetMimeTypeFromEntryKind(google_apis::DriveEntryKind kind) {
switch (kind) {
case google_apis::ENTRY_KIND_DOCUMENT:
return kGoogleDocumentMimeType;
case google_apis::ENTRY_KIND_SPREADSHEET:
return kGoogleSpreadsheetMimeType;
case google_apis::ENTRY_KIND_PRESENTATION:
return kGooglePresentationMimeType;
case google_apis::ENTRY_KIND_DRAWING:
return kGoogleDrawingMimeType;
case google_apis::ENTRY_KIND_TABLE:
return kGoogleTableMimeType;
case google_apis::ENTRY_KIND_FORM:
return kGoogleFormMimeType;
default:
return std::string();
}
}

// Returns the argument string.
std::string Identity(const std::string& resource_id) { return resource_id; }

struct HostedDocumentKind {
const char* mime_type;
const char* extension;
};

const HostedDocumentKind kHostedDocumentKinds[] = {
{kGoogleDocumentMimeType, ".gdoc"},
{kGoogleSpreadsheetMimeType, ".gsheet"},
{kGooglePresentationMimeType, ".gslides"},
{kGoogleDrawingMimeType, ".gdraw"},
{kGoogleTableMimeType, ".gtable"},
{kGoogleFormMimeType, ".gform"}
};

} // namespace


Expand Down Expand Up @@ -173,97 +168,6 @@ void ParseShareUrlAndRun(const google_apis::GetShareUrlCallback& callback,
callback.Run(error, share_link ? share_link->href() : GURL());
}

scoped_ptr<google_apis::FileResource> ConvertResourceEntryToFileResource(
const google_apis::ResourceEntry& entry) {
scoped_ptr<google_apis::FileResource> file(new google_apis::FileResource);

file->set_file_id(entry.resource_id());
file->set_title(entry.title());
file->set_created_date(entry.published_time());

if (std::find(entry.labels().begin(), entry.labels().end(),
"shared-with-me") != entry.labels().end()) {
// Set current time to mark the file is shared_with_me, since ResourceEntry
// doesn't have |shared_with_me_date| equivalent.
file->set_shared_with_me_date(base::Time::Now());
}

file->set_shared(std::find(entry.labels().begin(), entry.labels().end(),
"shared") != entry.labels().end());

if (entry.is_folder()) {
file->set_mime_type(kDriveFolderMimeType);
} else {
std::string mime_type = GetMimeTypeFromEntryKind(entry.kind());
if (mime_type.empty())
mime_type = entry.content_mime_type();
file->set_mime_type(mime_type);
}

file->set_md5_checksum(entry.file_md5());
file->set_file_size(entry.file_size());

file->mutable_labels()->set_trashed(entry.deleted());
file->set_etag(entry.etag());

google_apis::ImageMediaMetadata* image_media_metadata =
file->mutable_image_media_metadata();
image_media_metadata->set_width(entry.image_width());
image_media_metadata->set_height(entry.image_height());
image_media_metadata->set_rotation(entry.image_rotation());

std::vector<google_apis::ParentReference>* parents = file->mutable_parents();
for (size_t i = 0; i < entry.links().size(); ++i) {
using google_apis::Link;
const Link& link = *entry.links()[i];
switch (link.type()) {
case Link::LINK_PARENT: {
google_apis::ParentReference parent;
parent.set_parent_link(link.href());

std::string file_id =
drive::util::ExtractResourceIdFromUrl(link.href());
parent.set_file_id(file_id);
parents->push_back(parent);
break;
}
case Link::LINK_ALTERNATE:
file->set_alternate_link(link.href());
break;
default:
break;
}
}

file->set_modified_date(entry.updated_time());
file->set_last_viewed_by_me_date(entry.last_viewed_time());

return file.Pass();
}

google_apis::DriveEntryKind GetKind(
const google_apis::FileResource& file_resource) {
if (file_resource.IsDirectory())
return google_apis::ENTRY_KIND_FOLDER;

const std::string& mime_type = file_resource.mime_type();
if (mime_type == kGoogleDocumentMimeType)
return google_apis::ENTRY_KIND_DOCUMENT;
if (mime_type == kGoogleSpreadsheetMimeType)
return google_apis::ENTRY_KIND_SPREADSHEET;
if (mime_type == kGooglePresentationMimeType)
return google_apis::ENTRY_KIND_PRESENTATION;
if (mime_type == kGoogleDrawingMimeType)
return google_apis::ENTRY_KIND_DRAWING;
if (mime_type == kGoogleTableMimeType)
return google_apis::ENTRY_KIND_TABLE;
if (mime_type == kGoogleFormMimeType)
return google_apis::ENTRY_KIND_FORM;
if (mime_type == "application/pdf")
return google_apis::ENTRY_KIND_PDF;
return google_apis::ENTRY_KIND_FILE;
}

scoped_ptr<google_apis::ResourceEntry>
ConvertFileResourceToResourceEntry(
const google_apis::FileResource& file_resource) {
Expand All @@ -272,7 +176,12 @@ ConvertFileResourceToResourceEntry(
// ResourceEntry
entry->set_resource_id(file_resource.file_id());
entry->set_id(file_resource.file_id());
entry->set_kind(GetKind(file_resource));
if (file_resource.IsDirectory())
entry->set_kind(google_apis::ResourceEntry::ENTRY_KIND_FOLDER);
else if (IsHostedDocument(file_resource.mime_type()))
entry->set_kind(google_apis::ResourceEntry::ENTRY_KIND_UNKNOWN);
else
entry->set_kind(google_apis::ResourceEntry::ENTRY_KIND_FILE);
entry->set_title(file_resource.title());
entry->set_published_time(file_resource.created_date());

Expand Down Expand Up @@ -435,5 +344,37 @@ std::string GetMd5Digest(const base::FilePath& file_path) {

const char kWapiRootDirectoryResourceId[] = "folder:root";

std::string GetHostedDocumentExtension(const std::string& mime_type) {
for (size_t i = 0; i < arraysize(kHostedDocumentKinds); ++i) {
if (mime_type == kHostedDocumentKinds[i].mime_type)
return kHostedDocumentKinds[i].extension;
}
return std::string();
}

std::string GetHostedDocumentMimeType(const std::string& extension) {
for (size_t i = 0; i < arraysize(kHostedDocumentKinds); ++i) {
if (extension == kHostedDocumentKinds[i].extension)
return kHostedDocumentKinds[i].mime_type;
}
return std::string();
}

bool IsHostedDocument(const std::string& mime_type) {
for (size_t i = 0; i < arraysize(kHostedDocumentKinds); ++i) {
if (mime_type == kHostedDocumentKinds[i].mime_type)
return true;
}
return false;
}

bool IsHostedDocumentByExtension(const std::string& extension) {
for (size_t i = 0; i < arraysize(kHostedDocumentKinds); ++i) {
if (extension == kHostedDocumentKinds[i].extension)
return true;
}
return false;
}

} // namespace util
} // namespace drive
Loading

0 comments on commit 51ed7bd

Please sign in to comment.