Flutter Engine
platform_handler.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter 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 "flutter/shell/platform/glfw/platform_handler.h"
6 
7 #include <iostream>
8 
9 #include "flutter/shell/platform/common/cpp/json_method_codec.h"
10 
11 static constexpr char kChannelName[] = "flutter/platform";
12 
13 static constexpr char kGetClipboardDataMethod[] = "Clipboard.getData";
14 static constexpr char kSetClipboardDataMethod[] = "Clipboard.setData";
15 static constexpr char kSystemNavigatorPopMethod[] = "SystemNavigator.pop";
16 
17 static constexpr char kTextPlainFormat[] = "text/plain";
18 static constexpr char kTextKey[] = "text";
19 
20 static constexpr char kNoWindowError[] = "Missing window error";
21 static constexpr char kUnknownClipboardFormatError[] =
22  "Unknown clipboard format error";
23 
24 namespace flutter {
25 
27  GLFWwindow* window)
28  : channel_(std::make_unique<flutter::MethodChannel<rapidjson::Document>>(
29  messenger,
31  &flutter::JsonMethodCodec::GetInstance())),
32  window_(window) {
33  channel_->SetMethodCallHandler(
34  [this](
36  std::unique_ptr<flutter::MethodResult<rapidjson::Document>> result) {
37  HandleMethodCall(call, std::move(result));
38  });
39 }
40 
41 void PlatformHandler::HandleMethodCall(
43  std::unique_ptr<flutter::MethodResult<rapidjson::Document>> result) {
44  const std::string& method = method_call.method_name();
45 
46  if (method.compare(kGetClipboardDataMethod) == 0) {
47  if (!window_) {
48  result->Error(kNoWindowError,
49  "Clipboard is not available in GLFW headless mode.");
50  return;
51  }
52  // Only one string argument is expected.
53  const rapidjson::Value& format = method_call.arguments()[0];
54 
55  if (strcmp(format.GetString(), kTextPlainFormat) != 0) {
56  result->Error(kUnknownClipboardFormatError,
57  "GLFW clipboard API only supports text.");
58  return;
59  }
60 
61  const char* clipboardData = glfwGetClipboardString(window_);
62  if (clipboardData == nullptr) {
63  result->Error(kUnknownClipboardFormatError,
64  "Failed to retrieve clipboard data from GLFW api.");
65  return;
66  }
67  rapidjson::Document document;
68  document.SetObject();
69  rapidjson::Document::AllocatorType& allocator = document.GetAllocator();
70  document.AddMember(rapidjson::Value(kTextKey, allocator),
71  rapidjson::Value(clipboardData, allocator), allocator);
72  result->Success(document);
73  } else if (method.compare(kSetClipboardDataMethod) == 0) {
74  if (!window_) {
75  result->Error(kNoWindowError,
76  "Clipboard is not available in GLFW headless mode.");
77  return;
78  }
79  const rapidjson::Value& document = *method_call.arguments();
80  rapidjson::Value::ConstMemberIterator itr = document.FindMember(kTextKey);
81  if (itr == document.MemberEnd()) {
82  result->Error(kUnknownClipboardFormatError,
83  "Missing text to store on clipboard.");
84  return;
85  }
86  glfwSetClipboardString(window_, itr->value.GetString());
87  result->Success();
88  } else if (method.compare(kSystemNavigatorPopMethod) == 0) {
89  exit(EXIT_SUCCESS);
90  result->Success();
91  } else {
92  result->NotImplemented();
93  }
94 }
95 } // namespace flutter
static constexpr char kUnknownClipboardFormatError[]
G_BEGIN_DECLS FlMethodCall * method_call
static constexpr char kSystemNavigatorPopMethod[]
static constexpr char kNoWindowError[]
const T * arguments() const
Definition: method_call.h:34
PlatformHandler(flutter::BinaryMessenger *messenger, GLFWwindow *window)
Definition: ref_ptr.h:252
static constexpr char kGetClipboardDataMethod[]
static constexpr char kChannelName[]
static constexpr char kSetClipboardDataMethod[]
static constexpr char kTextKey[]
const std::string & method_name() const
Definition: method_call.h:31
static constexpr char kTextPlainFormat[]