From 65cbc95d38819e9b3d5870953015a9e7963a0ace Mon Sep 17 00:00:00 2001 From: Taym Haddadi Date: Wed, 3 Jan 2024 17:52:04 +0100 Subject: [PATCH] Replace time with std::time in components/devtools (#30927) Signed-off-by: Bentaimia Haddadi --- Cargo.lock | 2 +- components/devtools/Cargo.toml | 2 +- components/devtools/actors/console.rs | 12 ++++++--- components/devtools/actors/network_event.rs | 27 +++++++++++++++++---- components/devtools/actors/timeline.rs | 5 +--- components/net/http_loader.rs | 9 +++---- components/shared/devtools/lib.rs | 6 ++--- 7 files changed, 41 insertions(+), 22 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 111b758ca42..f8f9169dc34 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1351,6 +1351,7 @@ dependencies = [ name = "devtools" version = "0.0.1" dependencies = [ + "chrono", "crossbeam-channel", "devtools_traits", "embedder_traits", @@ -1364,7 +1365,6 @@ dependencies = [ "servo_config", "servo_rand", "servo_url", - "time 0.1.45", "uuid", ] diff --git a/components/devtools/Cargo.toml b/components/devtools/Cargo.toml index 413fc560c1b..a6e7c0685d7 100644 --- a/components/devtools/Cargo.toml +++ b/components/devtools/Cargo.toml @@ -11,6 +11,7 @@ name = "devtools" path = "lib.rs" [dependencies] +chrono = "0.4" crossbeam-channel = { workspace = true } devtools_traits = { workspace = true } embedder_traits = { workspace = true } @@ -24,5 +25,4 @@ serde_json = { workspace = true } servo_config = { path = "../config" } servo_rand = { path = "../rand" } servo_url = { path = "../url" } -time = { workspace = true } uuid = { workspace = true } diff --git a/components/devtools/actors/console.rs b/components/devtools/actors/console.rs index 16b9512631e..f9d7535e38f 100644 --- a/components/devtools/actors/console.rs +++ b/components/devtools/actors/console.rs @@ -10,6 +10,7 @@ use std::cell::RefCell; use std::collections::HashMap; use std::net::TcpStream; +use std::time::{SystemTime, UNIX_EPOCH}; use devtools_traits::EvaluateJSReply::{ ActorValue, BooleanValue, NullValue, NumberValue, StringValue, VoidValue, @@ -23,7 +24,6 @@ use log::debug; use msg::constellation_msg::TEST_PIPELINE_ID; use serde::Serialize; use serde_json::{self, Map, Number, Value}; -use time::precise_time_ns; use uuid::Uuid; use crate::actor::{Actor, ActorMessageStatus, ActorRegistry}; @@ -295,7 +295,10 @@ impl ConsoleActor { filename: console_message.filename.clone(), lineNumber: console_message.lineNumber as u32, functionName: "".to_string(), //TODO - timeStamp: precise_time_ns(), + timeStamp: SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap_or_default() + .as_nanos() as u64, private: false, arguments: vec![console_message.message.clone()], })); @@ -305,7 +308,10 @@ impl ConsoleActor { type_: "consoleAPICall".to_owned(), message: ConsoleMsg { level: level, - timeStamp: precise_time_ns(), + timeStamp: SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap_or_default() + .as_nanos() as u64, arguments: vec![console_message.message], filename: console_message.filename, lineNumber: console_message.lineNumber, diff --git a/components/devtools/actors/network_event.rs b/components/devtools/actors/network_event.rs index 53c858a680b..af360d46ca2 100644 --- a/components/devtools/actors/network_event.rs +++ b/components/devtools/actors/network_event.rs @@ -7,13 +7,14 @@ //! Handles interaction with the remote web console on network events (HTTP requests, responses) in Servo. use std::net::TcpStream; +use std::time::{SystemTime, UNIX_EPOCH}; +use chrono::{Local, LocalResult, TimeZone}; use devtools_traits::{HttpRequest as DevtoolsHttpRequest, HttpResponse as DevtoolsHttpResponse}; use headers::{ContentType, Cookie, HeaderMapExt}; use http::{header, HeaderMap, Method, StatusCode}; use serde::Serialize; use serde_json::{Map, Value}; -use time::Tm; use crate::actor::{Actor, ActorMessageStatus, ActorRegistry}; use crate::protocol::JsonPacketStream; @@ -24,7 +25,7 @@ struct HttpRequest { method: Method, headers: HeaderMap, body: Option>, - startedDateTime: Tm, + startedDateTime: SystemTime, timeStamp: i64, connect_time: u64, send_time: u64, @@ -328,8 +329,11 @@ impl NetworkEventActor { method: Method::GET, headers: HeaderMap::new(), body: None, - startedDateTime: time::now(), - timeStamp: time::get_time().sec, + startedDateTime: SystemTime::now(), + timeStamp: SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap_or_default() + .as_secs() as i64, send_time: 0, connect_time: 0, }, @@ -366,11 +370,24 @@ impl NetworkEventActor { pub fn event_actor(&self) -> EventActor { // TODO: Send the correct values for startedDateTime, isXHR, private + + let started_datetime_rfc3339 = match Local.timestamp_millis_opt( + self.request + .startedDateTime + .duration_since(UNIX_EPOCH) + .unwrap_or_default() + .as_millis() as i64, + ) { + LocalResult::None => "".to_owned(), + LocalResult::Single(dateTime) => format!("{}", dateTime.to_rfc3339()), + LocalResult::Ambiguous(dateTime, _) => format!("{}", dateTime.to_rfc3339()), + }; + EventActor { actor: self.name(), url: self.request.url.clone(), method: format!("{}", self.request.method), - startedDateTime: format!("{}", self.request.startedDateTime.rfc3339()), + startedDateTime: started_datetime_rfc3339, timeStamp: self.request.timeStamp, isXHR: self.is_xhr, private: false, diff --git a/components/devtools/actors/timeline.rs b/components/devtools/actors/timeline.rs index 9cf9475b141..2f86b214aa4 100644 --- a/components/devtools/actors/timeline.rs +++ b/components/devtools/actors/timeline.rs @@ -106,10 +106,7 @@ pub struct HighResolutionStamp(f64); impl HighResolutionStamp { pub fn new(start_stamp: PreciseTime, time: PreciseTime) -> HighResolutionStamp { - let duration = start_stamp - .to(time) - .num_microseconds() - .expect("Too big duration in microseconds"); + let duration = start_stamp.to(time).as_micros(); HighResolutionStamp(duration as f64 / 1000 as f64) } diff --git a/components/net/http_loader.rs b/components/net/http_loader.rs index 79b2c63dc44..17f12b13c1b 100644 --- a/components/net/http_loader.rs +++ b/components/net/http_loader.rs @@ -8,7 +8,7 @@ use std::iter::FromIterator; use std::mem; use std::ops::Deref; use std::sync::{Arc as StdArc, Condvar, Mutex, RwLock}; -use std::time::{Duration, SystemTime}; +use std::time::{Duration, SystemTime, UNIX_EPOCH}; use async_recursion::async_recursion; use crossbeam_channel::Sender; @@ -52,7 +52,6 @@ use net_traits::{ }; use servo_arc::Arc; use servo_url::{ImmutableOrigin, ServoUrl}; -use time::{self, Tm}; use tokio::runtime::Runtime; use tokio::sync::mpsc::{ channel, unbounded_channel, Receiver as TokioReceiver, Sender as TokioSender, @@ -358,7 +357,7 @@ fn prepare_devtools_request( headers: HeaderMap, body: Option>, pipeline_id: PipelineId, - now: Tm, + now: SystemTime, connect_time: u64, send_time: u64, is_xhr: bool, @@ -370,7 +369,7 @@ fn prepare_devtools_request( body: body, pipeline_id: pipeline_id, startedDateTime: now, - timeStamp: now.to_timespec().sec, + timeStamp: now.duration_since(UNIX_EPOCH).unwrap_or_default().as_secs() as i64, connect_time: connect_time, send_time: send_time, is_xhr: is_xhr, @@ -675,7 +674,7 @@ async fn obtain_response( headers, Some(devtools_bytes.lock().unwrap().clone()), pipeline_id, - time::now(), + SystemTime::now(), connect_end - connect_start, send_end - send_start, is_xhr, diff --git a/components/shared/devtools/lib.rs b/components/shared/devtools/lib.rs index 57e11ad8784..0726bb48541 100644 --- a/components/shared/devtools/lib.rs +++ b/components/shared/devtools/lib.rs @@ -12,6 +12,7 @@ #![deny(unsafe_code)] use std::net::TcpStream; +use std::time::{Duration, SystemTime}; use bitflags::bitflags; use http::{HeaderMap, Method}; @@ -20,7 +21,6 @@ use malloc_size_of_derive::MallocSizeOf; use msg::constellation_msg::{BrowsingContextId, PipelineId}; use serde::{Deserialize, Serialize}; use servo_url::ServoUrl; -use time::{self, Duration, Tm}; use uuid::Uuid; // Information would be attached to NewGlobal to be received and show in devtools. @@ -306,7 +306,7 @@ pub struct HttpRequest { pub headers: HeaderMap, pub body: Option>, pub pipeline_id: PipelineId, - pub startedDateTime: Tm, + pub startedDateTime: SystemTime, pub timeStamp: i64, pub connect_time: u64, pub send_time: u64, @@ -364,7 +364,7 @@ impl PreciseTime { } pub fn to(&self, later: PreciseTime) -> Duration { - Duration::nanoseconds((later.0 - self.0) as i64) + Duration::from_nanos(later.0 - self.0) } }