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 "content/public/browser/browser_thread.h"
11 #include "net/http/http_request_info.h"
13 using content::BrowserThread
;
15 DevToolsNetworkController::DevToolsNetworkController()
16 : default_interceptor_(new DevToolsNetworkInterceptor()),
17 appcache_interceptor_(new DevToolsNetworkInterceptor()),
18 weak_ptr_factory_(this) {
21 DevToolsNetworkController::~DevToolsNetworkController() {
24 base::WeakPtr
<DevToolsNetworkInterceptor
>
25 DevToolsNetworkController::GetInterceptor(
26 DevToolsNetworkTransaction
* transaction
) {
27 DCHECK(thread_checker_
.CalledOnValidThread());
28 DCHECK(transaction
->request());
30 if (!interceptors_
.size())
31 return default_interceptor_
->GetWeakPtr();
33 transaction
->ProcessRequest();
35 const std::string
& client_id
= transaction
->client_id();
36 if (client_id
.empty())
37 return default_interceptor_
->GetWeakPtr();
39 DevToolsNetworkInterceptor
* interceptor
= interceptors_
.get(client_id
);
42 return default_interceptor_
->GetWeakPtr();
44 return interceptor
->GetWeakPtr();
47 void DevToolsNetworkController::SetNetworkState(
48 const std::string
& client_id
,
49 scoped_ptr
<DevToolsNetworkConditions
> conditions
) {
50 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
51 BrowserThread::PostTask(
52 content::BrowserThread::IO
,
55 &DevToolsNetworkController::SetNetworkStateOnIO
,
56 weak_ptr_factory_
.GetWeakPtr(),
58 base::Passed(&conditions
)));
61 void DevToolsNetworkController::SetNetworkStateOnIO(
62 const std::string
& client_id
,
63 scoped_ptr
<DevToolsNetworkConditions
> conditions
) {
64 DCHECK(thread_checker_
.CalledOnValidThread());
66 DevToolsNetworkInterceptor
* interceptor
= interceptors_
.get(client_id
);
71 Interceptor new_interceptor
= Interceptor(new DevToolsNetworkInterceptor());
72 new_interceptor
->UpdateConditions(conditions
.Pass());
73 interceptors_
.set(client_id
, new_interceptor
.Pass());
76 scoped_ptr
<DevToolsNetworkConditions
> online_conditions(
77 new DevToolsNetworkConditions());
78 interceptor
->UpdateConditions(online_conditions
.Pass());
79 interceptors_
.erase(client_id
);
81 interceptor
->UpdateConditions(conditions
.Pass());
85 bool has_offline_interceptors
= false;
86 Interceptors::iterator it
= interceptors_
.begin();
87 for (; it
!= interceptors_
.end(); ++it
) {
88 if (it
->second
->conditions()->offline()) {
89 has_offline_interceptors
= true;
94 bool is_appcache_offline
= appcache_interceptor_
->conditions()->offline();
95 if (is_appcache_offline
!= has_offline_interceptors
) {
96 scoped_ptr
<DevToolsNetworkConditions
> appcache_conditions(
97 new DevToolsNetworkConditions(has_offline_interceptors
));
98 appcache_interceptor_
->UpdateConditions(appcache_conditions
.Pass());