-
Notifications
You must be signed in to change notification settings - Fork 336
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add DevToolsExtension
template to devtools_extension
package
#6066
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b1bd1fe
Add `DevToolsExtension` template to `devtools_extension` package
kenzieschmoll 43978ce
dart doc and renames
kenzieschmoll 77a4926
formatting
kenzieschmoll 6bdae1c
cleanup
kenzieschmoll a7319ca
fix test and rename
kenzieschmoll 7904576
rename
kenzieschmoll b89f2e2
review comments
kenzieschmoll 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 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
The code in this directory is shared code that is intended to be used by | ||
both DevTools itself and DevTools extensions. Files in this directory are | ||
exported through the `lib/api.dart` file. |
File renamed without changes.
File renamed without changes.
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,7 @@ | ||
The code in this directory is for the DevTools extension template that package | ||
authors will use to build DevTools extensions. Files in this directory are | ||
exported through the `lib/template.dart` file. | ||
|
||
This code is not intended to be imported into DevTools itself. Anything that | ||
should be shared between DevTools and DevTools extensions will be under the | ||
`src/api` directory and exported through `lib/api.dart`. |
64 changes: 64 additions & 0 deletions
64
packages/devtools_extensions/lib/src/template/devtools_extension.dart
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,64 @@ | ||
// Copyright 2023 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. | ||
|
||
// ignore: avoid_web_libraries_in_flutter, as designed | ||
import 'dart:html' as html; | ||
|
||
import 'package:flutter/widgets.dart'; | ||
import 'package:logging/logging.dart'; | ||
|
||
import '../../api.dart'; | ||
|
||
part 'extension_manager.dart'; | ||
|
||
/// A manager that allows extensions to interact with DevTools or the DevTools | ||
/// extensions framework. | ||
/// | ||
/// A couple use case examples include posting messages to DevTools or | ||
kenzieschmoll marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// registering an event handler from the extension. | ||
ExtensionManager get extensionManager => _extensionManager; | ||
late final ExtensionManager _extensionManager; | ||
|
||
/// A wrapper widget that initializes the [extensionManager] and establishes a | ||
/// connection with DevTools for this extension to interact over. | ||
class DevToolsExtension extends StatefulWidget { | ||
const DevToolsExtension({ | ||
super.key, | ||
required this.child, | ||
this.eventHandlers = const {}, | ||
}); | ||
|
||
/// The root of the extension Flutter web app that is wrapped by this | ||
/// [DevToolsExtension] wrapper. | ||
final Widget child; | ||
|
||
/// Event handlers registered by the extension so that it can respond to | ||
/// DevTools events. | ||
final Map<DevToolsExtensionEventType, ExtensionEventHandler> eventHandlers; | ||
|
||
@override | ||
State<DevToolsExtension> createState() => _DevToolsExtensionState(); | ||
} | ||
|
||
class _DevToolsExtensionState extends State<DevToolsExtension> { | ||
@override | ||
void initState() { | ||
super.initState(); | ||
_extensionManager = ExtensionManager().._init(); | ||
for (final handler in widget.eventHandlers.entries) { | ||
_extensionManager.registerEventHandler(handler.key, handler.value); | ||
} | ||
} | ||
|
||
@override | ||
void dispose() { | ||
_extensionManager._dispose(); | ||
super.dispose(); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return widget.child; | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
packages/devtools_extensions/lib/src/template/extension_manager.dart
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,59 @@ | ||
// Copyright 2023 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 'devtools_extension.dart'; | ||
|
||
final _log = Logger('devtools_extensions/extension_manager'); | ||
|
||
class ExtensionManager { | ||
final _registeredEventHandlers = | ||
<DevToolsExtensionEventType, ExtensionEventHandler>{}; | ||
|
||
void registerEventHandler( | ||
DevToolsExtensionEventType event, | ||
ExtensionEventHandler handler, | ||
) { | ||
_registeredEventHandlers[event] = handler; | ||
} | ||
|
||
// ignore: unused_element, false positive due to part files | ||
void _init() { | ||
html.window.addEventListener('message', _handleMessage); | ||
} | ||
|
||
// ignore: unused_element, false positive due to part files | ||
void _dispose() { | ||
_registeredEventHandlers.clear(); | ||
html.window.removeEventListener('message', _handleMessage); | ||
} | ||
|
||
void _handleMessage(html.Event e) { | ||
if (e is html.MessageEvent) { | ||
final extensionEvent = DevToolsExtensionEvent.tryParse(e.data); | ||
if (extensionEvent != null) { | ||
switch (extensionEvent.type) { | ||
case DevToolsExtensionEventType.ping: | ||
html.window.parent?.postMessage( | ||
DevToolsExtensionEvent.pong.toJson(), | ||
e.origin, | ||
); | ||
break; | ||
case DevToolsExtensionEventType.pong: | ||
// Ignore. DevTools extensions should not receive or handle pong | ||
// events. | ||
break; | ||
case DevToolsExtensionEventType.unknown: | ||
_log.info('Unrecognized event received by extension: ${e.data}'); | ||
kenzieschmoll marked this conversation as resolved.
Show resolved
Hide resolved
|
||
break; | ||
default: | ||
} | ||
_registeredEventHandlers[extensionEvent.type]?.call(extensionEvent); | ||
} | ||
} | ||
} | ||
|
||
void postMessageToDevTools(DevToolsExtensionEvent event) { | ||
html.window.parent?.postMessage(event.toJson(), html.window.origin!); | ||
} | ||
} |
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,7 @@ | ||
// Copyright 2023 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. | ||
|
||
library template; | ||
|
||
export 'src/template/devtools_extension.dart'; |
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
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.
How about moving template out of
lib
toexample
and make it separate package with its own pubspec.yaml?You will have to create
example
anyway, because it will be requested by pub.get publishing code.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.
the template isn't an example - it is actually the file an extension author would import to write a devtools extension.
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.
renamed to
devtools_extensions.dart
to make that more clearThere 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.
added an
example
dir in #6069