[Media Router] Add integration tests and e2e tests for media router and presentation...
[chromium-blink-merge.git] / chrome / browser / devtools / devtools_network_controller.cc
blobff785b5523faaaa14e09db55f3ae5ba921b00a5f
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);
40 DCHECK(interceptor);
41 if (!interceptor)
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,
53 FROM_HERE,
54 base::Bind(
55 &DevToolsNetworkController::SetNetworkStateOnIO,
56 weak_ptr_factory_.GetWeakPtr(),
57 client_id,
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);
67 if (!interceptor) {
68 DCHECK(conditions);
69 if (!conditions)
70 return;
71 Interceptor new_interceptor = Interceptor(new DevToolsNetworkInterceptor());
72 new_interceptor->UpdateConditions(conditions.Pass());
73 interceptors_.set(client_id, new_interceptor.Pass());
74 } else {
75 if (!conditions) {
76 scoped_ptr<DevToolsNetworkConditions> online_conditions(
77 new DevToolsNetworkConditions());
78 interceptor->UpdateConditions(online_conditions.Pass());
79 interceptors_.erase(client_id);
80 } else {
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;
90 break;
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());