Move Webstore URL concepts to //extensions and out
[chromium-blink-merge.git] / chrome / browser / devtools / devtools_network_controller.cc
blob1d22fe94e619a057cddf27e1f7c9e988ed793d40
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/base/load_flags.h"
12 #include "net/http/http_request_info.h"
14 using content::BrowserThread;
16 DevToolsNetworkController::DevToolsNetworkController()
17 : default_interceptor_(new DevToolsNetworkInterceptor()),
18 appcache_interceptor_(new DevToolsNetworkInterceptor()),
19 weak_ptr_factory_(this) {
22 DevToolsNetworkController::~DevToolsNetworkController() {
25 base::WeakPtr<DevToolsNetworkInterceptor>
26 DevToolsNetworkController::GetInterceptor(
27 DevToolsNetworkTransaction* transaction) {
28 DCHECK(thread_checker_.CalledOnValidThread());
29 DCHECK(transaction->request());
31 if (!interceptors_.size())
32 return default_interceptor_->GetWeakPtr();
34 if (transaction->request()->load_flags & net::LOAD_DISABLE_INTERCEPT)
35 return appcache_interceptor_->GetWeakPtr();
37 transaction->ProcessRequest();
39 const std::string& client_id = transaction->client_id();
40 if (client_id.empty())
41 return default_interceptor_->GetWeakPtr();
43 DevToolsNetworkInterceptor* interceptor = interceptors_.get(client_id);
44 DCHECK(interceptor);
45 if (!interceptor)
46 return default_interceptor_->GetWeakPtr();
48 return interceptor->GetWeakPtr();
51 void DevToolsNetworkController::SetNetworkState(
52 const std::string& client_id,
53 scoped_ptr<DevToolsNetworkConditions> conditions) {
54 DCHECK_CURRENTLY_ON(BrowserThread::UI);
55 BrowserThread::PostTask(
56 content::BrowserThread::IO,
57 FROM_HERE,
58 base::Bind(
59 &DevToolsNetworkController::SetNetworkStateOnIO,
60 weak_ptr_factory_.GetWeakPtr(),
61 client_id,
62 base::Passed(&conditions)));
65 void DevToolsNetworkController::SetNetworkStateOnIO(
66 const std::string& client_id,
67 scoped_ptr<DevToolsNetworkConditions> conditions) {
68 DCHECK(thread_checker_.CalledOnValidThread());
70 DevToolsNetworkInterceptor* interceptor = interceptors_.get(client_id);
71 if (!interceptor) {
72 DCHECK(conditions);
73 if (!conditions)
74 return;
75 Interceptor new_interceptor = Interceptor(new DevToolsNetworkInterceptor());
76 new_interceptor->UpdateConditions(conditions.Pass());
77 interceptors_.set(client_id, new_interceptor.Pass());
78 } else {
79 if (!conditions) {
80 scoped_ptr<DevToolsNetworkConditions> online_conditions(
81 new DevToolsNetworkConditions());
82 interceptor->UpdateConditions(online_conditions.Pass());
83 interceptors_.erase(client_id);
84 } else {
85 interceptor->UpdateConditions(conditions.Pass());
89 bool has_offline_interceptors = false;
90 Interceptors::iterator it = interceptors_.begin();
91 for (; it != interceptors_.end(); ++it) {
92 if (it->second->conditions()->offline()) {
93 has_offline_interceptors = true;
94 break;
98 bool is_appcache_offline = appcache_interceptor_->conditions()->offline();
99 if (is_appcache_offline != has_offline_interceptors) {
100 scoped_ptr<DevToolsNetworkConditions> appcache_conditions(
101 new DevToolsNetworkConditions(has_offline_interceptors));
102 appcache_interceptor_->UpdateConditions(appcache_conditions.Pass());