• 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/darwin/ios/framework/Source/platform_message_router.h"
6
7#include <vector>
8
9#include "flutter/shell/platform/darwin/common/buffer_conversions.h"
10
11namespace flutter {
12
13PlatformMessageRouter::PlatformMessageRouter() = default;
14
15PlatformMessageRouter::~PlatformMessageRouter() = default;
16
17void PlatformMessageRouter::HandlePlatformMessage(
18    fml::RefPtr<flutter::PlatformMessage> message) const {
19  fml::RefPtr<flutter::PlatformMessageResponse> completer = message->response();
20  auto it = message_handlers_.find(message->channel());
21  if (it != message_handlers_.end()) {
22    FlutterBinaryMessageHandler handler = it->second;
23    NSData* data = nil;
24    if (message->hasData()) {
25      data = GetNSDataFromVector(message->data());
26    }
27    handler(data, ^(NSData* reply) {
28      if (completer) {
29        if (reply) {
30          completer->Complete(GetMappingFromNSData(reply));
31        } else {
32          completer->CompleteEmpty();
33        }
34      }
35    });
36  } else {
37    if (completer) {
38      completer->CompleteEmpty();
39    }
40  }
41}
42
43void PlatformMessageRouter::SetMessageHandler(const std::string& channel,
44                                              FlutterBinaryMessageHandler handler) {
45  message_handlers_.erase(channel);
46  if (handler) {
47    message_handlers_[channel] =
48        fml::ScopedBlock<FlutterBinaryMessageHandler>{handler, fml::OwnershipPolicy::Retain};
49  }
50}
51
52}  // namespace flutter
53