summaryrefslogtreecommitdiffstats
path: root/mobile/android/test_runner/src/main/java/org/mozilla/geckoview/test_runner/XpcshellTestRunnerService.java
blob: 529b740c28ea445f781f344da07ebf7fc67940cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */

package org.mozilla.geckoview.test_runner;

import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import androidx.annotation.Nullable;
import java.util.HashMap;
import org.mozilla.geckoview.ContentBlocking;
import org.mozilla.geckoview.GeckoResult;
import org.mozilla.geckoview.GeckoRuntime;
import org.mozilla.geckoview.GeckoRuntimeSettings;
import org.mozilla.geckoview.WebExtension;
import org.mozilla.geckoview.WebExtensionController;

public class XpcshellTestRunnerService extends Service {
  public static final class i0 extends XpcshellTestRunnerService {}

  public static final class i1 extends XpcshellTestRunnerService {}

  public static final class i2 extends XpcshellTestRunnerService {}

  public static final class i3 extends XpcshellTestRunnerService {}

  public static final class i4 extends XpcshellTestRunnerService {}

  public static final class i5 extends XpcshellTestRunnerService {}

  public static final class i6 extends XpcshellTestRunnerService {}

  public static final class i7 extends XpcshellTestRunnerService {}

  public static final class i8 extends XpcshellTestRunnerService {}

  public static final class i9 extends XpcshellTestRunnerService {}

  private static final String LOGTAG = "XpcshellTestRunner";
  static GeckoRuntime sRuntime;

  private HashMap<String, WebExtension> mExtensions = new HashMap<>();

  private static WebExtensionController webExtensionController() {
    return sRuntime.getWebExtensionController();
  }

  @Override
  public synchronized int onStartCommand(Intent intent, int flags, int startId) {
    if (sRuntime != null) {
      // We don't support restarting GeckoRuntime
      throw new RuntimeException("Cannot start more than once");
    }

    final Bundle extras = intent.getExtras();
    for (final String key : extras.keySet()) {
      Log.i(LOGTAG, "Got extras " + key + "=" + extras.get(key));
    }

    final ContentBlocking.SafeBrowsingProvider googleLegacy =
        ContentBlocking.SafeBrowsingProvider.from(
                ContentBlocking.GOOGLE_LEGACY_SAFE_BROWSING_PROVIDER)
            .getHashUrl("http://mochi.test:8888/safebrowsing-dummy/gethash")
            .updateUrl("http://mochi.test:8888/safebrowsing-dummy/update")
            .build();

    final ContentBlocking.SafeBrowsingProvider google =
        ContentBlocking.SafeBrowsingProvider.from(ContentBlocking.GOOGLE_SAFE_BROWSING_PROVIDER)
            .getHashUrl("http://mochi.test:8888/safebrowsing4-dummy/gethash")
            .updateUrl("http://mochi.test:8888/safebrowsing4-dummy/update")
            .build();

    final GeckoRuntimeSettings runtimeSettings =
        new GeckoRuntimeSettings.Builder()
            .arguments(new String[] {"-xpcshell"})
            .extras(extras)
            .consoleOutput(true)
            .contentBlocking(
                new ContentBlocking.Settings.Builder()
                    .safeBrowsingProviders(google, googleLegacy)
                    .build())
            .build();

    sRuntime = GeckoRuntime.create(this, runtimeSettings);

    webExtensionController()
        .setDebuggerDelegate(
            new WebExtensionController.DebuggerDelegate() {
              @Override
              public void onExtensionListUpdated() {
                refreshExtensionList();
              }
            });

    webExtensionController()
        .setAddonManagerDelegate(new WebExtensionController.AddonManagerDelegate() {});

    sRuntime.setDelegate(
        () -> {
          stopSelf();
          System.exit(0);
        });

    return Service.START_NOT_STICKY;
  }

  // Random start timestamp for the BrowsingDataDelegate API.
  private static final int CLEAR_DATA_START_TIMESTAMP = 1234;

  private void refreshExtensionList() {
    webExtensionController()
        .list()
        .accept(
            extensions -> {
              mExtensions.clear();
              for (final WebExtension extension : extensions) {
                mExtensions.put(extension.id, extension);

                extension.setBrowsingDataDelegate(
                    new WebExtension.BrowsingDataDelegate() {
                      @Nullable
                      @Override
                      public GeckoResult<Settings> onGetSettings() {
                        final long types =
                            Type.CACHE
                                | Type.COOKIES
                                | Type.HISTORY
                                | Type.FORM_DATA
                                | Type.DOWNLOADS;
                        return GeckoResult.fromValue(
                            new Settings(CLEAR_DATA_START_TIMESTAMP, types, types));
                      }
                    });
              }
            });
  }

  @Override
  public synchronized IBinder onBind(Intent intent) {
    return null;
  }
}