summaryrefslogtreecommitdiff
path: root/chromium/services/network/public/cpp/cookie_manager_mojom_traits.cc
blob: d9fdedbe4ddb4eefa9792fa933ca5f5ad1da85a7 (plain)
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "services/network/public/cpp/cookie_manager_mojom_traits.h"

#include "mojo/public/cpp/base/time_mojom_traits.h"
#include "net/cookies/cookie_options.h"
#include "net/cookies/same_party_context.h"

namespace mojo {

network::mojom::CookiePriority
EnumTraits<network::mojom::CookiePriority, net::CookiePriority>::ToMojom(
    net::CookiePriority input) {
  switch (input) {
    case net::COOKIE_PRIORITY_LOW:
      return network::mojom::CookiePriority::LOW;
    case net::COOKIE_PRIORITY_MEDIUM:
      return network::mojom::CookiePriority::MEDIUM;
    case net::COOKIE_PRIORITY_HIGH:
      return network::mojom::CookiePriority::HIGH;
  }
  NOTREACHED();
  return static_cast<network::mojom::CookiePriority>(input);
}

bool EnumTraits<network::mojom::CookiePriority, net::CookiePriority>::FromMojom(
    network::mojom::CookiePriority input,
    net::CookiePriority* output) {
  switch (input) {
    case network::mojom::CookiePriority::LOW:
      *output = net::CookiePriority::COOKIE_PRIORITY_LOW;
      return true;
    case network::mojom::CookiePriority::MEDIUM:
      *output = net::CookiePriority::COOKIE_PRIORITY_MEDIUM;
      return true;
    case network::mojom::CookiePriority::HIGH:
      *output = net::CookiePriority::COOKIE_PRIORITY_HIGH;
      return true;
  }
  return false;
}

network::mojom::CookieSameSite
EnumTraits<network::mojom::CookieSameSite, net::CookieSameSite>::ToMojom(
    net::CookieSameSite input) {
  switch (input) {
    case net::CookieSameSite::UNSPECIFIED:
      return network::mojom::CookieSameSite::UNSPECIFIED;
    case net::CookieSameSite::NO_RESTRICTION:
      return network::mojom::CookieSameSite::NO_RESTRICTION;
    case net::CookieSameSite::LAX_MODE:
      return network::mojom::CookieSameSite::LAX_MODE;
    case net::CookieSameSite::STRICT_MODE:
      return network::mojom::CookieSameSite::STRICT_MODE;
    default:
      break;
  }
  NOTREACHED();
  return static_cast<network::mojom::CookieSameSite>(input);
}

bool EnumTraits<network::mojom::CookieSameSite, net::CookieSameSite>::FromMojom(
    network::mojom::CookieSameSite input,
    net::CookieSameSite* output) {
  switch (input) {
    case network::mojom::CookieSameSite::UNSPECIFIED:
      *output = net::CookieSameSite::UNSPECIFIED;
      return true;
    case network::mojom::CookieSameSite::NO_RESTRICTION:
      *output = net::CookieSameSite::NO_RESTRICTION;
      return true;
    case network::mojom::CookieSameSite::LAX_MODE:
      *output = net::CookieSameSite::LAX_MODE;
      return true;
    case network::mojom::CookieSameSite::STRICT_MODE:
      *output = net::CookieSameSite::STRICT_MODE;
      return true;
    default:
      break;
  }
  return false;
}

network::mojom::CookieEffectiveSameSite EnumTraits<
    network::mojom::CookieEffectiveSameSite,
    net::CookieEffectiveSameSite>::ToMojom(net::CookieEffectiveSameSite input) {
  switch (input) {
    case net::CookieEffectiveSameSite::NO_RESTRICTION:
      return network::mojom::CookieEffectiveSameSite::kNoRestriction;
    case net::CookieEffectiveSameSite::LAX_MODE:
      return network::mojom::CookieEffectiveSameSite::kLaxMode;
    case net::CookieEffectiveSameSite::STRICT_MODE:
      return network::mojom::CookieEffectiveSameSite::kStrictMode;
    case net::CookieEffectiveSameSite::LAX_MODE_ALLOW_UNSAFE:
      return network::mojom::CookieEffectiveSameSite::kLaxModeAllowUnsafe;
    case net::CookieEffectiveSameSite::UNDEFINED:
      return network::mojom::CookieEffectiveSameSite::kUndefined;
    default:
      break;
  }
  NOTREACHED();
  return static_cast<network::mojom::CookieEffectiveSameSite>(input);
}

bool EnumTraits<network::mojom::CookieEffectiveSameSite,
                net::CookieEffectiveSameSite>::
    FromMojom(network::mojom::CookieEffectiveSameSite input,
              net::CookieEffectiveSameSite* output) {
  switch (input) {
    case network::mojom::CookieEffectiveSameSite::kNoRestriction:
      *output = net::CookieEffectiveSameSite::NO_RESTRICTION;
      return true;
    case network::mojom::CookieEffectiveSameSite::kLaxMode:
      *output = net::CookieEffectiveSameSite::LAX_MODE;
      return true;
    case network::mojom::CookieEffectiveSameSite::kStrictMode:
      *output = net::CookieEffectiveSameSite::STRICT_MODE;
      return true;
    case network::mojom::CookieEffectiveSameSite::kLaxModeAllowUnsafe:
      *output = net::CookieEffectiveSameSite::LAX_MODE_ALLOW_UNSAFE;
      return true;
    case network::mojom::CookieEffectiveSameSite::kUndefined:
      *output = net::CookieEffectiveSameSite::UNDEFINED;
      return true;
    default:
      break;
  }
  return false;
}

network::mojom::CookieSourceScheme
EnumTraits<network::mojom::CookieSourceScheme,
           net::CookieSourceScheme>::ToMojom(net::CookieSourceScheme input) {
  switch (input) {
    case net::CookieSourceScheme::kUnset:
      return network::mojom::CookieSourceScheme::kUnset;
    case net::CookieSourceScheme::kNonSecure:
      return network::mojom::CookieSourceScheme::kNonSecure;
    case net::CookieSourceScheme::kSecure:
      return network::mojom::CookieSourceScheme::kSecure;
  }
  NOTREACHED();
  return static_cast<network::mojom::CookieSourceScheme>(input);
}

bool EnumTraits<network::mojom::CookieSourceScheme, net::CookieSourceScheme>::
    FromMojom(network::mojom::CookieSourceScheme input,
              net::CookieSourceScheme* output) {
  switch (input) {
    case network::mojom::CookieSourceScheme::kUnset:
      *output = net::CookieSourceScheme::kUnset;
      return true;
    case network::mojom::CookieSourceScheme::kNonSecure:
      *output = net::CookieSourceScheme::kNonSecure;
      return true;
    case network::mojom::CookieSourceScheme::kSecure:
      *output = net::CookieSourceScheme::kSecure;
      return true;
  }
  return false;
}

network::mojom::CookieAccessSemantics EnumTraits<
    network::mojom::CookieAccessSemantics,
    net::CookieAccessSemantics>::ToMojom(net::CookieAccessSemantics input) {
  switch (input) {
    case net::CookieAccessSemantics::UNKNOWN:
      return network::mojom::CookieAccessSemantics::UNKNOWN;
    case net::CookieAccessSemantics::NONLEGACY:
      return network::mojom::CookieAccessSemantics::NONLEGACY;
    case net::CookieAccessSemantics::LEGACY:
      return network::mojom::CookieAccessSemantics::LEGACY;
    default:
      break;
  }
  NOTREACHED();
  return static_cast<network::mojom::CookieAccessSemantics>(input);
}

bool EnumTraits<network::mojom::CookieAccessSemantics,
                net::CookieAccessSemantics>::
    FromMojom(network::mojom::CookieAccessSemantics input,
              net::CookieAccessSemantics* output) {
  switch (input) {
    case network::mojom::CookieAccessSemantics::UNKNOWN:
      *output = net::CookieAccessSemantics::UNKNOWN;
      return true;
    case network::mojom::CookieAccessSemantics::NONLEGACY:
      *output = net::CookieAccessSemantics::NONLEGACY;
      return true;
    case network::mojom::CookieAccessSemantics::LEGACY:
      *output = net::CookieAccessSemantics::LEGACY;
      return true;
    default:
      break;
  }
  return false;
}

network::mojom::ContextType
EnumTraits<network::mojom::ContextType,
           net::CookieOptions::SameSiteCookieContext::ContextType>::
    ToMojom(net::CookieOptions::SameSiteCookieContext::ContextType input) {
  switch (input) {
    case net::CookieOptions::SameSiteCookieContext::ContextType::
        SAME_SITE_STRICT:
      return network::mojom::ContextType::SAME_SITE_STRICT;
    case net::CookieOptions::SameSiteCookieContext::ContextType::SAME_SITE_LAX:
      return network::mojom::ContextType::SAME_SITE_LAX;
    case net::CookieOptions::SameSiteCookieContext::ContextType::
        SAME_SITE_LAX_METHOD_UNSAFE:
      return network::mojom::ContextType::SAME_SITE_LAX_METHOD_UNSAFE;
    case net::CookieOptions::SameSiteCookieContext::ContextType::CROSS_SITE:
      return network::mojom::ContextType::CROSS_SITE;
    default:
      NOTREACHED();
      return network::mojom::ContextType::CROSS_SITE;
  }
}

bool EnumTraits<network::mojom::ContextType,
                net::CookieOptions::SameSiteCookieContext::ContextType>::
    FromMojom(network::mojom::ContextType input,
              net::CookieOptions::SameSiteCookieContext::ContextType* output) {
  switch (input) {
    case network::mojom::ContextType::SAME_SITE_STRICT:
      *output = net::CookieOptions::SameSiteCookieContext::ContextType::
          SAME_SITE_STRICT;
      return true;
    case network::mojom::ContextType::SAME_SITE_LAX:
      *output =
          net::CookieOptions::SameSiteCookieContext::ContextType::SAME_SITE_LAX;
      return true;
    case network::mojom::ContextType::SAME_SITE_LAX_METHOD_UNSAFE:
      *output = net::CookieOptions::SameSiteCookieContext::ContextType::
          SAME_SITE_LAX_METHOD_UNSAFE;
      return true;
    case network::mojom::ContextType::CROSS_SITE:
      *output =
          net::CookieOptions::SameSiteCookieContext::ContextType::CROSS_SITE;
      return true;
  }
  return false;
}

network::mojom::CookieSameSiteContextMetadataDowngradeType
EnumTraits<network::mojom::CookieSameSiteContextMetadataDowngradeType,
           net::CookieOptions::SameSiteCookieContext::ContextMetadata::
               ContextDowngradeType>::
    ToMojom(net::CookieOptions::SameSiteCookieContext::ContextMetadata::
                ContextDowngradeType input) {
  switch (input) {
    case net::CookieOptions::SameSiteCookieContext::ContextMetadata::
        ContextDowngradeType::kNoDowngrade:
      return network::mojom::CookieSameSiteContextMetadataDowngradeType::
          kNoDowngrade;
    case net::CookieOptions::SameSiteCookieContext::ContextMetadata::
        ContextDowngradeType::kStrictToLax:
      return network::mojom::CookieSameSiteContextMetadataDowngradeType::
          kStrictToLax;
    case net::CookieOptions::SameSiteCookieContext::ContextMetadata::
        ContextDowngradeType::kStrictToCross:
      return network::mojom::CookieSameSiteContextMetadataDowngradeType::
          kStrictToCross;
    case net::CookieOptions::SameSiteCookieContext::ContextMetadata::
        ContextDowngradeType::kLaxToCross:
      return network::mojom::CookieSameSiteContextMetadataDowngradeType::
          kLaxToCross;
  }
}

bool EnumTraits<network::mojom::CookieSameSiteContextMetadataDowngradeType,
                net::CookieOptions::SameSiteCookieContext::ContextMetadata::
                    ContextDowngradeType>::
    FromMojom(network::mojom::CookieSameSiteContextMetadataDowngradeType input,
              net::CookieOptions::SameSiteCookieContext::ContextMetadata::
                  ContextDowngradeType* output) {
  switch (input) {
    case network::mojom::CookieSameSiteContextMetadataDowngradeType::
        kNoDowngrade:
      *output = net::CookieOptions::SameSiteCookieContext::ContextMetadata::
          ContextDowngradeType::kNoDowngrade;
      return true;
    case network::mojom::CookieSameSiteContextMetadataDowngradeType::
        kStrictToLax:
      *output = net::CookieOptions::SameSiteCookieContext::ContextMetadata::
          ContextDowngradeType::kStrictToLax;
      return true;
    case network::mojom::CookieSameSiteContextMetadataDowngradeType::
        kStrictToCross:
      *output = net::CookieOptions::SameSiteCookieContext::ContextMetadata::
          ContextDowngradeType::kStrictToCross;
      return true;
    case network::mojom::CookieSameSiteContextMetadataDowngradeType::
        kLaxToCross:
      *output = net::CookieOptions::SameSiteCookieContext::ContextMetadata::
          ContextDowngradeType::kLaxToCross;
      return true;
  }
  return false;
}

network::mojom::CookieChangeCause
EnumTraits<network::mojom::CookieChangeCause, net::CookieChangeCause>::ToMojom(
    net::CookieChangeCause input) {
  switch (input) {
    case net::CookieChangeCause::INSERTED:
      return network::mojom::CookieChangeCause::INSERTED;
    case net::CookieChangeCause::EXPLICIT:
      return network::mojom::CookieChangeCause::EXPLICIT;
    case net::CookieChangeCause::UNKNOWN_DELETION:
      return network::mojom::CookieChangeCause::UNKNOWN_DELETION;
    case net::CookieChangeCause::OVERWRITE:
      return network::mojom::CookieChangeCause::OVERWRITE;
    case net::CookieChangeCause::EXPIRED:
      return network::mojom::CookieChangeCause::EXPIRED;
    case net::CookieChangeCause::EVICTED:
      return network::mojom::CookieChangeCause::EVICTED;
    case net::CookieChangeCause::EXPIRED_OVERWRITE:
      return network::mojom::CookieChangeCause::EXPIRED_OVERWRITE;
    default:
      break;
  }
  NOTREACHED();
  return static_cast<network::mojom::CookieChangeCause>(input);
}

bool EnumTraits<network::mojom::CookieChangeCause, net::CookieChangeCause>::
    FromMojom(network::mojom::CookieChangeCause input,
              net::CookieChangeCause* output) {
  switch (input) {
    case network::mojom::CookieChangeCause::INSERTED:
      *output = net::CookieChangeCause::INSERTED;
      return true;
    case network::mojom::CookieChangeCause::EXPLICIT:
      *output = net::CookieChangeCause::EXPLICIT;
      return true;
    case network::mojom::CookieChangeCause::UNKNOWN_DELETION:
      *output = net::CookieChangeCause::UNKNOWN_DELETION;
      return true;
    case network::mojom::CookieChangeCause::OVERWRITE:
      *output = net::CookieChangeCause::OVERWRITE;
      return true;
    case network::mojom::CookieChangeCause::EXPIRED:
      *output = net::CookieChangeCause::EXPIRED;
      return true;
    case network::mojom::CookieChangeCause::EVICTED:
      *output = net::CookieChangeCause::EVICTED;
      return true;
    case network::mojom::CookieChangeCause::EXPIRED_OVERWRITE:
      *output = net::CookieChangeCause::EXPIRED_OVERWRITE;
      return true;
    default:
      break;
  }
  return false;
}

bool StructTraits<network::mojom::CookieSameSiteContextMetadataDataView,
                  net::CookieOptions::SameSiteCookieContext::ContextMetadata>::
    Read(network::mojom::CookieSameSiteContextMetadataDataView data,
         net::CookieOptions::SameSiteCookieContext::ContextMetadata* out) {
  if (!data.ReadCrossSiteRedirectDowngrade(&out->cross_site_redirect_downgrade))
    return false;

  return true;
}

bool StructTraits<network::mojom::CookieSameSiteContextDataView,
                  net::CookieOptions::SameSiteCookieContext>::
    Read(network::mojom::CookieSameSiteContextDataView mojo_context,
         net::CookieOptions::SameSiteCookieContext* context) {
  net::CookieOptions::SameSiteCookieContext::ContextType context_type;
  if (!mojo_context.ReadContext(&context_type))
    return false;

  net::CookieOptions::SameSiteCookieContext::ContextType schemeful_context;
  if (!mojo_context.ReadSchemefulContext(&schemeful_context))
    return false;

  // schemeful_context must be <= context.
  if (schemeful_context > context_type)
    return false;

  net::CookieOptions::SameSiteCookieContext::ContextMetadata metadata;
  if (!mojo_context.ReadMetadata(&metadata))
    return false;

  net::CookieOptions::SameSiteCookieContext::ContextMetadata schemeful_metadata;
  if (!mojo_context.ReadSchemefulMetadata(&schemeful_metadata))
    return false;

  *context = net::CookieOptions::SameSiteCookieContext(
      context_type, schemeful_context, metadata, schemeful_metadata);
  return true;
}

bool EnumTraits<network::mojom::SamePartyCookieContextType,
                net::SamePartyContext::Type>::
    FromMojom(network::mojom::SamePartyCookieContextType context_type,
              net::SamePartyContext::Type* out) {
  switch (context_type) {
    case network::mojom::SamePartyCookieContextType::kCrossParty:
      *out = net::SamePartyContext::Type::kCrossParty;
      return true;
    case network::mojom::SamePartyCookieContextType::kSameParty:
      *out = net::SamePartyContext::Type::kSameParty;
      return true;
  }
  return false;
}

network::mojom::SamePartyCookieContextType
EnumTraits<network::mojom::SamePartyCookieContextType,
           net::SamePartyContext::Type>::ToMojom(net::SamePartyContext::Type
                                                     context_type) {
  switch (context_type) {
    case net::SamePartyContext::Type::kCrossParty:
      return network::mojom::SamePartyCookieContextType::kCrossParty;
    case net::SamePartyContext::Type::kSameParty:
      return network::mojom::SamePartyCookieContextType::kSameParty;
  }
  NOTREACHED();
  return network::mojom::SamePartyCookieContextType::kCrossParty;
}

bool StructTraits<network::mojom::CookieOptionsDataView, net::CookieOptions>::
    Read(network::mojom::CookieOptionsDataView mojo_options,
         net::CookieOptions* cookie_options) {
  if (mojo_options.exclude_httponly())
    cookie_options->set_exclude_httponly();
  else
    cookie_options->set_include_httponly();

  net::CookieOptions::SameSiteCookieContext same_site_cookie_context;
  if (!mojo_options.ReadSameSiteCookieContext(&same_site_cookie_context))
    return false;
  cookie_options->set_same_site_cookie_context(same_site_cookie_context);

  if (mojo_options.update_access_time())
    cookie_options->set_update_access_time();
  else
    cookie_options->set_do_not_update_access_time();

  if (mojo_options.return_excluded_cookies())
    cookie_options->set_return_excluded_cookies();
  else
    cookie_options->unset_return_excluded_cookies();

  net::SamePartyContext same_party_context;
  if (!mojo_options.ReadSamePartyContext(&same_party_context))
    return false;
  cookie_options->set_same_party_context(same_party_context);

  cookie_options->set_full_party_context_size(
      mojo_options.full_party_context_size());

  cookie_options->set_is_in_nontrivial_first_party_set(
      mojo_options.is_in_nontrivial_first_party_set());

  return true;
}

bool StructTraits<network::mojom::CookiePartitionKeyDataView,
                  net::CookiePartitionKey>::
    Read(network::mojom::CookiePartitionKeyDataView partition_key,
         net::CookiePartitionKey* out) {
  net::SchemefulSite site;
  if (!partition_key.ReadSite(&site))
    return false;
  *out = net::CookiePartitionKey::FromWire(site);
  return true;
}

const std::vector<net::CookiePartitionKey> StructTraits<
    network::mojom::CookiePartitionKeychainDataView,
    net::CookiePartitionKeychain>::keys(const net::CookiePartitionKeychain&
                                            keychain) {
  std::vector<net::CookiePartitionKey> result;
  if (keychain.ContainsAllKeys() || keychain.IsEmpty())
    return result;
  result.insert(result.begin(), keychain.PartitionKeys().begin(),
                keychain.PartitionKeys().end());
  return result;
}

bool StructTraits<network::mojom::CookiePartitionKeychainDataView,
                  net::CookiePartitionKeychain>::
    Read(network::mojom::CookiePartitionKeychainDataView keychain,
         net::CookiePartitionKeychain* out) {
  if (keychain.contains_all_partitions()) {
    *out = net::CookiePartitionKeychain::ContainsAll();
    return true;
  }
  std::vector<net::CookiePartitionKey> keys;
  if (!keychain.ReadKeys(&keys))
    return false;
  *out = net::CookiePartitionKeychain(keys);
  return true;
}

bool StructTraits<
    network::mojom::CanonicalCookieDataView,
    net::CanonicalCookie>::Read(network::mojom::CanonicalCookieDataView cookie,
                                net::CanonicalCookie* out) {
  std::string name;
  if (!cookie.ReadName(&name))
    return false;

  std::string value;
  if (!cookie.ReadValue(&value))
    return false;

  std::string domain;
  if (!cookie.ReadDomain(&domain))
    return false;

  std::string path;
  if (!cookie.ReadPath(&path))
    return false;

  base::Time creation_time;
  base::Time expiry_time;
  base::Time last_access_time;
  if (!cookie.ReadCreation(&creation_time))
    return false;

  if (!cookie.ReadExpiry(&expiry_time))
    return false;

  if (!cookie.ReadLastAccess(&last_access_time))
    return false;

  net::CookieSameSite site_restrictions;
  if (!cookie.ReadSiteRestrictions(&site_restrictions))
    return false;

  net::CookiePriority priority;
  if (!cookie.ReadPriority(&priority))
    return false;

  absl::optional<net::CookiePartitionKey> partition_key;
  if (!cookie.ReadPartitionKey(&partition_key))
    return false;

  net::CookieSourceScheme source_scheme;
  if (!cookie.ReadSourceScheme(&source_scheme))
    return false;

  auto cc = net::CanonicalCookie::FromStorage(
      std::move(name), std::move(value), std::move(domain), std::move(path),
      std::move(creation_time), std::move(expiry_time),
      std::move(last_access_time), cookie.secure(), cookie.httponly(),
      site_restrictions, priority, cookie.same_party(), partition_key,
      source_scheme, cookie.source_port());
  if (!cc)
    return false;
  *out = *cc;
  return true;
}

bool StructTraits<network::mojom::CookieInclusionStatusDataView,
                  net::CookieInclusionStatus>::
    Read(network::mojom::CookieInclusionStatusDataView status,
         net::CookieInclusionStatus* out) {
  *out = net::CookieInclusionStatus();
  out->set_exclusion_reasons(status.exclusion_reasons());
  out->set_warning_reasons(status.warning_reasons());

  return net::CookieInclusionStatus::ValidateExclusionAndWarningFromWire(
      status.exclusion_reasons(), status.warning_reasons());
}

bool StructTraits<network::mojom::CookieAndLineWithAccessResultDataView,
                  net::CookieAndLineWithAccessResult>::
    Read(network::mojom::CookieAndLineWithAccessResultDataView c,
         net::CookieAndLineWithAccessResult* out) {
  absl::optional<net::CanonicalCookie> cookie;
  std::string cookie_string;
  net::CookieAccessResult access_result;
  if (!c.ReadCookie(&cookie))
    return false;
  if (!c.ReadCookieString(&cookie_string))
    return false;
  if (!c.ReadAccessResult(&access_result))
    return false;

  *out = {cookie, cookie_string, access_result};

  return true;
}

bool StructTraits<
    network::mojom::CookieAccessResultDataView,
    net::CookieAccessResult>::Read(network::mojom::CookieAccessResultDataView c,
                                   net::CookieAccessResult* out) {
  net::CookieEffectiveSameSite effective_same_site;
  net::CookieInclusionStatus status;
  net::CookieAccessSemantics access_semantics;

  if (!c.ReadEffectiveSameSite(&effective_same_site))
    return false;
  if (!c.ReadStatus(&status))
    return false;
  if (!c.ReadAccessSemantics(&access_semantics))
    return false;

  *out = {effective_same_site, status, access_semantics,
          c.is_allowed_to_access_secure_cookies()};

  return true;
}

bool StructTraits<network::mojom::CookieWithAccessResultDataView,
                  net::CookieWithAccessResult>::
    Read(network::mojom::CookieWithAccessResultDataView c,
         net::CookieWithAccessResult* out) {
  net::CanonicalCookie cookie;
  net::CookieAccessResult access_result;
  if (!c.ReadCookie(&cookie))
    return false;
  if (!c.ReadAccessResult(&access_result))
    return false;

  *out = {cookie, access_result};

  return true;
}

bool StructTraits<
    network::mojom::CookieChangeInfoDataView,
    net::CookieChangeInfo>::Read(network::mojom::CookieChangeInfoDataView info,
                                 net::CookieChangeInfo* out) {
  net::CanonicalCookie cookie;
  net::CookieAccessResult access_result;
  net::CookieChangeCause cause = net::CookieChangeCause::EXPLICIT;
  if (!info.ReadCookie(&cookie))
    return false;
  if (!info.ReadAccessResult(&access_result))
    return false;
  if (!info.ReadCause(&cause))
    return false;

  *out = net::CookieChangeInfo(cookie, access_result, cause);
  return true;
}

bool StructTraits<network::mojom::SamePartyContextDataView,
                  net::SamePartyContext>::
    Read(network::mojom::SamePartyContextDataView context,
         net::SamePartyContext* out) {
  net::SamePartyContext::Type context_type;
  if (!context.ReadContextType(&context_type))
    return false;

  net::SamePartyContext::Type ancestors_for_metrics_only;
  if (!context.ReadAncestorsForMetricsOnly(&ancestors_for_metrics_only))
    return false;

  net::SamePartyContext::Type top_resource_for_metrics_only;
  if (!context.ReadTopResourceForMetricsOnly(&top_resource_for_metrics_only))
    return false;

  *out = net::SamePartyContext(context_type, ancestors_for_metrics_only,
                               top_resource_for_metrics_only);
  return true;
}

}  // namespace mojo