1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/devtools/devtools_network_controller.h"
7 #include "chrome/browser/devtools/devtools_network_conditions.h"
8 #include "chrome/browser/devtools/devtools_network_interceptor.h"
9 #include "chrome/browser/devtools/devtools_network_transaction.h"
10 #include "net/http/http_request_info.h"
12 DevToolsNetworkController::DevToolsNetworkController()
13 : default_interceptor_(new DevToolsNetworkInterceptor()),
14 appcache_interceptor_(new DevToolsNetworkInterceptor()) {}
16 DevToolsNetworkController::~DevToolsNetworkController() {
17 DCHECK(thread_checker_
.CalledOnValidThread());
20 base::WeakPtr
<DevToolsNetworkInterceptor
>
21 DevToolsNetworkController::GetInterceptor(
22 DevToolsNetworkTransaction
* transaction
) {
23 DCHECK(thread_checker_
.CalledOnValidThread());
24 DCHECK(transaction
->request());
26 if (!interceptors_
.size())
27 return default_interceptor_
->GetWeakPtr();
29 transaction
->ProcessRequest();
31 const std::string
& client_id
= transaction
->client_id();
32 if (client_id
.empty())
33 return default_interceptor_
->GetWeakPtr();
35 DevToolsNetworkInterceptor
* interceptor
= interceptors_
.get(client_id
);
38 return default_interceptor_
->GetWeakPtr();
40 return interceptor
->GetWeakPtr();
43 void DevToolsNetworkController::SetNetworkState(
44 const std::string
& client_id
,
45 scoped_ptr
<DevToolsNetworkConditions
> conditions
) {
46 DCHECK(thread_checker_
.CalledOnValidThread());
48 DevToolsNetworkInterceptor
* interceptor
= interceptors_
.get(client_id
);
53 scoped_ptr
<DevToolsNetworkInterceptor
> new_interceptor(
54 new DevToolsNetworkInterceptor());
55 new_interceptor
->UpdateConditions(conditions
.Pass());
56 interceptors_
.set(client_id
, new_interceptor
.Pass());
59 scoped_ptr
<DevToolsNetworkConditions
> online_conditions(
60 new DevToolsNetworkConditions());
61 interceptor
->UpdateConditions(online_conditions
.Pass());
62 interceptors_
.erase(client_id
);
64 interceptor
->UpdateConditions(conditions
.Pass());
68 bool has_offline_interceptors
= false;
69 InterceptorMap::iterator it
= interceptors_
.begin();
70 for (; it
!= interceptors_
.end(); ++it
) {
71 if (it
->second
->conditions()->offline()) {
72 has_offline_interceptors
= true;
77 bool is_appcache_offline
= appcache_interceptor_
->conditions()->offline();
78 if (is_appcache_offline
!= has_offline_interceptors
) {
79 scoped_ptr
<DevToolsNetworkConditions
> appcache_conditions(
80 new DevToolsNetworkConditions(has_offline_interceptors
));
81 appcache_interceptor_
->UpdateConditions(appcache_conditions
.Pass());