mirror of
https://github.com/servo/servo.git
synced 2025-08-06 22:15:33 +01:00
Migrate user agent string to Cow<'static, str>
.
In most scenarios, where the user of Servo will not override the default user agent, the user agent can be a `&'static str`. But since we allow for customization, we currently use a `String` to represent the user agent. This commit migrates the user agent to be represented as a `Cow<'static, str>`, which (at the cost of ergonomics) prevents unnecessary allocations whenever cloning the user agent string in the scenario the user doesn't override the user agent.
This commit is contained in:
parent
ef7423bf00
commit
60afad1b61
7 changed files with 66 additions and 63 deletions
|
@ -29,6 +29,7 @@ use net_traits::request::{Type, Origin, Window};
|
|||
use net_traits::response::{HttpsState, TerminationReason};
|
||||
use net_traits::response::{Response, ResponseBody, ResponseType};
|
||||
use resource_thread::CancellationListener;
|
||||
use std::borrow::Cow;
|
||||
use std::collections::HashSet;
|
||||
use std::fs::File;
|
||||
use std::io::Read;
|
||||
|
@ -51,7 +52,7 @@ enum Data {
|
|||
|
||||
pub struct FetchContext {
|
||||
pub state: HttpState,
|
||||
pub user_agent: String,
|
||||
pub user_agent: Cow<'static, str>,
|
||||
pub devtools_chan: Option<Sender<DevtoolsControlMsg>>,
|
||||
}
|
||||
|
||||
|
@ -763,7 +764,8 @@ fn http_network_or_cache_fetch(request: Rc<Request>,
|
|||
|
||||
// Step 8
|
||||
if !http_request.headers.borrow().has::<UserAgent>() {
|
||||
http_request.headers.borrow_mut().set(UserAgent(context.user_agent.clone()));
|
||||
let user_agent = context.user_agent.clone().into_owned();
|
||||
http_request.headers.borrow_mut().set(UserAgent(user_agent));
|
||||
}
|
||||
|
||||
match http_request.cache_mode.get() {
|
||||
|
|
|
@ -39,7 +39,7 @@ use openssl::ssl::error::{OpensslError, SslError};
|
|||
use profile_traits::time::{ProfilerCategory, ProfilerChan, TimerMetadata, profile};
|
||||
use profile_traits::time::{TimerMetadataFrameType, TimerMetadataReflowType};
|
||||
use resource_thread::{AuthCache, AuthCacheEntry, CancellationListener, send_error, start_sending_sniffed_opt};
|
||||
use std::borrow::ToOwned;
|
||||
use std::borrow::{Cow, ToOwned};
|
||||
use std::boxed::FnBox;
|
||||
use std::collections::HashSet;
|
||||
use std::error::Error;
|
||||
|
@ -57,7 +57,7 @@ use util::prefs::PREFS;
|
|||
use util::thread::spawn_named;
|
||||
use uuid;
|
||||
|
||||
pub fn factory(user_agent: String,
|
||||
pub fn factory(user_agent: Cow<'static, str>,
|
||||
http_state: HttpState,
|
||||
devtools_chan: Option<Sender<DevtoolsControlMsg>>,
|
||||
profiler_chan: ProfilerChan,
|
||||
|
@ -137,7 +137,7 @@ fn load_for_consumer(load_data: LoadData,
|
|||
devtools_chan: Option<Sender<DevtoolsControlMsg>>,
|
||||
swmanager_chan: Option<IpcSender<CustomResponseMediator>>,
|
||||
cancel_listener: CancellationListener,
|
||||
user_agent: String) {
|
||||
user_agent: Cow<'static, str>) {
|
||||
let factory = NetworkHttpRequestFactory {
|
||||
connector: connector,
|
||||
};
|
||||
|
@ -865,7 +865,7 @@ pub fn load<A, B>(load_data: &LoadData,
|
|||
http_state: &HttpState,
|
||||
devtools_chan: Option<Sender<DevtoolsControlMsg>>,
|
||||
request_factory: &HttpRequestFactory<R=A>,
|
||||
user_agent: String,
|
||||
user_agent: Cow<'static, str>,
|
||||
cancel_listener: &CancellationListener,
|
||||
swmanager_chan: Option<IpcSender<CustomResponseMediator>>)
|
||||
-> Result<StreamedResponse, LoadError> where A: HttpRequest + 'static, B: UIProvider {
|
||||
|
|
|
@ -35,7 +35,7 @@ use net_traits::storage_thread::StorageThreadMsg;
|
|||
use profile_traits::time::ProfilerChan;
|
||||
use rustc_serialize::{Decodable, Encodable};
|
||||
use rustc_serialize::json;
|
||||
use std::borrow::ToOwned;
|
||||
use std::borrow::{Cow, ToOwned};
|
||||
use std::boxed::FnBox;
|
||||
use std::cell::Cell;
|
||||
use std::collections::HashMap;
|
||||
|
@ -164,7 +164,7 @@ fn start_sending_opt(start_chan: LoadConsumer, metadata: Metadata,
|
|||
}
|
||||
|
||||
/// Returns a tuple of (public, private) senders to the new threads.
|
||||
pub fn new_resource_threads(user_agent: String,
|
||||
pub fn new_resource_threads(user_agent: Cow<'static, str>,
|
||||
devtools_chan: Option<Sender<DevtoolsControlMsg>>,
|
||||
profiler_chan: ProfilerChan,
|
||||
config_dir: Option<PathBuf>)
|
||||
|
@ -181,7 +181,7 @@ pub fn new_resource_threads(user_agent: String,
|
|||
|
||||
|
||||
/// Create a CoreResourceThread
|
||||
pub fn new_core_resource_thread(user_agent: String,
|
||||
pub fn new_core_resource_thread(user_agent: Cow<'static, str>,
|
||||
devtools_chan: Option<Sender<DevtoolsControlMsg>>,
|
||||
profiler_chan: ProfilerChan,
|
||||
config_dir: Option<PathBuf>)
|
||||
|
@ -470,7 +470,7 @@ pub struct AuthCache {
|
|||
}
|
||||
|
||||
pub struct CoreResourceManager {
|
||||
user_agent: String,
|
||||
user_agent: Cow<'static, str>,
|
||||
mime_classifier: Arc<MimeClassifier>,
|
||||
devtools_chan: Option<Sender<DevtoolsControlMsg>>,
|
||||
swmanager_chan: Option<IpcSender<CustomResponseMediator>>,
|
||||
|
@ -481,7 +481,7 @@ pub struct CoreResourceManager {
|
|||
}
|
||||
|
||||
impl CoreResourceManager {
|
||||
pub fn new(user_agent: String,
|
||||
pub fn new(user_agent: Cow<'static, str>,
|
||||
devtools_channel: Option<Sender<DevtoolsControlMsg>>,
|
||||
profiler_chan: ProfilerChan) -> CoreResourceManager {
|
||||
CoreResourceManager {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue