1 // Copyright 2020 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/extensions/file_manager/private_api_media_parser_util.h"
6
7 #include "base/check.h"
8 #include "base/values.h"
9 #include "chrome/common/extensions/api/file_manager_private.h"
10 #include "net/base/mime_util.h"
11
12 namespace {
13
14 template <class T>
SetValueScopedPtr(T value,std::unique_ptr<T> * destination)15 void SetValueScopedPtr(T value, std::unique_ptr<T>* destination) {
16 DCHECK(destination);
17 if (value >= 0)
18 destination->reset(new T(value));
19 }
20
21 template <>
SetValueScopedPtr(std::string value,std::unique_ptr<std::string> * destination)22 void SetValueScopedPtr(std::string value,
23 std::unique_ptr<std::string>* destination) {
24 DCHECK(destination);
25 if (!value.empty())
26 destination->reset(new std::string(std::move(value)));
27 }
28
ChangeAudioMimePrefixToVideo(std::string * mime_type)29 void ChangeAudioMimePrefixToVideo(std::string* mime_type) {
30 const std::string audio_type("audio/*");
31 if (net::MatchesMimeType(audio_type, *mime_type))
32 mime_type->replace(0, audio_type.length() - 1, "video/");
33 }
34
35 } // namespace
36
37 namespace extensions {
38
39 namespace api {
40
41 namespace file_manager_private {
42
MojoMediaMetadataToValue(chrome::mojom::MediaMetadataPtr metadata)43 std::unique_ptr<base::DictionaryValue> MojoMediaMetadataToValue(
44 chrome::mojom::MediaMetadataPtr metadata) {
45 DCHECK(metadata);
46
47 file_manager_private::MediaMetadata media_metadata;
48 media_metadata.mime_type = std::move(metadata->mime_type);
49
50 // Video files have dimensions.
51 if (metadata->height >= 0 && metadata->width >= 0) {
52 ChangeAudioMimePrefixToVideo(&media_metadata.mime_type);
53 SetValueScopedPtr(metadata->height, &media_metadata.height);
54 SetValueScopedPtr(metadata->width, &media_metadata.width);
55 }
56
57 SetValueScopedPtr(metadata->duration, &media_metadata.duration);
58 SetValueScopedPtr(metadata->rotation, &media_metadata.rotation);
59 SetValueScopedPtr(std::move(metadata->artist), &media_metadata.artist);
60 SetValueScopedPtr(std::move(metadata->album), &media_metadata.album);
61 SetValueScopedPtr(std::move(metadata->comment), &media_metadata.comment);
62 SetValueScopedPtr(std::move(metadata->copyright), &media_metadata.copyright);
63 SetValueScopedPtr(metadata->disc, &media_metadata.disc);
64 SetValueScopedPtr(std::move(metadata->genre), &media_metadata.genre);
65 SetValueScopedPtr(std::move(metadata->language), &media_metadata.language);
66 SetValueScopedPtr(std::move(metadata->title), &media_metadata.title);
67 SetValueScopedPtr(metadata->track, &media_metadata.track);
68
69 for (const chrome::mojom::MediaStreamInfoPtr& info : metadata->raw_tags) {
70 file_manager_private::StreamInfo stream_info;
71 stream_info.type = std::move(info->type);
72 base::DictionaryValue* value = nullptr;
73 info->additional_properties.GetAsDictionary(&value);
74 stream_info.tags.additional_properties.Swap(value);
75 media_metadata.raw_tags.push_back(std::move(stream_info));
76 }
77
78 return media_metadata.ToValue();
79 }
80
81 } // namespace file_manager_private
82
83 } // namespace api
84
85 } // namespace extensions
86