Skip to content
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

Offline support for Network page #8332

Open
wants to merge 35 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
fe11d5b
added initial implementation
hrajwade96 Sep 21, 2024
6c140b4
adding constants file
hrajwade96 Sep 29, 2024
0c1f414
updated NEXT_RELEASE_NOTES.md
hrajwade96 Sep 30, 2024
e7eeecb
Revert "updated NEXT_RELEASE_NOTES.md"
hrajwade96 Sep 30, 2024
c5ec6f6
used constants, added util for header size calc, added params
hrajwade96 Oct 2, 2024
131cde6
Merge branch 'master' into network_screen_offline_support
hrajwade96 Oct 2, 2024
2a9c4cb
param renamed, used _log
hrajwade96 Oct 2, 2024
4f473be
Merge remote-tracking branch 'origin/network_screen_offline_support' …
hrajwade96 Oct 2, 2024
ae58738
added copyright header
hrajwade96 Oct 2, 2024
2bfbe1c
setup changes implemented
hrajwade96 Oct 3, 2024
857cfbe
put class name in square brackets
hrajwade96 Oct 3, 2024
7150436
added isNullOrEmpty check
hrajwade96 Oct 3, 2024
da11a67
added exit offline cta, hiding other controls, removed json string
hrajwade96 Oct 4, 2024
941ec88
fix for request selection
hrajwade96 Oct 4, 2024
852b144
comments resolved
hrajwade96 Oct 8, 2024
433af38
removing getFullRequestData section
hrajwade96 Oct 8, 2024
4644b00
using updateOrAddAll for setting data
hrajwade96 Oct 8, 2024
6fd98d0
Wrapped with OfflineAwareControls, removed if-else.
hrajwade96 Oct 8, 2024
665ea0d
removed addAutoDisposeListener as we are using OfflineAwareControls
hrajwade96 Oct 8, 2024
b6df572
removed try-catch
hrajwade96 Oct 8, 2024
be63db4
code refactoring
hrajwade96 Oct 9, 2024
91505b5
added enum
hrajwade96 Oct 9, 2024
0715379
code refactoring, reverting changes on network service
hrajwade96 Oct 10, 2024
ade8b5f
using values from enum
hrajwade96 Oct 10, 2024
f21a4f6
delegating toJson using extension methods, code refactoring
hrajwade96 Oct 23, 2024
454ca92
socket data de-serialisation changes
hrajwade96 Oct 27, 2024
b7b9679
Merge branch 'master' into network_screen_offline_support
hrajwade96 Oct 27, 2024
ac726e8
used factory constructor, made enum private
hrajwade96 Nov 4, 2024
2ea3044
reorder params, used isEmpty
hrajwade96 Nov 4, 2024
e01bc3c
added tests, minor fixes
hrajwade96 Nov 17, 2024
e7b5c90
removed repetitive expect statements
hrajwade96 Nov 21, 2024
6ab0884
Merge remote-tracking branch 'upstream/master' into network_screen_of…
hrajwade96 Nov 22, 2024
4f31b92
minor fix
hrajwade96 Nov 22, 2024
04c8b41
added timelineMicrosOffset to offline data
hrajwade96 Nov 24, 2024
ab8c519
added timelineMicrosOffset to tests
hrajwade96 Nov 24, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import 'dart:convert';

import '../../screens/network/utils/http_utils.dart';
import '../../shared/http/http_request_data.dart';
import '../../shared/primitives/utils.dart';
import 'constants.dart';
Expand Down Expand Up @@ -131,7 +132,7 @@ class HarDataEntry {
NetworkEventKeys.text.name: e.requestBody,
},
NetworkEventKeys.headersSize.name:
_calculateHeadersSize(e.requestHeaders),
calculateHeadersSize(e.requestHeaders),
NetworkEventKeys.bodySize.name: _calculateBodySize(e.requestBody),
},
// Response
Expand All @@ -150,7 +151,7 @@ class HarDataEntry {
},
NetworkEventKeys.redirectURL.name: '',
NetworkEventKeys.headersSize.name:
_calculateHeadersSize(e.responseHeaders),
calculateHeadersSize(e.responseHeaders),
NetworkEventKeys.bodySize.name: _calculateBodySize(e.responseBody),
},
// Cache
Expand Down Expand Up @@ -263,27 +264,6 @@ class HarDataEntry {
}
}

