Skip to content

Commit

Permalink
refactor(client/android): move fetching logic from TypeScript to Go (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jyyi1 authored Nov 4, 2024
1 parent b8ac24a commit 08678fd
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
import org.outline.vpn.Errors;
import org.outline.vpn.VpnServiceStarter;
import org.outline.vpn.VpnTunnelService;
import outline.Outline;
import outline.FetchResourceResult;
import platerrors.Platerrors;
import platerrors.PlatformError;

Expand All @@ -57,6 +59,7 @@ public enum Action {
STOP("stop"),
ON_STATUS_CHANGE("onStatusChange"),
IS_RUNNING("isRunning"),
FETCH_RESOURCE("fetchResource"),
INIT_ERROR_REPORTING("initializeErrorReporting"),
REPORT_EVENTS("reportEvents"),
QUIT("quitApplication");
Expand Down Expand Up @@ -202,6 +205,17 @@ private void executeAsync(
final String tunnelId = args.getString(0);
boolean isActive = isTunnelActive(tunnelId);
callback.sendPluginResult(new PluginResult(PluginResult.Status.OK, isActive));
} else if (Action.FETCH_RESOURCE.is(action)) {
final String url = args.getString(0);
LOG.fine(String.format(Locale.ROOT, "Fetching resource at %s ...", url));
final FetchResourceResult result = Outline.fetchResource(url);
if (result.getError() != null) {
LOG.warning(String.format(Locale.ROOT, "Fetch resource failed: %s", result.getError()));
sendActionResult(callback, result.getError());
} else {
LOG.info(String.format(Locale.ROOT, "Fetch resource result: %s", result.getContent()));
callback.success(result.getContent());
}

// Static actions
} else if (Action.INIT_ERROR_REPORTING.is(action)) {
Expand Down
4 changes: 4 additions & 0 deletions client/src/www/app/main.cordova.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {CordovaVpnApi} from './outline_server_repository/vpn.cordova';
import {OutlinePlatform} from './platform';
import {OUTLINE_PLUGIN_NAME, pluginExec} from './plugin.cordova';
import {BrowserResourceFetcher, ResourceFetcher} from './resource_fetcher';
import {CordovaResourceFetcher} from './resource_fetcher.cordova';
import {AbstractUpdater} from './updater';
import * as interceptors from './url_interceptor';
import {NoOpVpnInstaller, VpnInstaller} from './vpn_installer';
Expand Down Expand Up @@ -117,6 +118,9 @@ class CordovaPlatform implements OutlinePlatform {
}

getResourceFetcher(): ResourceFetcher {
if (cordova.platformId === 'android') {
return new CordovaResourceFetcher();
}
// TODO: move to Go fetch implementation later
return new BrowserResourceFetcher();
}
Expand Down
25 changes: 25 additions & 0 deletions client/src/www/app/resource_fetcher.cordova.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2024 The Outline Authors
//
// 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.

import {pluginExecWithErrorCode} from './plugin.cordova';
import {ResourceFetcher} from './resource_fetcher';

/**
* Fetches resources using Cordova plugin.
*/
export class CordovaResourceFetcher implements ResourceFetcher {
fetch(url: string): Promise<string> {
return pluginExecWithErrorCode<string>('fetchResource', url);
}
}

0 comments on commit 08678fd

Please sign in to comment.