| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ios/net/request_tracker.h" | 5 #include "ios/net/request_tracker.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #import "ios/net/clients/crn_forwarding_network_client.h" | 8 #import "ios/net/clients/crn_forwarding_network_client.h" |
| 9 #import "ios/net/clients/crn_forwarding_network_client_factory.h" | 9 #import "ios/net/clients/crn_forwarding_network_client_factory.h" |
| 10 | 10 |
| 11 #if !defined(__has_feature) || !__has_feature(objc_arc) | 11 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 12 #error "This file requires ARC support." | 12 #error "This file requires ARC support." |
| 13 #endif | 13 #endif |
| 14 | 14 |
| 15 namespace net { | 15 namespace net { |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 // Reference to the single instance of the RequestTrackerFactory. | 19 // Reference to the single instance of the RequestTrackerFactory. |
| 20 RequestTracker::RequestTrackerFactory* g_request_tracker_factory = nullptr; | 20 RequestTracker::RequestTrackerFactory* g_request_tracker_factory = nullptr; |
| 21 | 21 |
| 22 // Array of network client factories that should be added to any new request |
| 23 // tracker. |
| 24 class GlobalNetworkClientFactories { |
| 25 public: |
| 26 static GlobalNetworkClientFactories* GetInstance() { |
| 27 if (!g_global_network_client_factories) |
| 28 g_global_network_client_factories = new GlobalNetworkClientFactories; |
| 29 return g_global_network_client_factories; |
| 30 } |
| 31 |
| 32 // Gets the factories. |
| 33 NSArray* GetFactories() { |
| 34 DCHECK(thread_checker_.CalledOnValidThread()); |
| 35 return factories_.get(); |
| 36 } |
| 37 |
| 38 // Adds a factory. |
| 39 void AddFactory(CRNForwardingNetworkClientFactory* factory) { |
| 40 DCHECK(thread_checker_.CalledOnValidThread()); |
| 41 DCHECK_EQ(NSNotFound, |
| 42 static_cast<NSInteger>([factories_ indexOfObject:factory])); |
| 43 DCHECK(!IsSelectorOverriden(factory, @selector(clientHandlingRequest:))); |
| 44 DCHECK(!IsSelectorOverriden(factory, |
| 45 @selector(clientHandlingResponse:request:))); |
| 46 DCHECK(!IsSelectorOverriden( |
| 47 factory, @selector(clientHandlingRedirect:url:response:))); |
| 48 [factories_ addObject:factory]; |
| 49 } |
| 50 |
| 51 // Returns true if |factory| re-implements |selector|. |
| 52 // Only used for debugging. |
| 53 bool IsSelectorOverriden(CRNForwardingNetworkClientFactory* factory, |
| 54 SEL selector) { |
| 55 return |
| 56 [factory methodForSelector:selector] != |
| 57 [CRNForwardingNetworkClientFactory instanceMethodForSelector:selector]; |
| 58 } |
| 59 |
| 60 private: |
| 61 GlobalNetworkClientFactories() : factories_([[NSMutableArray alloc] init]) {} |
| 62 |
| 63 base::scoped_nsobject<NSMutableArray> factories_; |
| 64 base::ThreadChecker thread_checker_; |
| 65 |
| 66 static GlobalNetworkClientFactories* g_global_network_client_factories; |
| 67 }; |
| 68 |
| 69 GlobalNetworkClientFactories* |
| 70 GlobalNetworkClientFactories::g_global_network_client_factories = nullptr; |
| 71 |
| 22 } // namespace | 72 } // namespace |
| 23 | 73 |
| 24 RequestTracker::RequestTrackerFactory::~RequestTrackerFactory() { | 74 RequestTracker::RequestTrackerFactory::~RequestTrackerFactory() { |
| 25 } | 75 } |
| 26 | 76 |
| 27 // static | 77 // static |
| 28 void RequestTracker::SetRequestTrackerFactory(RequestTrackerFactory* factory) { | 78 void RequestTracker::SetRequestTrackerFactory(RequestTrackerFactory* factory) { |
| 29 g_request_tracker_factory = factory; | 79 g_request_tracker_factory = factory; |
| 30 } | 80 } |
| 31 | 81 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 51 NSUInteger idx, | 101 NSUInteger idx, |
| 52 BOOL* stop) { | 102 BOOL* stop) { |
| 53 return [obj clientClass] == [factory clientClass]; | 103 return [obj clientClass] == [factory clientClass]; |
| 54 }] == NSNotFound); | 104 }] == NSNotFound); |
| 55 [client_factories_ addObject:factory]; | 105 [client_factories_ addObject:factory]; |
| 56 if ([factory requiresOrdering]) { | 106 if ([factory requiresOrdering]) { |
| 57 [client_factories_ sortUsingSelector:@selector(orderRelativeTo:)]; | 107 [client_factories_ sortUsingSelector:@selector(orderRelativeTo:)]; |
| 58 } | 108 } |
| 59 } | 109 } |
| 60 | 110 |
| 111 // static |
| 112 void RequestTracker::AddGlobalNetworkClientFactory( |
| 113 CRNForwardingNetworkClientFactory* factory) { |
| 114 GlobalNetworkClientFactories::GetInstance()->AddFactory(factory); |
| 115 } |
| 116 |
| 61 RequestTracker::RequestTracker() | 117 RequestTracker::RequestTracker() |
| 62 : client_factories_([[NSMutableArray alloc] init]), | 118 : client_factories_([[NSMutableArray alloc] init]), |
| 63 initialized_(false), | 119 initialized_(false), |
| 64 cache_mode_(CACHE_NORMAL), | 120 cache_mode_(CACHE_NORMAL), |
| 65 weak_ptr_factory_(this) { | 121 weak_ptr_factory_(this) { |
| 66 // RequestTracker can be created from the main thread and used from another | 122 // RequestTracker can be created from the main thread and used from another |
| 67 // thread. | 123 // thread. |
| 68 thread_checker_.DetachFromThread(); | 124 thread_checker_.DetachFromThread(); |
| 69 } | 125 } |
| 70 | 126 |
| 71 RequestTracker::~RequestTracker() { | 127 RequestTracker::~RequestTracker() { |
| 72 } | 128 } |
| 73 | 129 |
| 74 base::WeakPtr<RequestTracker> RequestTracker::GetWeakPtr() { | 130 base::WeakPtr<RequestTracker> RequestTracker::GetWeakPtr() { |
| 75 return weak_ptr_factory_.GetWeakPtr(); | 131 return weak_ptr_factory_.GetWeakPtr(); |
| 76 } | 132 } |
| 77 | 133 |
| 78 void RequestTracker::InvalidateWeakPtrs() { | 134 void RequestTracker::InvalidateWeakPtrs() { |
| 79 weak_ptr_factory_.InvalidateWeakPtrs(); | 135 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 80 } | 136 } |
| 81 | 137 |
| 82 void RequestTracker::Init() { | 138 void RequestTracker::Init() { |
| 83 DCHECK(thread_checker_.CalledOnValidThread()); | 139 DCHECK(thread_checker_.CalledOnValidThread()); |
| 84 DCHECK(!initialized_); | 140 DCHECK(!initialized_); |
| 85 initialized_ = true; | 141 initialized_ = true; |
| 142 for (CRNForwardingNetworkClientFactory* factory in |
| 143 GlobalNetworkClientFactories::GetInstance()->GetFactories()) { |
| 144 AddNetworkClientFactory(factory); |
| 145 } |
| 146 } |
| 147 |
| 148 // static |
| 149 NSArray* RequestTracker::GlobalClientsHandlingAnyRequest() { |
| 150 NSMutableArray* applicable_clients = [NSMutableArray array]; |
| 151 for (CRNForwardingNetworkClientFactory* factory in |
| 152 GlobalNetworkClientFactories::GetInstance()->GetFactories()) { |
| 153 CRNForwardingNetworkClient* client = [factory clientHandlingAnyRequest]; |
| 154 if (client) |
| 155 [applicable_clients addObject:client]; |
| 156 } |
| 157 return applicable_clients; |
| 86 } | 158 } |
| 87 | 159 |
| 88 NSArray* RequestTracker::ClientsHandlingAnyRequest() { | 160 NSArray* RequestTracker::ClientsHandlingAnyRequest() { |
| 89 DCHECK(thread_checker_.CalledOnValidThread()); | 161 DCHECK(thread_checker_.CalledOnValidThread()); |
| 90 DCHECK(initialized_); | 162 DCHECK(initialized_); |
| 91 NSMutableArray* applicable_clients = [NSMutableArray array]; | 163 NSMutableArray* applicable_clients = [NSMutableArray array]; |
| 92 for (CRNForwardingNetworkClientFactory* factory in client_factories_.get()) { | 164 for (CRNForwardingNetworkClientFactory* factory in client_factories_.get()) { |
| 93 CRNForwardingNetworkClient* client = [factory clientHandlingAnyRequest]; | 165 CRNForwardingNetworkClient* client = [factory clientHandlingAnyRequest]; |
| 94 if (client) | 166 if (client) |
| 95 [applicable_clients addObject:client]; | 167 [applicable_clients addObject:client]; |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 DCHECK(thread_checker_.CalledOnValidThread()); | 219 DCHECK(thread_checker_.CalledOnValidThread()); |
| 148 return cache_mode_; | 220 return cache_mode_; |
| 149 } | 221 } |
| 150 | 222 |
| 151 void RequestTracker::SetCacheMode(RequestTracker::CacheMode mode) { | 223 void RequestTracker::SetCacheMode(RequestTracker::CacheMode mode) { |
| 152 DCHECK(thread_checker_.CalledOnValidThread()); | 224 DCHECK(thread_checker_.CalledOnValidThread()); |
| 153 cache_mode_ = mode; | 225 cache_mode_ = mode; |
| 154 } | 226 } |
| 155 | 227 |
| 156 } // namespace net | 228 } // namespace net |
| OLD | NEW |