int _calculateHeadersSize(Map<String, Object?>? headers) {
if (headers == null) return -1;

// Combine headers into a single string with CRLF endings
String headersString = headers.entries.map((entry) {
final key = entry.key;
var value = entry.value;
// If the value is a List, join it with a comma
if (value is List<String>) {
value = value.join(', ');
}
return '$key: $value\r\n';
}).join();

// Add final CRLF to indicate end of headers
headersString += '\r\n';

// Calculate the byte length of the headers string
return utf8.encode(headersString).length;
}

int _calculateBodySize(String? requestBody) {
if (requestBody.isNullOrEmpty) {
return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import '../../shared/config_specific/logger/allowed_error.dart';
import '../../shared/globals.dart';
import '../../shared/http/http_request_data.dart';
import '../../shared/http/http_service.dart' as http_service;
import '../../shared/offline_data.dart';
import '../../shared/primitives/utils.dart';
import '../../shared/ui/filter.dart';
import '../../shared/ui/search.dart';
Expand Down Expand Up @@ -49,6 +50,7 @@ class NetworkController extends DisposableController
with
SearchControllerMixin<NetworkRequest>,
FilterControllerMixin<NetworkRequest>,
OfflineScreenControllerMixin,
AutoDisposeControllerMixin {
NetworkController() {
_networkService = NetworkService(this);
Expand Down Expand Up @@ -388,6 +390,25 @@ class NetworkController extends DisposableController
}
}

@override
OfflineScreenData prepareOfflineScreenData() {
final requests =
filteredData.value.whereType<DartIOHttpRequestData>().toList();
return OfflineScreenData(
screenId: NetworkScreen.id,
data: serializeRequestDataForOfflineMode(requests),
hrajwade96 marked this conversation as resolved.
Show resolved Hide resolved
);
}
}

Map<String, Object> serializeRequestDataForOfflineMode(
List<DartIOHttpRequestData>? requests,
) {
if (requests == null) return {};
return {
'requests': requests.map((request) => request.toJson()).toList(),
};

Future<void> _fetchFullDataBeforeExport() => Future.wait(
filteredData.value
.whereType<DartIOHttpRequestData>()
Expand Down
55 changes: 42 additions & 13 deletions packages/devtools_app/lib/src/screens/network/network_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:devtools_app_shared/ui.dart';
import 'package:devtools_app_shared/utils.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:logging/logging.dart';
import 'package:provider/provider.dart';

import '../../shared/analytics/analytics.dart' as ga;
Expand All @@ -29,6 +30,8 @@ import 'network_controller.dart';
import 'network_model.dart';
import 'network_request_inspector.dart';

final _log = Logger('http_request_data');

class NetworkScreen extends Screen {
NetworkScreen() : super.fromMetaData(ScreenMetaData.network);

Expand Down Expand Up @@ -123,19 +126,45 @@ class _NetworkScreenBodyState extends State<NetworkScreenBody>
void didChangeDependencies() {
super.didChangeDependencies();
if (!initController()) return;
kenzieschmoll marked this conversation as resolved.
Show resolved Hide resolved
unawaited(controller.startRecording());

cancelListeners();
try {
hrajwade96 marked this conversation as resolved.
Show resolved Hide resolved
if (offlineDataController.showingOfflineData.value == true) {
loadOfflineData(offlineDataController.offlineDataJson);
}
cancelListeners();
if (!offlineDataController.showingOfflineData.value) {
unawaited(controller.startRecording());
debugPrint('started recording');
addAutoDisposeListener(
serviceConnection.serviceManager.isolateManager.mainIsolate,
() {
if (serviceConnection
.serviceManager.isolateManager.mainIsolate.value !=
null) {
unawaited(controller.startRecording());
}
},
);
}
} catch (e) {
_log.shout('Could not load offline data: $e');
}
}

addAutoDisposeListener(
serviceConnection.serviceManager.isolateManager.mainIsolate,
() {
if (serviceConnection.serviceManager.isolateManager.mainIsolate.value !=
null) {
unawaited(controller.startRecording());
}
},
);
void loadOfflineData(Map<String, dynamic> offlineData) {
final requestsMap = (offlineData['network']
as Map<String, dynamic>)['requests'] as List<dynamic>;
final requests = requestsMap
.map(
(e) => DartIOHttpRequestData.fromJson(
e as Map<String, dynamic>,
null,
null,
),
)
.toList();
controller.filteredData
..clear()
..addAll(requests);
}

@override
Expand Down Expand Up @@ -187,7 +216,7 @@ class _NetworkProfilerControlsState extends State<_NetworkProfilerControls>
@override
void initState() {
super.initState();

addAutoDisposeListener(offlineDataController.showingOfflineData);
hrajwade96 marked this conversation as resolved.
Show resolved Hide resolved
_recording = widget.controller.recordingNotifier.value;
addAutoDisposeListener(widget.controller.recordingNotifier, () {
setState(() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2024 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.

import 'dart:convert';

/// Calculates the size of the headers in bytes.
///
/// Takes a map of headers [headers], where keys are header names and values
/// can be strings or lists of strings. Returns the size of the headers
/// in bytes or -1 if [headers] is null.
int calculateHeadersSize(Map<String, Object?>? headers) {
if (headers == null) return -1;

// Combine headers into a single string with CRLF endings
String headersString = headers.entries.map((entry) {
final key = entry.key;
var value = entry.value;
// If the value is a List, join it with a comma
if (value is List<String>) {
value = value.join(', ');
}
return '$key: $value\r\n';
}).join();

// Add final CRLF to indicate end of headers
headersString += '\r\n';

// Calculate the byte length of the headers string
return utf8.encode(headersString).length;
}
8 changes: 8 additions & 0 deletions packages/devtools_app/lib/src/shared/feature_flags.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ bool get enableBeta => enableExperiments || !isExternalBuild;
const _kMemoryDisconnectExperience =
bool.fromEnvironment('memory_disconnect_experience', defaultValue: true);

const _kNetworkOfflineExperiment =
bool.fromEnvironment('network_disconnect_experience', defaultValue: true);

// It is ok to have enum-like static only classes.
// ignore: avoid_classes_with_only_static_members
/// Flags to hide features under construction.
Expand All @@ -62,6 +65,11 @@ abstract class FeatureFlags {
/// https://github.com/flutter/devtools/issues/5606
static const memoryDisconnectExperience = _kMemoryDisconnectExperience;

/// Flag to enable offline data on network screen.
///
/// https://github.com/flutter/devtools/issues/3806
static const networkOffline = _kNetworkOfflineExperiment;

/// Flag to enable save/load for the Memory screen.
///
/// https://github.com/flutter/devtools/issues/8019
Expand Down
74 changes: 74 additions & 0 deletions packages/devtools_app/lib/src/shared/http/constants.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright 2024 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.

enum HttpRequestDataKeys {
connectionInfo,
remoteAddress,
localPort,
contentLength,
startedDateTime,
time,
request,
method,
url,
httpVersion,
cookies,
headers,
queryString,
postData,
mimeType,
text,
headersSize,
bodySize,
followRedirects,
maxRedirects,
persistentConnection,
proxyDetails,
proxy,
type,
error,
response,
status,
statusCode,
statusText,
redirects,
redirectURL,
cache,
timings,
blocked,
dns,
connect,
send,
wait,
receive,
ssl,
connection,
comment,
isolateId,
uri,
id,
startTime,
events,
timestamp,
event,
compressionState,
isRedirect,
reasonPhrase,
queryParameters,
content,
size,
connectionId
}

enum HttpRequestDataValues {
json,
}

class HttpRequestDataDefaults {
static const none = 'None';
static const error = 'Error';
static const httpVersion = 'HTTP/2.0';
static const json = 'json';
static const httpProfileRequest = '@HttpProfileRequest';
}
Loading