OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "chrome/browser/devtools/devtools_network_controller.h" |
| 6 |
| 7 #include "chrome/browser/devtools/devtools_network_transaction.h" |
| 8 #include "chrome/browser/profiles/profile.h" |
| 9 #include "chrome/browser/profiles/profile_io_data.h" |
| 10 #include "content/public/browser/browser_thread.h" |
| 11 #include "content/public/browser/resource_context.h" |
| 12 |
| 13 using content::BrowserThread; |
| 14 |
| 15 namespace { |
| 16 |
| 17 const char kDevToolsRequestInitiator[] = "X-DevTools-Request-Initiator"; |
| 18 |
| 19 } // namespace |
| 20 |
| 21 DevToolsNetworkController::DevToolsNetworkController() |
| 22 : weak_ptr_factory_(this) { |
| 23 } |
| 24 |
| 25 DevToolsNetworkController::~DevToolsNetworkController() { |
| 26 } |
| 27 |
| 28 void DevToolsNetworkController::AddTransaction( |
| 29 DevToolsNetworkTransaction* transaction) { |
| 30 DCHECK(thread_checker_.CalledOnValidThread()); |
| 31 transactions_.insert(transaction); |
| 32 } |
| 33 |
| 34 void DevToolsNetworkController::RemoveTransaction( |
| 35 DevToolsNetworkTransaction* transaction) { |
| 36 DCHECK(thread_checker_.CalledOnValidThread()); |
| 37 DCHECK(transactions_.find(transaction) != transactions_.end()); |
| 38 transactions_.erase(transaction); |
| 39 } |
| 40 |
| 41 void DevToolsNetworkController::SetNetworkState( |
| 42 const std::string& client_id, |
| 43 bool offline) { |
| 44 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 45 BrowserThread::PostTask( |
| 46 content::BrowserThread::IO, |
| 47 FROM_HERE, |
| 48 base::Bind( |
| 49 &DevToolsNetworkController::SetNetworkStateOnIO, |
| 50 weak_ptr_factory_.GetWeakPtr(), |
| 51 client_id, |
| 52 offline)); |
| 53 } |
| 54 |
| 55 void DevToolsNetworkController::SetNetworkStateOnIO( |
| 56 const std::string& client_id, |
| 57 bool offline) { |
| 58 DCHECK(thread_checker_.CalledOnValidThread()); |
| 59 if (!offline) { |
| 60 clients_.erase(client_id); |
| 61 return; |
| 62 } |
| 63 clients_.insert(client_id); |
| 64 |
| 65 // Iterate over a copy of set, because failing of transaction could result in |
| 66 // creating a new one, or (theoretically) destroying one. |
| 67 Transactions old_transactions(transactions_); |
| 68 for (Transactions::iterator it = old_transactions.begin(); |
| 69 it != old_transactions.end(); ++it) { |
| 70 if (transactions_.find(*it) == transactions_.end()) |
| 71 continue; |
| 72 if (!(*it)->request() || (*it)->failed()) |
| 73 continue; |
| 74 if (ShouldFail((*it)->request())) |
| 75 (*it)->Fail(); |
| 76 } |
| 77 } |
| 78 |
| 79 bool DevToolsNetworkController::ShouldFail( |
| 80 const net::HttpRequestInfo* request) { |
| 81 DCHECK(thread_checker_.CalledOnValidThread()); |
| 82 DCHECK(request); |
| 83 if (clients_.empty()) |
| 84 return false; |
| 85 |
| 86 if (request->extra_headers.HasHeader(kDevToolsRequestInitiator)) |
| 87 return false; |
| 88 |
| 89 // TODO: Add domain blacklist. |
| 90 |
| 91 return true; |
| 92 } |
OLD | NEW |