Skip to content

Detect whether the Debug Extension was built for dev or release #1800

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

Merged
merged 7 commits into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions dwds/debug_extension_mv3/tool/update_dev_files.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ void main() async {
await _updateManifestJson();
}

/// Adds the Googler extension key.
/// Adds the Googler extension key, and prefixes the extension name with "DEV".
Future<void> _updateManifestJson() async {
final manifestJson = File('compiled/manifest.json');
final extensionKeyTxt = File('extension_key.txt');
Expand All @@ -19,7 +19,11 @@ Future<void> _updateManifestJson() async {
return _transformDevFile(manifestJson, (line) {
if (_matchesKey(line: line, key: 'name')) {
return [
line,
_newKeyValue(
oldLine: line,
newKey: 'name',
newValue: '[DEV] MV3 Dart Debug Extension',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! That is really helpful to distinguish them from each other!

),
if (extensionKey != null)
_newKeyValue(
oldLine: line,
Expand Down
7 changes: 5 additions & 2 deletions dwds/debug_extension_mv3/web/background.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import 'lifeline_ports.dart';
import 'logger.dart';
import 'messaging.dart';
import 'storage.dart';
import 'utils.dart';
import 'web_api.dart';

const _authSuccessResponse = 'Dart Debug Authentication Success!';
Expand Down Expand Up @@ -69,7 +70,8 @@ Future<bool> _authenticateUser(String extensionUrl, int tabId) async {
final response = await fetchRequest(authUrl);
final responseBody = response.body ?? '';
if (!responseBody.contains(_authSuccessResponse)) {
debugWarn('Not authenticated: ${response.status} / $responseBody');
debugError('Not authenticated: ${response.status} / $responseBody',
verbose: true);
_showWarningNotification('Please re-authenticate and try again.');
await _createTab(authUrl, inNewWindow: false);
return false;
Expand Down Expand Up @@ -128,7 +130,8 @@ void _setDebuggableIcon() {
}

void _setDefaultIcon() {
chrome.action.setIcon(IconInfo(path: 'dart_grey.png'), /*callback*/ null);
final iconPath = isDevMode() ? 'dart_dev.png' : 'dart_grey.png';
chrome.action.setIcon(IconInfo(path: iconPath), /*callback*/ null);
}

Future<DebugInfo?> _fetchDebugInfo(int tabId) {
Expand Down
2 changes: 2 additions & 0 deletions dwds/debug_extension_mv3/web/chrome_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ class Runtime {
external void sendMessage(
String? id, Object? message, Object? options, Function? callback);

external Object getManifest();

external ConnectionHandler get onConnect;

external OnMessageHandler get onMessage;
Expand Down
2 changes: 1 addition & 1 deletion dwds/debug_extension_mv3/web/detector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void _registerListeners() {
void _onDartAppReadyEvent(Event event) {
final debugInfo = getProperty(event, 'detail') as String?;
if (debugInfo == null) {
debugWarn('Can\'t debug Dart app without debug info.');
debugError('Can\'t debug Dart app without debug info.', verbose: true);
return;
}
_sendMessageToBackgroundScript(
Expand Down
60 changes: 45 additions & 15 deletions dwds/debug_extension_mv3/web/logger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,71 @@ library logger;

import 'package:js/js.dart';

import 'web_api.dart';

// Switch to true for debug logging.
bool _enableDebugLogging = true;
import 'utils.dart';

enum _LogLevel {
info,
warn,
error,
}

debugLog(String msg, {String? prefix}) {
_log(msg, prefix: prefix);
debugLog(
String msg, {
String? prefix,
bool verbose = false,
}) {
_log(msg, prefix: prefix, verbose: verbose);
}

debugWarn(String msg, {String? prefix}) {
_log(msg, prefix: prefix, level: _LogLevel.warn);
debugWarn(
String msg, {
String? prefix,
bool verbose = false,
}) {
_log(msg, prefix: prefix, level: _LogLevel.warn, verbose: verbose);
}

debugError(String msg, {String? prefix}) {
_log(msg, prefix: prefix, level: _LogLevel.error);
debugError(
String msg, {
String? prefix,
bool verbose = false,
}) {
_log(msg, prefix: prefix, level: _LogLevel.error, verbose: verbose);
}

void _log(String msg, {_LogLevel? level, String? prefix}) {
if (!_enableDebugLogging) return;
void _log(
String msg, {
bool verbose = false,
_LogLevel? level,
String? prefix,
}) {
if (!verbose && !isDevMode()) return;
final logMsg = prefix != null ? '[$prefix] $msg' : msg;
final logLevel = level ?? _LogLevel.info;
switch (logLevel) {
case _LogLevel.error:
console.error(logMsg);
_console.error(logMsg);
break;
case _LogLevel.warn:
console.warn(logMsg);
_console.warn(logMsg);
break;
case _LogLevel.info:
console.log(logMsg);
_console.log(logMsg);
}
}

@JS('console')
external _Console get _console;

@JS()
@anonymous
class _Console {
external void log(String header,
[String style1, String style2, String style3]);

external void warn(String header,
[String style1, String style2, String style3]);

external void error(String header,
[String style1, String style2, String style3]);
}
2 changes: 1 addition & 1 deletion dwds/debug_extension_mv3/web/manifest.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "[MV3] Dart Debug Extension",
"name": "MV3 Dart Debug Extension",
"version": "1.0",
"manifest_version": 3,
"action": {
Expand Down
23 changes: 23 additions & 0 deletions dwds/debug_extension_mv3/web/utils.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

@JS()
library utils;

import 'dart:js_util';

import 'package:js/js.dart';

import 'chrome_api.dart';

bool? _isDevMode;

bool isDevMode() {
if (_isDevMode != null) {
return _isDevMode!;
}
final extensionManifest = chrome.runtime.getManifest();
final extensionName = getProperty(extensionManifest, 'name') ?? '';
return extensionName.contains('DEV');
}