forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix destruction order in C++ plugin registrar (flutter#21840)
The C++ wrapper's plugin registrar can own plugins to provided lifetime management. However, plugins expect the registrar to be valid for the life of the object, including during destruction, so any owned plugins must be explicitly cleared before any registrar-specific destruction happens.
- Loading branch information
1 parent
dc848f1
commit 57d3c6d
Showing
9 changed files
with
162 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
shell/platform/glfw/client_wrapper/plugin_registrar_glfw_unittests.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright 2013 The Flutter 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 <memory> | ||
#include <string> | ||
|
||
#include "flutter/shell/platform/glfw/client_wrapper/include/flutter/plugin_registrar_glfw.h" | ||
#include "flutter/shell/platform/glfw/client_wrapper/testing/stub_flutter_glfw_api.h" | ||
#include "gtest/gtest.h" | ||
|
||
namespace flutter { | ||
|
||
namespace { | ||
|
||
// A test plugin that tries to access registrar state during destruction and | ||
// reports it out via a flag provided at construction. | ||
class TestPlugin : public Plugin { | ||
public: | ||
// registrar_valid_at_destruction will be set at destruction to indicate | ||
// whether or not |registrar->window()| was non-null. | ||
TestPlugin(PluginRegistrarGlfw* registrar, | ||
bool* registrar_valid_at_destruction) | ||
: registrar_(registrar), | ||
registrar_valid_at_destruction_(registrar_valid_at_destruction) {} | ||
virtual ~TestPlugin() { | ||
*registrar_valid_at_destruction_ = registrar_->window() != nullptr; | ||
} | ||
|
||
private: | ||
PluginRegistrarGlfw* registrar_; | ||
bool* registrar_valid_at_destruction_; | ||
}; | ||
|
||
} // namespace | ||
|
||
TEST(PluginRegistrarGlfwTest, GetView) { | ||
testing::ScopedStubFlutterGlfwApi scoped_api_stub( | ||
std::make_unique<testing::StubFlutterGlfwApi>()); | ||
PluginRegistrarGlfw registrar( | ||
reinterpret_cast<FlutterDesktopPluginRegistrarRef>(1)); | ||
EXPECT_NE(registrar.window(), nullptr); | ||
} | ||
|
||
// Tests that the registrar runs plugin destructors before its own teardown. | ||
TEST(PluginRegistrarGlfwTest, PluginDestroyedBeforeRegistrar) { | ||
auto dummy_registrar_handle = | ||
reinterpret_cast<FlutterDesktopPluginRegistrarRef>(1); | ||
bool registrar_valid_at_destruction = false; | ||
{ | ||
PluginRegistrarGlfw registrar(dummy_registrar_handle); | ||
|
||
auto plugin = std::make_unique<TestPlugin>(®istrar, | ||
®istrar_valid_at_destruction); | ||
registrar.AddPlugin(std::move(plugin)); | ||
} | ||
EXPECT_TRUE(registrar_valid_at_destruction); | ||
} | ||
|
||
} // namespace flutter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters