summaryrefslogtreecommitdiff
path: root/chromium/components/mus/clipboard/clipboard_impl.cc
blob: a82d95e8f031625a5e53eed76e63718becc23ba8 (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
// Copyright 2016 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 "components/mus/clipboard/clipboard_impl.h"

#include <string.h>
#include <utility>

#include "base/macros.h"
#include "mojo/common/common_type_converters.h"
#include "mojo/public/cpp/bindings/array.h"
#include "mojo/public/cpp/bindings/string.h"

using mojo::Array;
using mojo::Map;
using mojo::String;

namespace mus {
namespace clipboard {

// ClipboardData contains data copied to the Clipboard for a variety of formats.
// It mostly just provides APIs to cleanly access and manipulate this data.
class ClipboardImpl::ClipboardData {
 public:
  ClipboardData() : sequence_number_(0) {}
  ~ClipboardData() {}

  uint64_t sequence_number() const {
    return sequence_number_;
  }

  Array<String> GetMimeTypes() const {
    Array<String> types(data_types_.size());
    int i = 0;
    for (auto it = data_types_.begin(); it != data_types_.end(); ++it, ++i)
      types[i] = it->first;

    return types;
  }

  void SetData(Map<String, Array<uint8_t>> data) {
    sequence_number_++;
    data_types_ = std::move(data);
  }

  void GetData(const String& mime_type, Array<uint8_t>* data) const {
    auto it = data_types_.find(mime_type);
    if (it != data_types_.end())
      *data = it->second.Clone();
  }

 private:
  uint64_t sequence_number_;
  Map<String, Array<uint8_t>> data_types_;

  DISALLOW_COPY_AND_ASSIGN(ClipboardData);
};

ClipboardImpl::ClipboardImpl() {
  for (int i = 0; i < kNumClipboards; ++i)
    clipboard_state_[i].reset(new ClipboardData);
}

ClipboardImpl::~ClipboardImpl() {
}

void ClipboardImpl::AddBinding(mojom::ClipboardRequest request) {
  bindings_.AddBinding(this, std::move(request));
}

void ClipboardImpl::GetSequenceNumber(
    Clipboard::Type clipboard_type,
    const GetSequenceNumberCallback& callback) {
  callback.Run(
      clipboard_state_[static_cast<int>(clipboard_type)]->sequence_number());
}

void ClipboardImpl::GetAvailableMimeTypes(
    Clipboard::Type clipboard_type,
    const GetAvailableMimeTypesCallback& callback) {
  int clipboard_num = static_cast<int>(clipboard_type);
  callback.Run(clipboard_state_[clipboard_num]->sequence_number(),
               clipboard_state_[clipboard_num]->GetMimeTypes());
}

void ClipboardImpl::ReadClipboardData(
    Clipboard::Type clipboard_type,
    const String& mime_type,
    const ReadClipboardDataCallback& callback) {
  int clipboard_num = static_cast<int>(clipboard_type);
  Array<uint8_t> mime_data(nullptr);
  uint64_t sequence = clipboard_state_[clipboard_num]->sequence_number();
  clipboard_state_[clipboard_num]->GetData(mime_type, &mime_data);
  callback.Run(sequence, std::move(mime_data));
}

void ClipboardImpl::WriteClipboardData(
    Clipboard::Type clipboard_type,
    Map<String, Array<uint8_t>> data,
    const WriteClipboardDataCallback& callback) {
  int clipboard_num = static_cast<int>(clipboard_type);
  clipboard_state_[clipboard_num]->SetData(std::move(data));
  callback.Run(clipboard_state_[clipboard_num]->sequence_number());
}

}  // namespace clipboard
}  // namespace mus