Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / drive / resource_entry_conversion.cc
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.
4
5 #include "chrome/browser/chromeos/drive/resource_entry_conversion.h"
6
7 #include <algorithm>
8 #include <string>
9
10 #include "base/logging.h"
11 #include "base/platform_file.h"
12 #include "base/time/time.h"
13 #include "chrome/browser/chromeos/drive/drive.pb.h"
14 #include "chrome/browser/chromeos/drive/file_system_util.h"
15 #include "chrome/browser/drive/drive_api_util.h"
16 #include "google_apis/drive/gdata_wapi_parser.h"
17
18 namespace drive {
19
20 namespace {
21
22 const char kSharedWithMeLabel[] = "shared-with-me";
23 const char kSharedLabel[] = "shared";
24
25 // Checks if |entry| has a specified label.
26 bool HasLabel(const google_apis::ResourceEntry& entry,
27               const std::string& label) {
28   std::vector<std::string>::const_iterator it =
29       std::find(entry.labels().begin(), entry.labels().end(), label);
30   return it != entry.labels().end();
31 }
32
33 }  // namespace
34
35 bool ConvertToResourceEntry(const google_apis::ResourceEntry& input,
36                             ResourceEntry* out_entry,
37                             std::string* out_parent_resource_id) {
38   DCHECK(out_entry);
39   DCHECK(out_parent_resource_id);
40
41   ResourceEntry converted;
42
43   // For regular files, the 'filename' and 'title' attribute in the metadata
44   // may be different (e.g. due to rename). To be consistent with the web
45   // interface and other client to use the 'title' attribute, instead of
46   // 'filename', as the file name in the local snapshot.
47   converted.set_title(input.title());
48   converted.set_base_name(util::NormalizeFileName(converted.title()));
49   converted.set_resource_id(input.resource_id());
50   converted.set_modification_date(input.modification_date().ToInternalValue());
51
52   // Gets parent Resource ID. On drive.google.com, a file can have multiple
53   // parents or no parent, but we are forcing a tree-shaped structure (i.e. no
54   // multi-parent or zero-parent entries). Therefore the first found "parent" is
55   // used for the entry and if the entry has no parent, we assign a special ID
56   // which represents no-parent entries. Tracked in http://crbug.com/158904.
57   std::string parent_resource_id;
58   const google_apis::Link* parent_link =
59       input.GetLinkByType(google_apis::Link::LINK_PARENT);
60   if (parent_link)
61     parent_resource_id = util::ExtractResourceIdFromUrl(parent_link->href());
62
63   converted.set_deleted(input.deleted());
64   converted.set_shared_with_me(HasLabel(input, kSharedWithMeLabel));
65   converted.set_shared(HasLabel(input, kSharedLabel));
66
67   PlatformFileInfoProto* file_info = converted.mutable_file_info();
68
69   file_info->set_last_modified(input.updated_time().ToInternalValue());
70   // If the file has never been viewed (last_viewed_time().is_null() == true),
71   // then we will set the last_accessed field in the protocol buffer to 0.
72   file_info->set_last_accessed(input.last_viewed_time().ToInternalValue());
73   file_info->set_creation_time(input.published_time().ToInternalValue());
74
75   if (input.is_file() || input.is_hosted_document()) {
76     FileSpecificInfo* file_specific_info =
77         converted.mutable_file_specific_info();
78     if (input.is_file()) {
79       file_info->set_size(input.file_size());
80       file_specific_info->set_md5(input.file_md5());
81
82       // The resumable-edit-media link should only be present for regular
83       // files as hosted documents are not uploadable.
84     } else if (input.is_hosted_document()) {
85       // Attach .g<something> extension to hosted documents so we can special
86       // case their handling in UI.
87       // TODO(satorux): Figure out better way how to pass input info like kind
88       // to UI through the File API stack.
89       const std::string document_extension = input.GetHostedDocumentExtension();
90       file_specific_info->set_document_extension(document_extension);
91       converted.set_base_name(
92           util::NormalizeFileName(converted.title() + document_extension));
93
94       // We don't know the size of hosted docs and it does not matter since
95       // is has no effect on the quota.
96       file_info->set_size(0);
97     }
98     file_info->set_is_directory(false);
99     file_specific_info->set_content_mime_type(input.content_mime_type());
100     file_specific_info->set_is_hosted_document(input.is_hosted_document());
101
102     const google_apis::Link* alternate_link =
103         input.GetLinkByType(google_apis::Link::LINK_ALTERNATE);
104     if (alternate_link)
105       file_specific_info->set_alternate_url(alternate_link->href().spec());
106
107     const int64 image_width = input.image_width();
108     if (image_width != -1)
109       file_specific_info->set_image_width(image_width);
110
111     const int64 image_height = input.image_height();
112     if (image_height != -1)
113       file_specific_info->set_image_height(image_height);
114
115     const int64 image_rotation = input.image_rotation();
116     if (image_rotation != -1)
117       file_specific_info->set_image_rotation(image_rotation);
118   } else if (input.is_folder()) {
119     file_info->set_is_directory(true);
120   } else {
121     // There are two cases to reach here.
122     // * The entry is something that doesn't map into files (i.e. sites).
123     //   We don't handle these kind of entries hence return false.
124     // * The entry is un-shared to you by other owner. In that case, we
125     //   get an entry with only deleted() and resource_id() fields are
126     //   filled. Since we want to delete such entries locally as well,
127     //   in that case we need to return true to proceed.
128     if (!input.deleted())
129       return false;
130   }
131
132   out_entry->Swap(&converted);
133   swap(*out_parent_resource_id, parent_resource_id);
134   return true;
135 }
136
137 void ConvertResourceEntryToFileInfo(const ResourceEntry& entry,
138                                     base::File::Info* file_info) {
139   file_info->size = entry.file_info().size();
140   file_info->is_directory = entry.file_info().is_directory();
141   file_info->is_symbolic_link = entry.file_info().is_symbolic_link();
142   file_info->last_modified = base::Time::FromInternalValue(
143       entry.file_info().last_modified());
144   file_info->last_accessed = base::Time::FromInternalValue(
145       entry.file_info().last_accessed());
146   file_info->creation_time = base::Time::FromInternalValue(
147       entry.file_info().creation_time());
148 }
149
150 void SetPlatformFileInfoToResourceEntry(const base::File::Info& file_info,
151                                         ResourceEntry* entry) {
152   PlatformFileInfoProto* entry_file_info = entry->mutable_file_info();
153   entry_file_info->set_size(file_info.size);
154   entry_file_info->set_is_directory(file_info.is_directory);
155   entry_file_info->set_is_symbolic_link(file_info.is_symbolic_link);
156   entry_file_info->set_last_modified(file_info.last_modified.ToInternalValue());
157   entry_file_info->set_last_accessed(file_info.last_accessed.ToInternalValue());
158   entry_file_info->set_creation_time(file_info.creation_time.ToInternalValue());
159 }
160
161 }  // namespace drive