This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
Background Execution Implementation for iOS #5539
Merged
Merged
Changes from 14 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
4aa0ace
Background execution implementation for iOS
bkonyi 59f4c05
Updated licenses + removed extra line of code
bkonyi 2f3e0d8
Addressed some comments
bkonyi ddbd5f3
Added tests for PluginUtilities and updated function params.
bkonyi b15e35c
Updated documentation for getClosureByName
bkonyi 9806222
Updated docs for plugins.dart and changed ArgumentErrors to asserts
bkonyi 3341e83
Removed unused method and s/`null`/null/g
bkonyi 8373f88
Updated callback lookup/passing to use handles instead of human readable
bkonyi f4d23c2
Added CallbackHandle wrapper for handles returned from
bkonyi 87d3d80
Updated licenses
bkonyi 14e7aca
Fixed analyzer complaints
bkonyi af25c66
Merge branch 'master' into headless_plugin
bkonyi 2ff7690
Update licenses_flutter
bkonyi 48261fd
Update licenses_flutter
bkonyi abcabee
Updated docs for plugins.dart
bkonyi 2ec1ef4
Added caching in PluginUtilities
bkonyi 8cc4394
Fix analyzer issues
bkonyi 81492d6
Addressed comments
bkonyi 514df89
Update analysis_options.yaml
bkonyi c6a955d
Added private factory to make analyzer happy
bkonyi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,58 @@ | ||
| // Copyright 2018 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. | ||
|
|
||
| part of dart.ui; | ||
|
|
||
| /// An wrapper for a raw callback handle. | ||
| class CallbackHandle { | ||
| final int _handle; | ||
|
|
||
| /// Create an instance using a raw callback handle. | ||
| /// | ||
| /// Only values produced by a call to [CallbackHandle.toRawHandle] should be | ||
| /// used, otherwise this object will be an invalid handle. | ||
| CallbackHandle.fromRawHandle(this._handle) | ||
| : assert(_handle != null, "'_handle' must not be null."); | ||
|
|
||
| /// Get the raw callback handle to pass over a [MethodChannel] or isolate | ||
| /// port. | ||
| int toRawHandle() => _handle; | ||
|
|
||
| @override | ||
| int get hashCode => _handle; | ||
|
|
||
| @override | ||
| bool operator ==(dynamic other) => | ||
| (other is CallbackHandle) && (_handle == other._handle); | ||
| } | ||
|
|
||
| /// Functionality for Flutter plugin authors. | ||
| abstract class PluginUtilities { | ||
| /// Get a handle to a named top-level or static callback function which can | ||
| /// be easily passed between isolates. | ||
| /// | ||
| /// `callback` must not be null. | ||
| /// | ||
| /// Returns a [CallbackHandle] that can be provided to | ||
| /// [PluginUtilities.getCallbackFromHandle] to retrieve a tear-off of the | ||
| /// original callback. If `callback` is not a top-level or static function, | ||
| /// null is returned. | ||
| static CallbackHandle getCallbackHandle(Function callback) { | ||
| assert(callback != null, "'callback' must not be null."); | ||
| return new CallbackHandle.fromRawHandle(_getCallbackHandle(callback)); | ||
| } | ||
|
|
||
| /// Get a tear-off of a named top-level or static callback represented by a | ||
| /// handle. | ||
| /// | ||
| /// `handle` must not be null. | ||
| /// | ||
| /// If `handle` is not a valid handle returned by | ||
| /// [PluginUtilities.getCallbackHandle], null is returned. Otherwise, a | ||
| /// tear-off of the callback associated with `handle` is returned. | ||
| static Function getCallbackFromHandle(CallbackHandle handle) { | ||
| assert(handle != null, "'handle' must not be null."); | ||
| return _getCallbackFromHandle(handle.toRawHandle()); | ||
| } | ||
| } |
This file contains hidden or 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,87 @@ | ||
| // Copyright 2018 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 "flutter/lib/ui/plugins/callback_cache.h" | ||
| #include "lib/fxl/logging.h" | ||
| #include "lib/tonic/converter/dart_converter.h" | ||
|
|
||
| using tonic::ToDart; | ||
|
|
||
| namespace blink { | ||
|
|
||
| std::mutex DartCallbackCache::mutex_; | ||
| std::map<int64_t, DartCallbackRepresentation> DartCallbackCache::cache_; | ||
|
|
||
| Dart_Handle DartCallbackCache::GetCallback(int64_t handle) { | ||
| std::unique_lock<std::mutex> lock(mutex_); | ||
| auto iterator = cache_.find(handle); | ||
| if (iterator != cache_.end()) { | ||
| DartCallbackRepresentation cb = iterator->second; | ||
| return LookupDartClosure(cb.name, cb.class_name, cb.library_path); | ||
| } | ||
| return Dart_Null(); | ||
| } | ||
|
|
||
| int64_t DartCallbackCache::GetCallbackHandle(const std::string& name, | ||
| const std::string& class_name, | ||
| const std::string& library_path) { | ||
| std::unique_lock<std::mutex> lock(mutex_); | ||
| std::hash<std::string> hasher; | ||
| int64_t hash = hasher(name); | ||
| hash += hasher(class_name); | ||
| hash += hasher(library_path); | ||
|
|
||
| if (cache_.find(hash) == cache_.end()) { | ||
| cache_[hash] = {name, class_name, library_path}; | ||
| } | ||
| return hash; | ||
| } | ||
|
|
||
| DartCallbackRepresentation DartCallbackCache::GetCallbackInformation( | ||
| int64_t handle) { | ||
| std::unique_lock<std::mutex> lock(mutex_); | ||
| auto iterator = cache_.find(handle); | ||
| if (iterator != cache_.end()) { | ||
| return iterator->second; | ||
| } | ||
| return {"", "", ""}; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this returns something else to more clearly indicate the error? |
||
| } | ||
|
|
||
| Dart_Handle DartCallbackCache::LookupDartClosure( | ||
| const std::string& name, | ||
| const std::string& class_name, | ||
| const std::string& library_path) { | ||
| Dart_Handle closure_name = ToDart(name); | ||
| Dart_Handle library_name = | ||
| library_path.empty() ? Dart_Null() : ToDart(library_path); | ||
| Dart_Handle cls_name = class_name.empty() ? Dart_Null() : ToDart(class_name); | ||
| DART_CHECK_VALID(closure_name); | ||
| DART_CHECK_VALID(library_name); | ||
| DART_CHECK_VALID(cls_name); | ||
|
|
||
| Dart_Handle library; | ||
| if (library_name == Dart_Null()) { | ||
| library = Dart_RootLibrary(); | ||
| } else { | ||
| library = Dart_LookupLibrary(library_name); | ||
| } | ||
| DART_CHECK_VALID(library); | ||
|
|
||
| Dart_Handle closure; | ||
| if (Dart_IsNull(cls_name)) { | ||
| closure = Dart_GetClosure(library, closure_name); | ||
| } else { | ||
| Dart_Handle cls = Dart_GetClass(library, cls_name); | ||
| DART_CHECK_VALID(cls); | ||
| if (Dart_IsNull(cls)) { | ||
| closure = Dart_Null(); | ||
| } else { | ||
| closure = Dart_GetStaticMethodClosure(library, cls, closure_name); | ||
| } | ||
| } | ||
| DART_CHECK_VALID(closure); | ||
| return closure; | ||
| } | ||
|
|
||
| } // namespace blink | ||
This file contains hidden or 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,54 @@ | ||
| // Copyright 2018 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. | ||
|
|
||
| #ifndef FLUTTER_LIB_UI_CALLBACK_CACHE_H_ | ||
| #define FLUTTER_LIB_UI_CALLBACK_CACHE_H_ | ||
|
|
||
| #include <map> | ||
| #include <mutex> | ||
| #include <string> | ||
|
|
||
| #include "flutter/fml/synchronization/thread_annotations.h" | ||
| #include "lib/fxl/macros.h" | ||
| #include "third_party/dart/runtime/include/dart_api.h" | ||
|
|
||
| #define DART_CALLBACK_INVALID_HANDLE -1 | ||
| #define LOCK_UNLOCK(m) FML_ACQUIRE(m) FML_RELEASE(m) | ||
|
|
||
| namespace blink { | ||
|
|
||
| typedef struct { | ||
| std::string name; | ||
| std::string class_name; | ||
| std::string library_path; | ||
| } DartCallbackRepresentation; | ||
|
|
||
| class DartCallbackCache { | ||
| public: | ||
| static int64_t GetCallbackHandle(const std::string& name, | ||
| const std::string& class_name, | ||
| const std::string& library_path) | ||
| LOCK_UNLOCK(mutex_); | ||
|
|
||
| static Dart_Handle GetCallback(int64_t handle) LOCK_UNLOCK(mutex_); | ||
|
|
||
| static DartCallbackRepresentation GetCallbackInformation(int64_t handle) | ||
| LOCK_UNLOCK(mutex_); | ||
|
|
||
| private: | ||
| static Dart_Handle LookupDartClosure(const std::string& name, | ||
| const std::string& class_name, | ||
| const std::string& library_path); | ||
|
|
||
| static std::mutex mutex_; | ||
|
|
||
| static std::map<int64_t, DartCallbackRepresentation> cache_ | ||
| FML_GUARDED_BY(mutex_); | ||
|
|
||
| FXL_DISALLOW_IMPLICIT_CONSTRUCTORS(DartCallbackCache); | ||
| }; | ||
|
|
||
| } // namespace blink | ||
|
|
||
| #endif // FLUTTER_LIB_UI_CALLBACK_CACHE_H_ |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the incoming arg closure is not a closure, don't you need a check that it is a Function Object? further down you are calling Dart_FunctionOwner on it and this method will return an error if it is not a function object.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, good catch. I'll add a check.