Ensure low-memory renderers retry failed loads correctly.
[chromium-blink-merge.git] / components / drive / resource_entry_conversion.cc
bloba4fa83d613c8e52075782126f4f2031664a956cb
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/drive/resource_entry_conversion.h"
7 #include <string>
9 #include "base/logging.h"
10 #include "base/time/time.h"
11 #include "components/drive/drive.pb.h"
12 #include "components/drive/drive_api_util.h"
13 #include "components/drive/file_system_core_util.h"
14 #include "google_apis/drive/drive_api_parser.h"
16 namespace drive {
18 bool ConvertChangeResourceToResourceEntry(
19 const google_apis::ChangeResource& input,
20 ResourceEntry* out_entry,
21 std::string* out_parent_resource_id) {
22 DCHECK(out_entry);
23 DCHECK(out_parent_resource_id);
25 ResourceEntry converted;
26 std::string parent_resource_id;
27 if (input.file() &&
28 !ConvertFileResourceToResourceEntry(*input.file(), &converted,
29 &parent_resource_id))
30 return false;
32 converted.set_resource_id(input.file_id());
33 converted.set_deleted(converted.deleted() || input.is_deleted());
34 converted.set_modification_date(input.modification_date().ToInternalValue());
36 out_entry->Swap(&converted);
37 swap(*out_parent_resource_id, parent_resource_id);
38 return true;
41 bool ConvertFileResourceToResourceEntry(
42 const google_apis::FileResource& input,
43 ResourceEntry* out_entry,
44 std::string* out_parent_resource_id) {
45 DCHECK(out_entry);
46 DCHECK(out_parent_resource_id);
47 ResourceEntry converted;
49 // For regular files, the 'filename' and 'title' attribute in the metadata
50 // may be different (e.g. due to rename). To be consistent with the web
51 // interface and other client to use the 'title' attribute, instead of
52 // 'filename', as the file name in the local snapshot.
53 converted.set_title(input.title());
54 converted.set_base_name(util::NormalizeFileName(converted.title()));
55 converted.set_resource_id(input.file_id());
57 // Gets parent Resource ID. On drive.google.com, a file can have multiple
58 // parents or no parent, but we are forcing a tree-shaped structure (i.e. no
59 // multi-parent or zero-parent entries). Therefore the first found "parent" is
60 // used for the entry. Tracked in http://crbug.com/158904.
61 std::string parent_resource_id;
62 if (!input.parents().empty())
63 parent_resource_id = input.parents()[0].file_id();
65 converted.set_deleted(input.labels().is_trashed());
66 converted.set_shared_with_me(!input.shared_with_me_date().is_null());
67 converted.set_shared(input.shared());
69 PlatformFileInfoProto* file_info = converted.mutable_file_info();
71 file_info->set_last_modified(input.modified_date().ToInternalValue());
72 // If the file has never been viewed (last_viewed_by_me_date().is_null() ==
73 // true), then we will set the last_accessed field in the protocol buffer to
74 // 0.
75 file_info->set_last_accessed(
76 input.last_viewed_by_me_date().ToInternalValue());
77 file_info->set_creation_time(input.created_date().ToInternalValue());
79 if (input.IsDirectory()) {
80 file_info->set_is_directory(true);
81 } else {
82 FileSpecificInfo* file_specific_info =
83 converted.mutable_file_specific_info();
84 if (!input.IsHostedDocument()) {
85 file_info->set_size(input.file_size());
86 file_specific_info->set_md5(input.md5_checksum());
87 file_specific_info->set_is_hosted_document(false);
88 } else {
89 // Attach .g<something> extension to hosted documents so we can special
90 // case their handling in UI.
91 // TODO(satorux): Figure out better way how to pass input info like kind
92 // to UI through the File API stack.
93 const std::string document_extension =
94 drive::util::GetHostedDocumentExtension(input.mime_type());
95 file_specific_info->set_document_extension(document_extension);
96 converted.set_base_name(
97 util::NormalizeFileName(converted.title() + document_extension));
99 // We don't know the size of hosted docs and it does not matter since
100 // it has no effect on the quota.
101 file_info->set_size(0);
102 file_specific_info->set_is_hosted_document(true);
104 file_info->set_is_directory(false);
105 file_specific_info->set_content_mime_type(input.mime_type());
107 if (!input.alternate_link().is_empty())
108 file_specific_info->set_alternate_url(input.alternate_link().spec());
110 const int64 image_width = input.image_media_metadata().width();
111 if (image_width != -1)
112 file_specific_info->set_image_width(image_width);
114 const int64 image_height = input.image_media_metadata().height();
115 if (image_height != -1)
116 file_specific_info->set_image_height(image_height);
118 const int64 image_rotation = input.image_media_metadata().rotation();
119 if (image_rotation != -1)
120 file_specific_info->set_image_rotation(image_rotation);
123 out_entry->Swap(&converted);
124 swap(*out_parent_resource_id, parent_resource_id);
125 return true;
128 void ConvertResourceEntryToFileInfo(const ResourceEntry& entry,
129 base::File::Info* file_info) {
130 file_info->size = entry.file_info().size();
131 file_info->is_directory = entry.file_info().is_directory();
132 file_info->is_symbolic_link = entry.file_info().is_symbolic_link();
133 file_info->last_modified = base::Time::FromInternalValue(
134 entry.file_info().last_modified());
135 file_info->last_accessed = base::Time::FromInternalValue(
136 entry.file_info().last_accessed());
137 file_info->creation_time = base::Time::FromInternalValue(
138 entry.file_info().creation_time());
141 } // namespace drive