| LEFT | RIGHT |
| 1 /* | 1 /* |
| 2 * This file is part of Adblock Plus <http://adblockplus.org/>, | 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2014 Eyeo GmbH | 3 * Copyright (C) 2006-2014 Eyeo GmbH |
| 4 * | 4 * |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
| 7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
| 8 * | 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 import java.util.ArrayList; | 22 import java.util.ArrayList; |
| 23 import java.util.HashMap; | 23 import java.util.HashMap; |
| 24 | 24 |
| 25 import org.adblockplus.android.Utils; | 25 import org.adblockplus.android.Utils; |
| 26 | 26 |
| 27 import android.content.Context; | 27 import android.content.Context; |
| 28 import android.util.Log; | 28 import android.util.Log; |
| 29 | 29 |
| 30 /** | 30 /** |
| 31 * Common proxy configurator methods. | 31 * Common proxy configurator methods. |
| 32 * | |
| 33 * @author rjeschke | |
| 34 */ | 32 */ |
| 35 public final class ProxyConfigurators | 33 public final class ProxyConfigurators |
| 36 { | 34 { |
| 37 private static final String TAG = Utils.getTag(ProxyConfigurators.class); | 35 private static final String TAG = Utils.getTag(ProxyConfigurators.class); |
| 38 private static final ArrayList<Class<?>> CONFIGURATORS = new ArrayList<Class<?
>>(); | 36 private static final ArrayList<Class<?>> CONFIGURATORS = new ArrayList<Class<?
>>(); |
| 39 private static final HashMap<ProxyRegistrationType, Class<?>> CONFIGURATOR_MAP
= new HashMap<ProxyRegistrationType, Class<?>>(); | 37 private static final HashMap<ProxyRegistrationType, Class<?>> CONFIGURATOR_MAP
= new HashMap<ProxyRegistrationType, Class<?>>(); |
| 40 private static final HashMap<Class<?>, ProxyRegistrationType> CONFIGURATOR_INV
_MAP = new HashMap<Class<?>, ProxyRegistrationType>(); | 38 private static final HashMap<Class<?>, ProxyRegistrationType> CONFIGURATOR_INV
_MAP = new HashMap<Class<?>, ProxyRegistrationType>(); |
| 41 | 39 |
| 42 static | 40 static |
| 43 { | 41 { |
| 44 /* | 42 /* |
| 45 * Add new proxy-configurators here. | 43 * Add new proxy-configurators here. |
| 46 * | 44 * |
| 47 * Note: order of adding is important (as later added configurators act as f
allbacks for earlier ones). | 45 * Note: order of adding is important (as later added configurators act as f
allbacks for earlier ones). |
| 48 */ | 46 */ |
| 49 add(CyanogenProxyConfigurator.class); | 47 add(CyanogenProxyConfigurator.class); |
| 50 add(IptablesProxyConfigurator.class); | 48 add(IptablesProxyConfigurator.class); |
| 51 add(ReflectionProxyConfigurator.class); | 49 add(NativeProxyConfigurator.class); |
| 52 add(ManualProxyConfigurator.class); | 50 add(ManualProxyConfigurator.class); |
| 53 } | 51 } |
| 54 | 52 |
| 55 private ProxyConfigurators() | 53 private ProxyConfigurators() |
| 56 { | 54 { |
| 57 // no instantiation | 55 // no instantiation |
| 58 } | 56 } |
| 59 | 57 |
| 60 private static void add(Class<?> clazz) | 58 private static void add(final Class<?> clazz) |
| 61 { | 59 { |
| 62 try | 60 try |
| 63 { | 61 { |
| 64 final Constructor<?> ctor = clazz.getConstructor(Context.class); | 62 final Constructor<?> ctor = clazz.getConstructor(Context.class); |
| 65 final ProxyConfigurator pc = (ProxyConfigurator) ctor.newInstance((Context
) null); | 63 final ProxyConfigurator pc = (ProxyConfigurator) ctor.newInstance((Context
) null); |
| 66 final ProxyRegistrationType type = pc.getType(); | 64 final ProxyRegistrationType type = pc.getType(); |
| 67 | 65 |
| 68 if (!CONFIGURATOR_MAP.containsKey(type)) | 66 if (!CONFIGURATOR_MAP.containsKey(type)) |
| 69 { | 67 { |
| 70 CONFIGURATORS.add(clazz); | 68 CONFIGURATORS.add(clazz); |
| 71 CONFIGURATOR_MAP.put(type, clazz); | 69 CONFIGURATOR_MAP.put(type, clazz); |
| 72 CONFIGURATOR_INV_MAP.put(clazz, type); | 70 CONFIGURATOR_INV_MAP.put(clazz, type); |
| 73 } | 71 } |
| 74 else | 72 else |
| 75 { | 73 { |
| 76 // We fail hard here. | 74 // We fail hard here. |
| 77 throw new IllegalArgumentException("Duplicate proxy configurator (" + cl
azz + ") for type: " + type); | 75 throw new IllegalArgumentException("Duplicate proxy configurator (" + cl
azz + ") for type: " + type); |
| 78 } | 76 } |
| 79 } | 77 } |
| 80 catch (Exception e) | 78 catch (final Exception e) |
| 81 { | 79 { |
| 82 // We fail hard here. | 80 // We fail hard here. |
| 83 throw new IllegalStateException("Failed to register proxy configurator ("
+ clazz + ")", e); | 81 throw new IllegalStateException("Failed to register proxy configurator ("
+ clazz + ")", e); |
| 84 } | 82 } |
| 85 } | 83 } |
| 86 | 84 |
| 87 private static ProxyConfigurator initialize(Context context, ProxyConfigurator
from) | 85 private static ProxyConfigurator initialize(final Context context, final Proxy
Configurator from) |
| 88 { | 86 { |
| 89 int i = 0; | 87 int i = 0; |
| 90 | 88 |
| 91 if (from != null) | 89 if (from != null) |
| 92 { | 90 { |
| 93 // Scan through configurators to find last used one | 91 // Scan through configurators to find last used one |
| 94 while (i < CONFIGURATORS.size() && !CONFIGURATORS.get(i).equals(from.getCl
ass())) | 92 while (i < CONFIGURATORS.size() && !CONFIGURATORS.get(i).equals(from.getCl
ass())) |
| 95 { | 93 { |
| 96 i++; | 94 i++; |
| 97 } | 95 } |
| 98 // One after last-used | 96 // One after last-used |
| 99 i++; | 97 i++; |
| 100 } | 98 } |
| 101 | 99 |
| 102 // Loop through configurators until one succeeds to initialize | 100 // Loop through configurators until one succeeds to initialize |
| 103 while (i < CONFIGURATORS.size()) | 101 while (i < CONFIGURATORS.size()) |
| 104 { | 102 { |
| 105 try | 103 try |
| 106 { | 104 { |
| 107 final Class<?> clazz = CONFIGURATORS.get(i); | 105 final Class<?> clazz = CONFIGURATORS.get(i); |
| 108 final Constructor<?> ctor = clazz.getConstructor(Context.class); | 106 final Constructor<?> ctor = clazz.getConstructor(Context.class); |
| 109 final ProxyConfigurator pc = (ProxyConfigurator) ctor.newInstance(contex
t); | 107 final ProxyConfigurator pc = (ProxyConfigurator) ctor.newInstance(contex
t); |
| 110 if (pc.initialize()) | 108 if (pc.initialize()) |
| 111 { | 109 { |
| 112 return pc; | 110 return pc; |
| 113 } | 111 } |
| 114 } | 112 } |
| 115 catch (Exception e) | 113 catch (final Exception e) |
| 116 { | 114 { |
| 117 Log.d(TAG, "Configurator exception", e); | 115 Log.d(TAG, "Configurator exception", e); |
| 118 // We don't need to handle exceptions here, only success matters | 116 // We don't need to handle exceptions here, only success matters |
| 119 } | 117 } |
| 120 | 118 |
| 121 i++; | 119 i++; |
| 122 } | 120 } |
| 123 | 121 |
| 124 return null; | 122 return null; |
| 125 } | 123 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 141 } | 139 } |
| 142 | 140 |
| 143 pc.shutdown(); | 141 pc.shutdown(); |
| 144 } | 142 } |
| 145 } | 143 } |
| 146 while (pc != null); | 144 while (pc != null); |
| 147 | 145 |
| 148 return null; | 146 return null; |
| 149 } | 147 } |
| 150 } | 148 } |
| LEFT | RIGHT |