|
| 1 | +/** |
| 2 | + * Copyright (c) 2016-present, Facebook, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + */ |
| 9 | + |
| 10 | +package com.facebook.react.packagerconnection; |
| 11 | + |
| 12 | +import javax.annotation.Nullable; |
| 13 | + |
| 14 | +import java.io.FileInputStream; |
| 15 | +import java.io.FileNotFoundException; |
| 16 | +import java.io.IOException; |
| 17 | +import java.util.HashMap; |
| 18 | +import java.util.Iterator; |
| 19 | +import java.util.Map; |
| 20 | + |
| 21 | +import android.os.Handler; |
| 22 | +import android.os.Looper; |
| 23 | +import android.util.Base64; |
| 24 | + |
| 25 | +import com.facebook.common.logging.FLog; |
| 26 | + |
| 27 | +import org.json.JSONObject; |
| 28 | + |
| 29 | +public class FileIoHandler implements Runnable { |
| 30 | + private static final String TAG = JSPackagerClient.class.getSimpleName(); |
| 31 | + private static final long FILE_TTL = 30 * 1000; |
| 32 | + |
| 33 | + private static class TtlFileInputStream { |
| 34 | + private final FileInputStream mStream; |
| 35 | + private long mTtl; |
| 36 | + |
| 37 | + public TtlFileInputStream(String path) throws FileNotFoundException { |
| 38 | + mStream = new FileInputStream(path); |
| 39 | + mTtl = System.currentTimeMillis() + FILE_TTL; |
| 40 | + } |
| 41 | + |
| 42 | + private void extendTtl() { |
| 43 | + mTtl = System.currentTimeMillis() + FILE_TTL; |
| 44 | + } |
| 45 | + |
| 46 | + public boolean expiredTtl() { |
| 47 | + return System.currentTimeMillis() >= mTtl; |
| 48 | + } |
| 49 | + |
| 50 | + public String read(int size) throws IOException { |
| 51 | + extendTtl(); |
| 52 | + byte[] buffer = new byte[size]; |
| 53 | + int bytesRead = mStream.read(buffer); |
| 54 | + return Base64.encodeToString(buffer, 0, bytesRead, Base64.DEFAULT); |
| 55 | + } |
| 56 | + |
| 57 | + public void close() throws IOException { |
| 58 | + mStream.close(); |
| 59 | + } |
| 60 | + }; |
| 61 | + |
| 62 | + private int mNextHandle; |
| 63 | + private final Handler mHandler; |
| 64 | + private final Map<Integer, TtlFileInputStream> mOpenFiles; |
| 65 | + private final Map<String, JSPackagerClient.RequestHandler> mRequestHandlers; |
| 66 | + |
| 67 | + public FileIoHandler() { |
| 68 | + mNextHandle = 1; |
| 69 | + mHandler = new Handler(Looper.getMainLooper()); |
| 70 | + mOpenFiles = new HashMap<>(); |
| 71 | + mRequestHandlers = new HashMap<>(); |
| 72 | + mRequestHandlers.put("fopen", new JSPackagerClient.RequestOnlyHandler() { |
| 73 | + @Override |
| 74 | + public void onRequest( |
| 75 | + @Nullable Object params, JSPackagerClient.Responder responder) { |
| 76 | + synchronized (mOpenFiles) { |
| 77 | + try { |
| 78 | + JSONObject paramsObj = (JSONObject)params; |
| 79 | + if (paramsObj == null) { |
| 80 | + throw new Exception("params must be an object { mode: string, filename: string }"); |
| 81 | + } |
| 82 | + String mode = paramsObj.optString("mode"); |
| 83 | + if (mode == null) { |
| 84 | + throw new Exception("missing params.mode"); |
| 85 | + } |
| 86 | + String filename = paramsObj.optString("filename"); |
| 87 | + if (filename == null) { |
| 88 | + throw new Exception("missing params.filename"); |
| 89 | + } |
| 90 | + if (!mode.equals("r")) { |
| 91 | + throw new IllegalArgumentException("unsupported mode: " + mode); |
| 92 | + } |
| 93 | + |
| 94 | + responder.respond(addOpenFile(filename)); |
| 95 | + } catch (Exception e) { |
| 96 | + responder.error(e.toString()); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + }); |
| 101 | + mRequestHandlers.put("fclose", new JSPackagerClient.RequestOnlyHandler() { |
| 102 | + @Override |
| 103 | + public void onRequest( |
| 104 | + @Nullable Object params, JSPackagerClient.Responder responder) { |
| 105 | + synchronized (mOpenFiles) { |
| 106 | + try { |
| 107 | + if (!(params instanceof Number)) { |
| 108 | + throw new Exception("params must be a file handle"); |
| 109 | + } |
| 110 | + TtlFileInputStream stream = mOpenFiles.get((int)params); |
| 111 | + if (stream == null) { |
| 112 | + throw new Exception("invalid file handle, it might have timed out"); |
| 113 | + } |
| 114 | + |
| 115 | + mOpenFiles.remove((int)params); |
| 116 | + stream.close(); |
| 117 | + responder.respond(""); |
| 118 | + } catch (Exception e) { |
| 119 | + responder.error(e.toString()); |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + }); |
| 124 | + mRequestHandlers.put("fread", new JSPackagerClient.RequestOnlyHandler() { |
| 125 | + @Override |
| 126 | + public void onRequest( |
| 127 | + @Nullable Object params, JSPackagerClient.Responder responder) { |
| 128 | + synchronized (mOpenFiles) { |
| 129 | + try { |
| 130 | + JSONObject paramsObj = (JSONObject)params; |
| 131 | + if (paramsObj == null) { |
| 132 | + throw new Exception("params must be an object { file: handle, size: number }"); |
| 133 | + } |
| 134 | + int file = paramsObj.optInt("file"); |
| 135 | + if (file == 0) { |
| 136 | + throw new Exception("invalid or missing file handle"); |
| 137 | + } |
| 138 | + int size = paramsObj.optInt("size"); |
| 139 | + if (size == 0) { |
| 140 | + throw new Exception("invalid or missing read size"); |
| 141 | + } |
| 142 | + TtlFileInputStream stream = mOpenFiles.get(file); |
| 143 | + if (stream == null) { |
| 144 | + throw new Exception("invalid file handle, it might have timed out"); |
| 145 | + } |
| 146 | + |
| 147 | + responder.respond(stream.read(size)); |
| 148 | + } catch (Exception e) { |
| 149 | + responder.error(e.toString()); |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + }); |
| 154 | + } |
| 155 | + |
| 156 | + public Map<String, JSPackagerClient.RequestHandler> handlers() { |
| 157 | + return mRequestHandlers; |
| 158 | + } |
| 159 | + |
| 160 | + private int addOpenFile(String filename) throws FileNotFoundException { |
| 161 | + int handle = mNextHandle++; |
| 162 | + mOpenFiles.put(handle, new TtlFileInputStream(filename)); |
| 163 | + if (mOpenFiles.size() == 1) { |
| 164 | + mHandler.postDelayed(FileIoHandler.this, FILE_TTL); |
| 165 | + } |
| 166 | + return handle; |
| 167 | + } |
| 168 | + |
| 169 | + @Override |
| 170 | + public void run() { |
| 171 | + // clean up files that are past their expiry date |
| 172 | + synchronized (mOpenFiles) { |
| 173 | + Iterator<TtlFileInputStream> i = mOpenFiles.values().iterator(); |
| 174 | + while (i.hasNext()) { |
| 175 | + TtlFileInputStream stream = i.next(); |
| 176 | + if (stream.expiredTtl()) { |
| 177 | + i.remove(); |
| 178 | + try { |
| 179 | + stream.close(); |
| 180 | + } catch (IOException e) { |
| 181 | + FLog.e( |
| 182 | + TAG, |
| 183 | + "closing expired file failed: " + e.toString()); |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | + if (!mOpenFiles.isEmpty()) { |
| 188 | + mHandler.postDelayed(this, FILE_TTL); |
| 189 | + } |
| 190 | + } |
| 191 | + } |
| 192 | +} |
0 commit comments