File: web_app_proto_utils.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (410 lines) | stat: -rw-r--r-- 14,663 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/web_applications/web_app_proto_utils.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/web_applications/mojom/user_display_mode.mojom.h"
#include "chrome/browser/web_applications/proto/web_app_url_pattern.pb.h"
#include "chrome/browser/web_applications/user_display_mode.h"
#include "components/services/app_service/public/cpp/icon_info.h"
#include "third_party/blink/public/common/manifest/manifest.h"
#include "third_party/blink/public/mojom/manifest/manifest.mojom.h"
#include "third_party/liburlpattern/pattern.h"
#include "ui/gfx/geometry/size.h"

namespace web_app {

namespace {

std::optional<apps::IconInfo::Purpose> SyncPurposeToIconInfoPurpose(
    sync_pb::WebAppIconInfo_Purpose purpose) {
  switch (purpose) {
    // Treat UNSPECIFIED purpose as invalid. It means a new purpose was added
    // that this client does not understand.
    case sync_pb::WebAppIconInfo_Purpose_UNSPECIFIED:
      return std::nullopt;
    case sync_pb::WebAppIconInfo_Purpose_ANY:
      return apps::IconInfo::Purpose::kAny;
    case sync_pb::WebAppIconInfo_Purpose_MASKABLE:
      return apps::IconInfo::Purpose::kMaskable;
    case sync_pb::WebAppIconInfo_Purpose_MONOCHROME:
      return apps::IconInfo::Purpose::kMonochrome;
  }
}

sync_pb::WebAppIconInfo_Purpose IconInfoPurposeToSyncPurpose(
    apps::IconInfo::Purpose purpose) {
  switch (purpose) {
    case apps::IconInfo::Purpose::kAny:
      return sync_pb::WebAppIconInfo_Purpose_ANY;
    case apps::IconInfo::Purpose::kMonochrome:
      return sync_pb::WebAppIconInfo_Purpose_MONOCHROME;
    case apps::IconInfo::Purpose::kMaskable:
      return sync_pb::WebAppIconInfo_Purpose_MASKABLE;
  }
}

content::proto::ImageResource_Purpose
ManifestImageResourcePurposeToImageResoucePurposeProto(
    blink::mojom::ManifestImageResource_Purpose purpose) {
  switch (purpose) {
    case blink::mojom::ManifestImageResource_Purpose::ANY:
      return content::proto::ImageResource_Purpose_ANY;
    case blink::mojom::ManifestImageResource_Purpose::MONOCHROME:
      return content::proto::ImageResource_Purpose_MONOCHROME;
    case blink::mojom::ManifestImageResource_Purpose::MASKABLE:
      return content::proto::ImageResource_Purpose_MASKABLE;
  }
}

proto::UrlPatternPart::Modifier UrlPatternModifierToProto(
    liburlpattern::Modifier modifier) {
  switch (modifier) {
    case liburlpattern::Modifier::kZeroOrMore:
      return proto::UrlPatternPart::MODIFIER_ZERO_OR_MORE;
    case liburlpattern::Modifier::kOptional:
      return proto::UrlPatternPart::MODIFIER_OPTIONAL;
    case liburlpattern::Modifier::kOneOrMore:
      return proto::UrlPatternPart::MODIFIER_ONE_OR_MORE;
    case liburlpattern::Modifier::kNone:
      return proto::UrlPatternPart::MODIFIER_NONE;
  }
}

std::optional<liburlpattern::Modifier> ProtoToUrlPatternModifier(
    proto::UrlPatternPart::Modifier modifier) {
  switch (modifier) {
    case proto::UrlPatternPart::MODIFIER_UNSPECIFIED:
      return std::nullopt;
    case proto::UrlPatternPart::MODIFIER_ZERO_OR_MORE:
      return liburlpattern::Modifier::kZeroOrMore;
    case proto::UrlPatternPart::MODIFIER_OPTIONAL:
      return liburlpattern::Modifier::kOptional;
    case proto::UrlPatternPart::MODIFIER_ONE_OR_MORE:
      return liburlpattern::Modifier::kOneOrMore;
    case proto::UrlPatternPart::MODIFIER_NONE:
      return liburlpattern::Modifier::kNone;
  }
}

proto::UrlPatternPart::PartType UrlPatternPartTypeToProto(
    liburlpattern::PartType part_type) {
  switch (part_type) {
    case liburlpattern::PartType::kRegex:
      NOTREACHED();
    case liburlpattern::PartType::kFullWildcard:
      return proto::UrlPatternPart::PART_TYPE_FULL_WILDCARD;
    case liburlpattern::PartType::kSegmentWildcard:
      return proto::UrlPatternPart::PART_TYPE_SEGMENT_WILDCARD;
    case liburlpattern::PartType::kFixed:
      return proto::UrlPatternPart::PART_TYPE_FIXED;
  }
}

std::optional<liburlpattern::PartType> ProtoToUrlPatternPartType(
    proto::UrlPatternPart::PartType part_type) {
  switch (part_type) {
    case proto::UrlPatternPart::PART_TYPE_UNSPECIFIED:
      return std::nullopt;
    case proto::UrlPatternPart::PART_TYPE_FULL_WILDCARD:
      return liburlpattern::PartType::kFullWildcard;
    case proto::UrlPatternPart::PART_TYPE_SEGMENT_WILDCARD:
      return liburlpattern::PartType::kSegmentWildcard;
    case proto::UrlPatternPart::PART_TYPE_FIXED:
      return liburlpattern::PartType::kFixed;
  }
}

TabStrip::Visibility ProtoToTabStripVisibility(
    proto::TabStrip::Visibility visibility) {
  switch (visibility) {
    case proto::TabStrip::VISIBILITY_AUTO:
      return TabStrip::Visibility::kAuto;
    case proto::TabStrip::VISIBILITY_ABSENT:
      return TabStrip::Visibility::kAbsent;
  }
}

}  // namespace

std::optional<std::vector<apps::IconInfo>> ParseAppIconInfos(
    const char* container_name_for_logging,
    const RepeatedIconInfosProto& manifest_icons_proto) {
  std::vector<apps::IconInfo> manifest_icons;
  for (const sync_pb::WebAppIconInfo& icon_info_proto : manifest_icons_proto) {
    apps::IconInfo icon_info;

    if (icon_info_proto.has_size_in_px())
      icon_info.square_size_px = icon_info_proto.size_in_px();

    if (!icon_info_proto.has_url()) {
      DLOG(ERROR) << container_name_for_logging << " IconInfo has missing url";
      return std::nullopt;
    }
    icon_info.url = GURL(icon_info_proto.url());
    if (!icon_info.url.is_valid()) {
      DLOG(ERROR) << container_name_for_logging << " IconInfo has invalid url: "
                  << icon_info.url.possibly_invalid_spec();
      return std::nullopt;
    }

    if (icon_info_proto.has_purpose()) {
      std::optional<apps::IconInfo::Purpose> opt_purpose =
          SyncPurposeToIconInfoPurpose(icon_info_proto.purpose());
      if (!opt_purpose.has_value())
        return std::nullopt;
      icon_info.purpose = opt_purpose.value();
    } else {
      // Treat unset purpose as ANY so that old data without the field is
      // interpreted correctly.
      icon_info.purpose = apps::IconInfo::Purpose::kAny;
    }

    manifest_icons.push_back(std::move(icon_info));
  }
  return manifest_icons;
}

std::optional<std::vector<blink::Manifest::ImageResource>>
ParseAppImageResource(const char* container_name_for_logging,
                      const RepeatedImageResourceProto& manifest_icons_proto) {
  std::vector<blink::Manifest::ImageResource> manifest_icons;
  for (const content::proto::ImageResource& image_resource_proto :
       manifest_icons_proto) {
    blink::Manifest::ImageResource image_resource;

    if (!image_resource_proto.has_src()) {
      DLOG(ERROR) << container_name_for_logging
                  << " ImageResource has missing url";
      return std::nullopt;
    }
    image_resource.src = GURL(image_resource_proto.src());

    if (!image_resource.src.is_valid()) {
      DLOG(ERROR) << container_name_for_logging
                  << " ImageResource has invalid url: "
                  << image_resource.src.possibly_invalid_spec();
      return std::nullopt;
    }

    if (image_resource_proto.has_type()) {
      image_resource.type = base::ASCIIToUTF16(image_resource_proto.type());
    }

    if (!image_resource_proto.sizes().empty()) {
      std::vector<gfx::Size> sizes;
      for (const auto& size_proto : image_resource_proto.sizes()) {
        sizes.emplace_back(size_proto.width(), size_proto.height());
      }
      image_resource.sizes = std::move(sizes);
    }

    std::vector<blink::mojom::ManifestImageResource_Purpose> purpose;
    if (!image_resource_proto.purpose().empty()) {
      for (const auto& purpose_proto : image_resource_proto.purpose()) {
        switch (purpose_proto) {
          case content::proto::ImageResource_Purpose_ANY:
            purpose.push_back(blink::mojom::ManifestImageResource_Purpose::ANY);
            break;
          case content::proto::ImageResource_Purpose_MASKABLE:
            purpose.push_back(
                blink::mojom::ManifestImageResource_Purpose::MASKABLE);
            break;
          case content::proto::ImageResource_Purpose_MONOCHROME:
            purpose.push_back(
                blink::mojom::ManifestImageResource_Purpose::MONOCHROME);
            break;
        }
      }
    } else {
      purpose.push_back(blink::mojom::ManifestImageResource_Purpose::ANY);
    }
    image_resource.purpose = std::move(purpose);
    manifest_icons.push_back(std::move(image_resource));
  }

  return manifest_icons;
}

sync_pb::WebAppIconInfo AppIconInfoToSyncProto(
    const apps::IconInfo& icon_info) {
  sync_pb::WebAppIconInfo icon_info_proto;
  if (icon_info.square_size_px.has_value())
    icon_info_proto.set_size_in_px(icon_info.square_size_px.value());
  DCHECK(!icon_info.url.is_empty());
  icon_info_proto.set_url(icon_info.url.spec());
  icon_info_proto.set_purpose(IconInfoPurposeToSyncPurpose(icon_info.purpose));
  return icon_info_proto;
}

content::proto::ImageResource AppImageResourceToProto(
    const blink::Manifest::ImageResource& image_resource) {
  content::proto::ImageResource image_resource_proto;
  DCHECK(!image_resource.src.is_empty());

  image_resource_proto.set_src(image_resource.src.spec());

  if (!image_resource.type.empty()) {
    image_resource_proto.set_type(base::UTF16ToASCII(image_resource.type));
  }

  for (const auto& size : image_resource.sizes) {
    content::proto::ImageResource::Size size_proto;
    size_proto.set_width(size.width());
    size_proto.set_height(size.height());
    *(image_resource_proto.add_sizes()) = size_proto;
  }

  for (const auto& purpose : image_resource.purpose) {
    image_resource_proto.add_purpose(
        ManifestImageResourcePurposeToImageResoucePurposeProto(purpose));
  }
  return image_resource_proto;
}

RunOnOsLoginMode ToRunOnOsLoginMode(proto::WebApp::RunOnOsLoginMode mode) {
  switch (mode) {
    case proto::WebApp::RUN_ON_OS_LOGIN_MODE_MINIMIZED:
      return RunOnOsLoginMode::kMinimized;
    case proto::WebApp::RUN_ON_OS_LOGIN_MODE_WINDOWED:
      return RunOnOsLoginMode::kWindowed;
    case proto::WebApp::RUN_ON_OS_LOGIN_MODE_NOT_RUN:
    default:
      return RunOnOsLoginMode::kNotRun;
  }
}

proto::WebApp::RunOnOsLoginMode ToWebAppProtoRunOnOsLoginMode(
    RunOnOsLoginMode mode) {
  switch (mode) {
    case RunOnOsLoginMode::kMinimized:
      return proto::WebApp::RUN_ON_OS_LOGIN_MODE_MINIMIZED;
    case RunOnOsLoginMode::kWindowed:
      return proto::WebApp::RUN_ON_OS_LOGIN_MODE_WINDOWED;
    case RunOnOsLoginMode::kNotRun:
      return proto::WebApp::RUN_ON_OS_LOGIN_MODE_NOT_RUN;
  }
}

std::optional<blink::SafeUrlPattern> ToUrlPattern(
    const proto::UrlPattern& proto_url_pattern) {
  blink::SafeUrlPattern url_pattern;

  for (const proto::UrlPatternPart& proto_part : proto_url_pattern.pathname()) {
    liburlpattern::Part part;

    if (!proto_part.has_part_type()) {
      DLOG(ERROR) << "WebApp UrlPattern Part has missing type";
      continue;
    }
    std::optional<liburlpattern::PartType> opt_part_type =
        ProtoToUrlPatternPartType(proto_part.part_type());
    if (!opt_part_type.has_value()) {
      return std::nullopt;
    }
    part.type = opt_part_type.value();

    if (!proto_part.has_value()) {
      DLOG(ERROR) << "WebApp UrlPattern Part has missing value";
      continue;
    }
    part.value = proto_part.value();

    if (!proto_part.has_modifier()) {
      DLOG(ERROR) << "WebApp UrlPattern Part has missing type";
      continue;
    }

    std::optional<liburlpattern::Modifier> opt_modifier =
        ProtoToUrlPatternModifier(proto_part.modifier());
    if (!opt_modifier.has_value()) {
      return std::nullopt;
    }
    part.modifier = opt_modifier.value();

    if (proto_part.has_name()) {
      part.name = proto_part.name();
    }

    if (proto_part.has_prefix()) {
      part.prefix = proto_part.prefix();
    }

    if (proto_part.has_suffix()) {
      part.suffix = proto_part.suffix();
    }

    url_pattern.pathname.push_back(std::move(part));
  }
  return url_pattern;
}

proto::UrlPattern ToUrlPatternProto(const blink::SafeUrlPattern& url_pattern) {
  proto::UrlPattern url_pattern_proto;
  for (const auto& part : url_pattern.pathname) {
    proto::UrlPatternPart* url_pattern_part_proto =
        url_pattern_proto.add_pathname();

    url_pattern_part_proto->set_name(part.name);
    url_pattern_part_proto->set_prefix(part.prefix);
    url_pattern_part_proto->set_value(part.value);
    url_pattern_part_proto->set_suffix(part.suffix);

    url_pattern_part_proto->set_part_type(UrlPatternPartTypeToProto(part.type));
    url_pattern_part_proto->set_modifier(
        UrlPatternModifierToProto(part.modifier));
  }
  return url_pattern_proto;
}

std::optional<TabStrip> ProtoToTabStrip(proto::TabStrip tab_strip_proto) {
  TabStrip tab_strip;
  if (tab_strip_proto.has_home_tab_visibility()) {
    tab_strip.home_tab =
        ProtoToTabStripVisibility(tab_strip_proto.home_tab_visibility());
  } else {
    std::optional<std::vector<blink::Manifest::ImageResource>> icons =
        ParseAppImageResource("WebApp",
                              tab_strip_proto.home_tab_params().icons());
    blink::Manifest::HomeTabParams home_tab_params;
    if (!icons->empty()) {
      home_tab_params.icons = std::move(*icons);
    }

    std::vector<blink::SafeUrlPattern> scope_patterns;
    for (const proto::UrlPattern& proto_url_pattern :
         tab_strip_proto.home_tab_params().scope_patterns()) {
      std::optional<blink::SafeUrlPattern> url_pattern =
          ToUrlPattern(proto_url_pattern);
      if (!url_pattern) {
        return std::nullopt;
      }
      scope_patterns.push_back(url_pattern.value());
    }
    home_tab_params.scope_patterns = std::move(scope_patterns);

    tab_strip.home_tab = std::move(home_tab_params);
  }

  blink::Manifest::NewTabButtonParams new_tab_button_params;
  if (tab_strip_proto.new_tab_button_params().has_url()) {
    new_tab_button_params.url =
        GURL(tab_strip_proto.new_tab_button_params().url());
  }
  tab_strip.new_tab_button = new_tab_button_params;

  return tab_strip;
}

std::string RelativeManifestIdPath(webapps::ManifestId manifest_id) {
  CHECK(manifest_id.is_valid());
  // The relative id does not include the initial '/' character.
  std::string relative_manifest_id_path = manifest_id.PathForRequest();
  if (relative_manifest_id_path.starts_with("/")) {
    relative_manifest_id_path = relative_manifest_id_path.substr(1);
  }
  return relative_manifest_id_path;
}

}  // namespace web_app