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 #import "ios/net/clients/crn_forwarding_network_client_factory.h"
7 #include "base/mac/scoped_nsobject.h"
8 #include "base/memory/weak_ptr.h"
9 #import "ios/net/clients/crn_forwarding_network_client.h"
11 @implementation CRNForwardingNetworkClientFactory
13 // Init just sanity checks that the factory class has sane ordering rules.
14 - (instancetype)init {
15 if ((self = [super init])) {
17 DCHECK(![[self class] mustApplyBefore] ||
18 [[[self class] mustApplyBefore]
19 isSubclassOfClass:[CRNForwardingNetworkClientFactory class]]);
20 DCHECK(![[self class] mustApplyAfter] ||
21 [[[self class] mustApplyAfter]
22 isSubclassOfClass:[CRNForwardingNetworkClientFactory class]]);
24 DCHECK([[self class] orderedOK]);
29 - (CRNForwardingNetworkClient*)clientHandlingAnyRequest {
33 - (CRNForwardingNetworkClient*)
34 clientHandlingRequest:(const net::URLRequest&)request {
38 - (CRNForwardingNetworkClient*)
39 clientHandlingResponse:(NSURLResponse*)response
40 request:(const net::URLRequest&)request {
44 - (CRNForwardingNetworkClient*)
45 clientHandlingRedirect:(const net::URLRequest&)request
47 response:(NSURLResponse*)response {
51 - (Class)clientClass {
55 + (Class)mustApplyBefore {
59 + (Class)mustApplyAfter {
63 - (BOOL)requiresOrdering {
64 return [[self class] mustApplyAfter] || [[self class] mustApplyBefore];
67 // Verify that the ordering is sensible. If the calling class encounters itself
68 // walking dependencies in either direction, it's broken. If any of the classes
69 // it must appear after are also classes it must appear before, it's broken.
71 Class precursor = [self class];
72 NSMutableArray* precursors = [NSMutableArray array];
73 while ((precursor = [precursor mustApplyAfter])) {
74 if (precursor == [self class])
76 [precursors addObject:precursor];
78 id successor = [self class];
79 while ((successor = [successor mustApplyBefore])) {
80 if (successor == [self class])
82 if ([precursors indexOfObject:successor] != NSNotFound)
88 - (NSComparisonResult)
89 orderRelativeTo:(CRNForwardingNetworkClientFactory*)factory {
90 NSComparisonResult result = NSOrderedSame;
91 if ([[self class] mustApplyBefore] == [factory class] ||
92 [[factory class] mustApplyAfter] == [self class]) {
93 result = NSOrderedAscending;
95 if ([[self class] mustApplyAfter] == [factory class] ||
96 [[factory class] mustApplyBefore] == [self class]) {
97 result = NSOrderedDescending;