1 package org.mozilla.geckoview.test; 2 3 import android.app.Service; 4 import android.content.Intent; 5 import android.os.IBinder; 6 import android.os.Message; 7 import android.os.Messenger; 8 import android.os.RemoteException; 9 10 import androidx.annotation.NonNull; 11 import androidx.annotation.Nullable; 12 13 import org.mozilla.geckoview.GeckoRuntime; 14 import org.mozilla.geckoview.GeckoRuntimeSettings; 15 import org.mozilla.geckoview.GeckoSession; 16 17 public class TestProfileLockService extends Service implements 18 GeckoSession.ProgressDelegate, 19 GeckoRuntime.Delegate { 20 public static final class p0 extends TestProfileLockService {} 21 public static final class p1 extends TestProfileLockService {} 22 23 // Used by the client to register themselves 24 public static final int MESSAGE_REGISTER = 1; 25 // Sent when the first page load completes 26 public static final int MESSAGE_PAGE_STOP = 2; 27 // Sent when GeckoRuntime exits 28 public static final int MESSAGE_QUIT = 3; 29 30 private GeckoRuntime mRuntime; 31 32 private Messenger mClient; 33 34 private class Handler extends android.os.Handler { 35 @Override handleMessage(@onNull final Message msg)36 public void handleMessage(@NonNull final Message msg) { 37 switch (msg.what) { 38 case MESSAGE_REGISTER: 39 mClient = msg.replyTo; 40 return; 41 default: 42 throw new IllegalStateException("Unknown message: " + msg.what); 43 } 44 } 45 } 46 47 final Messenger mMessenger = new Messenger(new Handler()); 48 49 @Override onShutdown()50 public void onShutdown() { 51 final Message message = Message.obtain(null, MESSAGE_QUIT); 52 try { 53 mClient.send(message); 54 } catch (final RemoteException ex) { 55 throw new RuntimeException(ex); 56 } 57 } 58 59 @Override onPageStop(@onNull final GeckoSession session, final boolean success)60 public void onPageStop(@NonNull final GeckoSession session, final boolean success) { 61 final Message message = Message.obtain(null, MESSAGE_PAGE_STOP); 62 try { 63 mClient.send(message); 64 } catch (final RemoteException ex) { 65 throw new RuntimeException(ex); 66 } 67 } 68 69 @Override onDestroy()70 public void onDestroy() { 71 // Sometimes the service doesn't die on it's own so we need to kill it here. 72 System.exit(0); 73 } 74 75 @Nullable 76 @Override onBind(final Intent intent)77 public IBinder onBind(final Intent intent) { 78 // Request to be killed as soon as the client unbinds. 79 stopSelf(); 80 81 if (mRuntime != null) { 82 // We only expect one client 83 throw new RuntimeException("Multiple clients !?"); 84 } 85 86 final GeckoRuntimeSettings settings = new GeckoRuntimeSettings.Builder() 87 .extras(intent.getExtras()) 88 .build(); 89 mRuntime = GeckoRuntime.create(getApplicationContext(), settings); 90 91 mRuntime.setDelegate(this); 92 93 final GeckoSession session = new GeckoSession(); 94 session.setProgressDelegate(this); 95 session.open(mRuntime); 96 97 return mMessenger.getBinder(); 98 } 99 } 100