File: nearby_share_mojom_traits.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (100 lines) | stat: -rw-r--r-- 3,491 bytes parent folder | download | duplicates (10)
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
// 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/ui/webui/nearby_share/nearby_share_mojom_traits.h"

#include "base/notreached.h"
#include "chrome/browser/nearby_sharing/attachment.h"
#include "chrome/browser/nearby_sharing/file_attachment.h"
#include "chrome/browser/nearby_sharing/text_attachment.h"
#include "mojo/public/cpp/bindings/optional_as_pointer.h"

namespace mojo {

// static
const base::UnguessableToken&
StructTraits<nearby_share::mojom::ShareTargetDataView, ShareTarget>::id(
    const ShareTarget& share_target) {
  return share_target.id;
}

// static
const std::string&
StructTraits<nearby_share::mojom::ShareTargetDataView, ShareTarget>::name(
    const ShareTarget& share_target) {
  return share_target.device_name;
}

// static
nearby_share::mojom::ShareTargetType
StructTraits<nearby_share::mojom::ShareTargetDataView, ShareTarget>::type(
    const ShareTarget& share_target) {
  return share_target.type;
}

// static
mojo::OptionalAsPointer<const GURL>
StructTraits<nearby_share::mojom::ShareTargetDataView, ShareTarget>::image_url(
    const ShareTarget& share_target) {
  return mojo::OptionalAsPointer(share_target.image_url &&
                                         share_target.image_url->is_valid()
                                     ? &share_target.image_url.value()
                                     : nullptr);
}

// static
nearby_share::mojom::PayloadPreviewPtr
StructTraits<nearby_share::mojom::ShareTargetDataView,
             ShareTarget>::payload_preview(const ShareTarget& share_target) {
  // TODO(crbug.com/1158627): Extract this which is very similar to logic in
  // NearbyPerSessionDiscoveryManager.
  nearby_share::mojom::PayloadPreviewPtr payload_preview =
      nearby_share::mojom::PayloadPreview::New();
  payload_preview->file_count = share_target.file_attachments.size();
  payload_preview->share_type = nearby_share::mojom::ShareType::kText;

  // Retrieve the attachment that we'll use for the default description.
  const Attachment* attachment = nullptr;
  if (!share_target.file_attachments.empty()) {
    attachment = &share_target.file_attachments[0];
  } else if (!share_target.text_attachments.empty()) {
    attachment = &share_target.text_attachments[0];
  } else if (!share_target.wifi_credentials_attachments.empty()) {
    attachment = &share_target.wifi_credentials_attachments[0];
  }

  // If there are no attachments, return an empty text preview.
  if (!attachment) {
    return payload_preview;
  }

  payload_preview->description = attachment->GetDescription();

  // Determine the share type.
  if (payload_preview->file_count > 1) {
    payload_preview->share_type =
        nearby_share::mojom::ShareType::kMultipleFiles;
  } else {
    payload_preview->share_type = attachment->GetShareType();
  }

  return payload_preview;
}

// static
bool mojo::StructTraits<nearby_share::mojom::ShareTargetDataView,
                        ShareTarget>::for_self_share(const ShareTarget&
                                                         share_target) {
  return share_target.for_self_share;
}

// static
bool StructTraits<nearby_share::mojom::ShareTargetDataView, ShareTarget>::Read(
    nearby_share::mojom::ShareTargetDataView data,
    ShareTarget* out) {
  return data.ReadId(&out->id) && data.ReadName(&out->device_name) &&
         data.ReadType(&out->type);
}

}  // namespace mojo