• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/client_wrapper/include/flutter/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 kUnknownClipboardFormatError[] =
21     "Unknown clipboard format error";
22 
23 namespace flutter {
24 
PlatformHandler(flutter::BinaryMessenger * messenger,GLFWwindow * window)25 PlatformHandler::PlatformHandler(flutter::BinaryMessenger* messenger,
26                                  GLFWwindow* window)
27     : channel_(std::make_unique<flutter::MethodChannel<rapidjson::Document>>(
28           messenger,
29           kChannelName,
30           &flutter::JsonMethodCodec::GetInstance())),
31       window_(window) {
32   channel_->SetMethodCallHandler(
33       [this](
34           const flutter::MethodCall<rapidjson::Document>& call,
35           std::unique_ptr<flutter::MethodResult<rapidjson::Document>> result) {
36         HandleMethodCall(call, std::move(result));
37       });
38 }
39 
HandleMethodCall(const flutter::MethodCall<rapidjson::Document> & method_call,std::unique_ptr<flutter::MethodResult<rapidjson::Document>> result)40 void PlatformHandler::HandleMethodCall(
41     const flutter::MethodCall<rapidjson::Document>& method_call,
42     std::unique_ptr<flutter::MethodResult<rapidjson::Document>> result) {
43   const std::string& method = method_call.method_name();
44 
45   if (method.compare(kGetClipboardDataMethod) == 0) {
46     // Only one string argument is expected.
47     const rapidjson::Value& format = method_call.arguments()[0];
48 
49     if (strcmp(format.GetString(), kTextPlainFormat) != 0) {
50       result->Error(kUnknownClipboardFormatError,
51                     "GLFW clipboard API only supports text.");
52       return;
53     }
54 
55     const char* clipboardData = glfwGetClipboardString(window_);
56     if (clipboardData == nullptr) {
57       result->Error(kUnknownClipboardFormatError,
58                     "Failed to retrieve clipboard data from GLFW api.");
59       return;
60     }
61     rapidjson::Document document;
62     document.SetObject();
63     rapidjson::Document::AllocatorType& allocator = document.GetAllocator();
64     document.AddMember(rapidjson::Value(kTextKey, allocator),
65                        rapidjson::Value(clipboardData, allocator), allocator);
66     result->Success(&document);
67   } else if (method.compare(kSetClipboardDataMethod) == 0) {
68     const rapidjson::Value& document = *method_call.arguments();
69     rapidjson::Value::ConstMemberIterator itr = document.FindMember(kTextKey);
70     if (itr == document.MemberEnd()) {
71       result->Error(kUnknownClipboardFormatError,
72                     "Missing text to store on clipboard.");
73       return;
74     }
75     glfwSetClipboardString(window_, itr->value.GetString());
76     result->Success();
77   } else if (method.compare(kSystemNavigatorPopMethod) == 0) {
78     exit(EXIT_SUCCESS);
79     result->Success();
80   } else {
81     result->NotImplemented();
82   }
83 }
84 }  // namespace flutter
85