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/media/feeds/media_feeds_utils.h"
6 
7 #include "chrome/browser/media/feeds/media_feeds.pb.h"
8 #include "chrome/browser/media/feeds/media_feeds_store.mojom.h"
9 #include "services/media_session/public/cpp/media_image.h"
10 
11 namespace media_feeds {
12 
MojoContentAttributeToProto(media_feeds::mojom::ContentAttribute attribute)13 Image::ContentAttribute MojoContentAttributeToProto(
14     media_feeds::mojom::ContentAttribute attribute) {
15   switch (attribute) {
16     case media_feeds::mojom::ContentAttribute::kIconic:
17       return Image::ContentAttribute::Image_ContentAttribute_ICONIC;
18     case media_feeds::mojom::ContentAttribute::kSceneStill:
19       return Image::ContentAttribute::Image_ContentAttribute_SCENE_STILL;
20     case media_feeds::mojom::ContentAttribute::kPoster:
21       return Image::ContentAttribute::Image_ContentAttribute_POSTER;
22     case media_feeds::mojom::ContentAttribute::kBackground:
23       return Image::ContentAttribute::Image_ContentAttribute_BACKGROUND;
24     case media_feeds::mojom::ContentAttribute::kForDarkBackground:
25       return Image::ContentAttribute::
26           Image_ContentAttribute_FOR_DARK_BACKGROUND;
27     case media_feeds::mojom::ContentAttribute::kForLightBackground:
28       return Image::ContentAttribute::
29           Image_ContentAttribute_FOR_LIGHT_BACKGROUND;
30     case media_feeds::mojom::ContentAttribute::kCentered:
31       return Image::ContentAttribute::Image_ContentAttribute_CENTERED;
32     case media_feeds::mojom::ContentAttribute::kRightCentered:
33       return Image::ContentAttribute::Image_ContentAttribute_RIGHT_CENTERED;
34     case media_feeds::mojom::ContentAttribute::kLeftCentered:
35       return Image::ContentAttribute::Image_ContentAttribute_LEFT_CENTERED;
36     case media_feeds::mojom::ContentAttribute::kHasTransparentBackground:
37       return Image::ContentAttribute::
38           Image_ContentAttribute_HAS_TRANSPARENT_BACKGROUND;
39     case media_feeds::mojom::ContentAttribute::kHasTitle:
40       return Image::ContentAttribute::Image_ContentAttribute_HAS_TITLE;
41     case media_feeds::mojom::ContentAttribute::kNoTitle:
42       return Image::ContentAttribute::Image_ContentAttribute_NO_TITLE;
43     case media_feeds::mojom::ContentAttribute::kUnknown:
44       return Image::ContentAttribute::
45           Image_ContentAttribute_CONTENT_ATTRIBUTE_UNSPECIFIED;
46   }
47 }
48 
ProtoContentAttributeToMojo(Image::ContentAttribute attribute)49 media_feeds::mojom::ContentAttribute ProtoContentAttributeToMojo(
50     Image::ContentAttribute attribute) {
51   switch (attribute) {
52     case Image::ContentAttribute::Image_ContentAttribute_ICONIC:
53       return media_feeds::mojom::ContentAttribute::kIconic;
54     case Image::ContentAttribute::Image_ContentAttribute_SCENE_STILL:
55       return media_feeds::mojom::ContentAttribute::kSceneStill;
56     case Image::ContentAttribute::Image_ContentAttribute_POSTER:
57       return media_feeds::mojom::ContentAttribute::kPoster;
58     case Image::ContentAttribute::Image_ContentAttribute_BACKGROUND:
59       return media_feeds::mojom::ContentAttribute::kBackground;
60     case Image::ContentAttribute::Image_ContentAttribute_FOR_DARK_BACKGROUND:
61       return media_feeds::mojom::ContentAttribute::kForDarkBackground;
62     case Image::ContentAttribute::Image_ContentAttribute_FOR_LIGHT_BACKGROUND:
63       return media_feeds::mojom::ContentAttribute::kForLightBackground;
64     case Image::ContentAttribute::Image_ContentAttribute_CENTERED:
65       return media_feeds::mojom::ContentAttribute::kCentered;
66     case Image::ContentAttribute::Image_ContentAttribute_RIGHT_CENTERED:
67       return media_feeds::mojom::ContentAttribute::kRightCentered;
68     case Image::ContentAttribute::Image_ContentAttribute_LEFT_CENTERED:
69       return media_feeds::mojom::ContentAttribute::kLeftCentered;
70     case Image::ContentAttribute::
71         Image_ContentAttribute_HAS_TRANSPARENT_BACKGROUND:
72       return media_feeds::mojom::ContentAttribute::kHasTransparentBackground;
73     case Image::ContentAttribute::Image_ContentAttribute_HAS_TITLE:
74       return media_feeds::mojom::ContentAttribute::kHasTitle;
75     case Image::ContentAttribute::Image_ContentAttribute_NO_TITLE:
76       return media_feeds::mojom::ContentAttribute::kNoTitle;
77     case Image::ContentAttribute::
78         Image_ContentAttribute_CONTENT_ATTRIBUTE_UNSPECIFIED:
79       return media_feeds::mojom::ContentAttribute::kUnknown;
80     case Image::ContentAttribute::
81         Image_ContentAttribute_Image_ContentAttribute_INT_MIN_SENTINEL_DO_NOT_USE_:
82     case Image::ContentAttribute::
83         Image_ContentAttribute_Image_ContentAttribute_INT_MAX_SENTINEL_DO_NOT_USE_:
84       NOTREACHED();
85       return media_feeds::mojom::ContentAttribute::kUnknown;
86   }
87 }
88 
MediaImageToProto(Image * proto,const media_feeds::mojom::MediaImagePtr & image)89 void MediaImageToProto(Image* proto,
90                        const media_feeds::mojom::MediaImagePtr& image) {
91   if (!image)
92     return;
93 
94   proto->set_url(image->src.spec());
95   proto->set_width(image->size.width());
96   proto->set_height(image->size.height());
97 
98   for (const auto attribute : image->content_attributes)
99     proto->add_content_attribute(MojoContentAttributeToProto(attribute));
100 }
101 
MediaImagesToProto(const std::vector<media_feeds::mojom::MediaImagePtr> & images,int max_number)102 ImageSet MediaImagesToProto(
103     const std::vector<media_feeds::mojom::MediaImagePtr>& images,
104     int max_number) {
105   ImageSet image_set;
106 
107   for (auto& image : images) {
108     MediaImageToProto(image_set.add_image(), image);
109 
110     if (image_set.image().size() >= max_number)
111       break;
112   }
113 
114   return image_set;
115 }
116 
ProtoToMediaImage(const Image & proto)117 media_feeds::mojom::MediaImagePtr ProtoToMediaImage(const Image& proto) {
118   media_feeds::mojom::MediaImagePtr image =
119       media_feeds::mojom::MediaImage::New();
120   image->src = GURL(proto.url());
121   image->size = gfx::Size(proto.width(), proto.height());
122 
123   for (int i = 0; i < proto.content_attribute_size(); i++) {
124     image->content_attributes.push_back(
125         ProtoContentAttributeToMojo(proto.content_attribute(i)));
126   }
127 
128   return image;
129 }
130 
ProtoToMediaImages(const ImageSet & image_set,unsigned max_number)131 std::vector<media_feeds::mojom::MediaImagePtr> ProtoToMediaImages(
132     const ImageSet& image_set,
133     unsigned max_number) {
134   std::vector<media_feeds::mojom::MediaImagePtr> images;
135 
136   for (auto& proto : image_set.image()) {
137     images.push_back(ProtoToMediaImage(proto));
138 
139     if (images.size() >= max_number)
140       break;
141   }
142 
143   return images;
144 }
145 
146 }  // namespace media_feeds
147