summaryrefslogtreecommitdiffstats
path: root/services
diff options
context:
space:
mode:
authorJason Monk <jmonk@google.com>2013-08-20 23:35:26 +0000
committerAndroid (Google) Code Review <android-gerrit@google.com>2013-08-20 23:35:26 +0000
commitff796e5a24481febd8c07b1a6a3c3eda0e3fd88b (patch)
tree2ee074ef02867deb1bcfc2ac0762f7c49e939ed6 /services
parent59bbba7c37f2ce7b7fd00aef87b49d681b4a3a79 (diff)
parent9ced3cd9d6ea414523051ec872fffc68f5fdbf08 (diff)
downloadframeworks_base-ff796e5a24481febd8c07b1a6a3c3eda0e3fd88b.zip
frameworks_base-ff796e5a24481febd8c07b1a6a3c3eda0e3fd88b.tar.gz
frameworks_base-ff796e5a24481febd8c07b1a6a3c3eda0e3fd88b.tar.bz2
Merge "Change PacProcessor to Android Service" into klp-dev
Diffstat (limited to 'services')
-rw-r--r--services/java/com/android/server/connectivity/PacManager.java230
1 files changed, 132 insertions, 98 deletions
diff --git a/services/java/com/android/server/connectivity/PacManager.java b/services/java/com/android/server/connectivity/PacManager.java
index defe9f0..0b68ff5 100644
--- a/services/java/com/android/server/connectivity/PacManager.java
+++ b/services/java/com/android/server/connectivity/PacManager.java
@@ -1,14 +1,31 @@
+/**
+ * Copyright (c) 2013, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
package com.android.server.connectivity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
+import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
-import android.net.ConnectivityManager;
+import android.content.ServiceConnection;
import android.net.ProxyProperties;
+import android.os.IBinder;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.SystemClock;
@@ -17,42 +34,26 @@ import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
+import com.android.internal.annotations.GuardedBy;
import com.android.net.IProxyService;
+import com.android.server.IoThread;
+import libcore.io.Streams;
-import org.apache.http.HttpEntity;
-import org.apache.http.HttpException;
-import org.apache.http.HttpHost;
-import org.apache.http.HttpRequest;
-import org.apache.http.HttpResponse;
-import org.apache.http.client.ClientProtocolException;
-import org.apache.http.client.HttpClient;
-import org.apache.http.client.methods.HttpGet;
-import org.apache.http.conn.params.ConnRouteParams;
-import org.apache.http.conn.routing.HttpRoute;
-import org.apache.http.conn.routing.HttpRoutePlanner;
-import org.apache.http.impl.client.DefaultHttpClient;
-import org.apache.http.protocol.HttpContext;
-
-import java.io.BufferedReader;
import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.net.InetAddress;
-import java.net.ProxySelector;
import java.net.URL;
import java.net.URLConnection;
/**
* @hide
*/
-public class PacManager implements Runnable {
- public static final int NO_ERROR = 0;
- public static final int PERMISSION_DENIED = 1;
- public static final String PROXY_SERVICE = "com.android.net.IProxyService";
+public class PacManager {
+ public static final String PROXY_PACKAGE = "com.android.pacprocessor";
+ public static final String PROXY_SERVICE = "com.android.pacprocessor.PacService";
+ public static final String PROXY_SERVICE_NAME = "com.android.net.IProxyService";
- private static final String TAG = "PACManager";
+ private static final String TAG = "PacManager";
private static final String ACTION_PAC_REFRESH = "android.net.proxy.PAC_REFRESH";
@@ -64,31 +65,57 @@ public class PacManager implements Runnable {
/** Keep these values up-to-date with ProxyService.java */
public static final String KEY_PROXY = "keyProxy";
private String mCurrentPac;
- private volatile String mPacUrl;
+ @GuardedBy("mProxyLock")
+ private String mPacUrl;
private AlarmManager mAlarmManager;
+ @GuardedBy("mProxyLock")
private IProxyService mProxyService;
private PendingIntent mPacRefreshIntent;
+ private ServiceConnection mConnection;
private Context mContext;
private int mCurrentDelay;
+ /**
+ * Used for locking when setting mProxyService and all references to mPacUrl or mCurrentPac.
+ */
+ private final Object mProxyLock = new Object();
+
+ private Runnable mPacDownloader = new Runnable() {
+ @Override
+ public void run() {
+ String file;
+ synchronized (mProxyLock) {
+ if (mPacUrl == null) return;
+ try {
+ file = get(mPacUrl);
+ } catch (IOException ioe) {
+ file = null;
+ Log.w(TAG, "Failed to load PAC file: " + ioe);
+ }
+ }
+ if (file != null) {
+ synchronized (mProxyLock) {
+ if (!file.equals(mCurrentPac)) {
+ setCurrentProxyScript(file);
+ }
+ }
+ longSchedule();
+ } else {
+ reschedule();
+ }
+ }
+ };
+
class PacRefreshIntentReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
- new Thread(PacManager.this).start();
+ IoThread.getHandler().post(mPacDownloader);
}
}
public PacManager(Context context) {
mContext = context;
- mProxyService = IProxyService.Stub.asInterface(
- ServiceManager.getService(PROXY_SERVICE));
- if (mProxyService == null) {
- // Added because of b10267814 where mako is restarting.
- Log.e(TAG, "PacManager: no proxy service");
- } else {
- Log.d(TAG, "PacManager: mProxyService available");
- }
mPacRefreshIntent = PendingIntent.getBroadcast(
context, 0, new Intent(ACTION_PAC_REFRESH), 0);
@@ -103,26 +130,28 @@ public class PacManager implements Runnable {
return mAlarmManager;
}
- public void setCurrentProxyScriptUrl(ProxyProperties proxy) {
- if (mProxyService == null) {
- Log.e(TAG, "setCurrentProxyScriptUrl: no proxy service");
- return;
- }
+ public synchronized void setCurrentProxyScriptUrl(ProxyProperties proxy) {
if (!TextUtils.isEmpty(proxy.getPacFileUrl())) {
- try {
- mProxyService.startPacSystem();
+ synchronized (mProxyLock) {
mPacUrl = proxy.getPacFileUrl();
- mCurrentDelay = DELAY_1;
- getAlarmManager().cancel(mPacRefreshIntent);
- new Thread(this).start();
- } catch (RemoteException e) {
- Log.e(TAG, "Unable to reach ProxyService - PAC will not be started", e);
}
+ mCurrentDelay = DELAY_1;
+ getAlarmManager().cancel(mPacRefreshIntent);
+ bind();
} else {
- try {
- mProxyService.stopPacSystem();
- } catch (RemoteException e) {
- e.printStackTrace();
+ getAlarmManager().cancel(mPacRefreshIntent);
+ synchronized (mProxyLock) {
+ mPacUrl = null;
+ mCurrentPac = null;
+ if (mProxyService != null) {
+ try {
+ mProxyService.stopPacSystem();
+ } catch (RemoteException e) {
+ Log.w(TAG, "Failed to stop PAC service", e);
+ } finally {
+ unbind();
+ }
+ }
}
}
}
@@ -132,51 +161,10 @@ public class PacManager implements Runnable {
*
* @throws IOException
*/
- public static String get(String urlString) throws IOException {
+ private static String get(String urlString) throws IOException {
URL url = new URL(urlString);
URLConnection urlConnection = url.openConnection(java.net.Proxy.NO_PROXY);
- BufferedReader in = new BufferedReader(new InputStreamReader(
- urlConnection.getInputStream()));
- String inputLine;
- String resp = "";
- while ((inputLine = in.readLine()) != null) {
- resp = resp + inputLine + "\n";
- }
- in.close();
- return resp;
- }
-
- private static String toString(InputStream content) throws IOException {
- StringBuffer buffer = new StringBuffer();
- String line;
- BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(content));
-
- while ((line = bufferedReader.readLine()) != null) {
- if (buffer.length() != 0) {
- buffer.append('\n');
- }
- buffer.append(line);
- }
-
- return buffer.toString();
- }
-
- @Override
- public void run() {
- String file;
- try {
- file = get(mPacUrl);
- } catch (IOException ioe) {
- file = null;
- }
- if (file != null) {
- if (!file.equals(mCurrentPac)) {
- setCurrentProxyScript(file);
- }
- longSchedule();
- } else {
- reschedule();
- }
+ return new String(Streams.readFully(urlConnection.getInputStream()));
}
private int getNextDelay(int currentDelay) {
@@ -227,14 +215,60 @@ public class PacManager implements Runnable {
return false;
}
try {
- if (mProxyService.setPacFile(script) != NO_ERROR) {
- Log.e(TAG, "Unable to parse proxy script.");
- return false;
- }
+ mProxyService.setPacFile(script);
mCurrentPac = script;
} catch (RemoteException e) {
Log.e(TAG, "Unable to set PAC file", e);
}
return true;
}
+
+ private void bind() {
+ if (mContext == null) {
+ Log.e(TAG, "No context for binding");
+ return;
+ }
+ Intent intent = new Intent();
+ intent.setClassName(PROXY_PACKAGE, PROXY_SERVICE);
+ mConnection = new ServiceConnection() {
+ @Override
+ public void onServiceDisconnected(ComponentName component) {
+ synchronized (mProxyLock) {
+ mProxyService = null;
+ }
+ }
+
+ @Override
+ public void onServiceConnected(ComponentName component, IBinder binder) {
+ synchronized (mProxyLock) {
+ try {
+ Log.d(TAG, "Adding service " + PROXY_SERVICE_NAME + " "
+ + binder.getInterfaceDescriptor());
+ } catch (RemoteException e1) {
+ Log.e(TAG, "Remote Exception", e1);
+ }
+ ServiceManager.addService(PROXY_SERVICE_NAME, binder);
+ mProxyService = IProxyService.Stub.asInterface(binder);
+ if (mProxyService == null) {
+ Log.e(TAG, "No proxy service");
+ } else {
+ try {
+ mProxyService.startPacSystem();
+ } catch (RemoteException e) {
+ Log.e(TAG, "Unable to reach ProxyService - PAC will not be started", e);
+ }
+ IoThread.getHandler().post(mPacDownloader);
+ }
+ }
+ }
+ };
+ Log.e(TAG, "Attempting to bind");
+ mContext.bindService(intent, mConnection,
+ Context.BIND_AUTO_CREATE | Context.BIND_NOT_FOREGROUND | Context.BIND_NOT_VISIBLE);
+ }
+
+ private void unbind() {
+ mContext.unbindService(mConnection);
+ mConnection = null;
+ }
}