@Override public void close() throws IOException { try { long bytesRead = 0; if (!mEofSeen) { byte[] buffer = new byte[BUFFER_SIZE]; int count; while ((count = this.read(buffer)) != -1) { bytesRead += count; } } if (bytesRead > 0) { CLog.writeToConsole( mNetworkPeerManager, Console.MessageLevel.ERROR, Console.MessageSource.NETWORK, "There were " + String.valueOf(bytesRead) + " bytes that were not consumed while " + "processing request " + mRequestId); } } finally { super.close(); closeOutputStreamQuietly(); } }
/** * Attempts to close all the output stream, and swallows any exceptions. */ private synchronized void closeOutputStreamQuietly() { if (!mClosed) { try { mOutputStream.close(); reportDecodedSizeIfApplicable(); } catch (IOException e) { CLog.writeToConsole( mNetworkPeerManager, Console.MessageLevel.ERROR, Console.MessageSource.NETWORK, "Could not close the output stream" + e); } finally { mClosed = true; } } }
@Nullable private static String readBodyAsString( NetworkPeerManager peerManager, InspectorRequest request) { try { byte[] body = request.body(); if (body != null) { return new String(body, Utf8Charset.INSTANCE); } } catch (IOException | OutOfMemoryError e) { CLog.writeToConsole( peerManager, Console.MessageLevel.WARNING, Console.MessageSource.NETWORK, "Could not reproduce POST body: " + e); } return null; }
public static void writeToConsole( ChromePeerManager chromePeerManager, Console.MessageLevel logLevel, Console.MessageSource messageSource, String messageText) { // Send to logcat to increase the chances that a developer will notice :) LogRedirector.d(TAG, messageText); Console.ConsoleMessage message = new Console.ConsoleMessage(); message.source = messageSource; message.level = logLevel; message.text = messageText; Console.MessageAddedRequest messageAddedRequest = new Console.MessageAddedRequest(); messageAddedRequest.message = message; chromePeerManager.sendNotificationToPeers("Console.messageAdded", messageAddedRequest); }
@Test public void testReadOneByte() throws IOException { int result = mResponseHandlingInputStream.read(); assertEquals(TEST_RESPONSE_BODY[0], positionToByte(result)); assertBufferMatchesResponseBody(mTestOutputStream.toByteArray(), 1); PowerMockito.mockStatic(CLog.class); PowerMockito.doNothing().when(CLog.class); CLog.writeToConsole( Mockito.any(ChromePeerManager.class), Mockito.any(Console.MessageLevel.class), Mockito.any(Console.MessageSource.class), Mockito.anyString()); mResponseHandlingInputStream.close(); PowerMockito.verifyStatic(); }
@Test public void testReadPartial() throws IOException { int numBytesToRead = TEST_RESPONSE_BODY.length / 2; byte[] tempReadingBuffer = new byte[numBytesToRead]; int result = mResponseHandlingInputStream.read(tempReadingBuffer, 0, numBytesToRead); assertEquals(numBytesToRead, result); assertBufferMatchesResponseBody(tempReadingBuffer, numBytesToRead); assertBufferMatchesResponseBody(mTestOutputStream.toByteArray(), numBytesToRead); PowerMockito.mockStatic(CLog.class); PowerMockito.doNothing().when(CLog.class); CLog.writeToConsole( Mockito.any(ChromePeerManager.class), Mockito.any(Console.MessageLevel.class), Mockito.any(Console.MessageSource.class), Mockito.anyString()); mResponseHandlingInputStream.close(); PowerMockito.verifyStatic(); }
@Test public void testSkipFew() throws IOException { long numBytesToSkip = TEST_RESPONSE_BODY.length / 2; long result = mResponseHandlingInputStream.skip(numBytesToSkip); assertEquals(numBytesToSkip, result); assertBufferMatchesResponseBody(mTestOutputStream.toByteArray(), (int) numBytesToSkip); PowerMockito.mockStatic(CLog.class); PowerMockito.doNothing().when(CLog.class); CLog.writeToConsole( Mockito.any(ChromePeerManager.class), Mockito.any(Console.MessageLevel.class), Mockito.any(Console.MessageSource.class), Mockito.anyString()); mResponseHandlingInputStream.close(); PowerMockito.verifyStatic(); }
private @NonNull ScriptableObject initJsScope(@NonNull Context jsContext) { // Set the main Rhino goodies ImporterTopLevel importerTopLevel = new ImporterTopLevel(jsContext); ScriptableObject scope = jsContext.initStandardObjects(importerTopLevel, false); ScriptableObject.putProperty(scope, "context", Context.javaToJS(mContext, scope)); try { importClasses(jsContext, scope); importPackages(jsContext, scope); importConsole(scope); importVariables(scope); importFunctions(scope); } catch (StethoJsException e) { String message = String.format("%s\n%s", e.getMessage(), Log.getStackTraceString(e)); LogUtil.e(e, message); CLog.writeToConsole(Console.MessageLevel.ERROR, Console.MessageSource.JAVASCRIPT, message); } return scope; }
Database.ExecuteSQLResponse executeSQL(String databaseId, String query) throws JSONException { Timber.d("executeSQL: %s, %s", databaseId, query); Database.ExecuteSQLResponse response = new Database.ExecuteSQLResponse(); Matcher matcher = mPattern.matcher(query); if (!matcher.find()) { return response; } String docId = matcher.group(1); Timber.d("Parsed doc ID: %s", docId); Map<String, String> map = getDocument(databaseId, docId); response.columnNames = COLUMN_NAMES; response.values = new ArrayList<>(); for (Map.Entry<String, String> entry : map.entrySet()) { final String key = entry.getKey(); if (!mShowMetadata && key.substring(0,1).equals("_")) { continue; } response.values.add(key); response.values.add(entry.getValue()); } // Log to console CLog.writeToConsole(Console.MessageLevel.DEBUG, Console.MessageSource.JAVASCRIPT, new JSONObject(map).toString(4)); return response; }
private void handleIOExceptionWritingToStream(IOException e) { CLog.writeToConsole( mNetworkPeerManager, Console.MessageLevel.ERROR, Console.MessageSource.NETWORK, "Could not write response body to the stream " + e); closeOutputStreamQuietly(); }
@Override public void requestWillBeSent(InspectorRequest request) { NetworkPeerManager peerManager = getPeerManagerIfEnabled(); if (peerManager != null) { Network.Request requestJSON = new Network.Request(); requestJSON.url = request.url(); requestJSON.method = request.method(); requestJSON.headers = formatHeadersAsJSON(request); requestJSON.postData = readBodyAsString(peerManager, request); // Hack to use the initiator of SCRIPT to generate a fake call stack that includes // the request's "friendly" name. String requestFriendlyName = request.friendlyName(); Integer requestPriority = request.friendlyNameExtra(); Network.Initiator initiatorJSON = new Network.Initiator(); initiatorJSON.type = Network.InitiatorType.SCRIPT; initiatorJSON.stackTrace = new ArrayList<Console.CallFrame>(); initiatorJSON.stackTrace.add(new Console.CallFrame(requestFriendlyName, requestFriendlyName, requestPriority != null ? requestPriority : 0 /* lineNumber */, 0 /* columnNumber */)); Network.RequestWillBeSentParams params = new Network.RequestWillBeSentParams(); params.requestId = request.id(); params.frameId = "1"; params.loaderId = "1"; params.documentURL = request.url(); params.request = requestJSON; params.timestamp = stethoNow() / 1000.0; params.initiator = initiatorJSON; params.redirectResponse = null; // Type is now required as of at least WebKit Inspector rev @188492. If you don't send // it, Chrome will refuse to draw the row in the Network tab until the response is // received (providing the type). This delay is very noticable on slow networks. params.type = Page.ResourceType.OTHER; peerManager.sendNotificationToPeers("Network.requestWillBeSent", params); } }
public static void writeToConsole( Console.MessageLevel logLevel, Console.MessageSource messageSource, String messageText ) { ConsolePeerManager peerManager = ConsolePeerManager.getInstanceOrNull(); if (peerManager == null) { return; } writeToConsole(peerManager, logLevel, messageSource, messageText); }
@Override protected void log(int priority, String tag, String message, Throwable t) { ConsolePeerManager peerManager = ConsolePeerManager.getInstanceOrNull(); if (peerManager == null) { return; } Console.MessageLevel logLevel; switch (priority) { case Log.VERBOSE: case Log.DEBUG: logLevel = Console.MessageLevel.DEBUG; break; case Log.INFO: logLevel = Console.MessageLevel.LOG; break; case Log.WARN: logLevel = Console.MessageLevel.WARNING; break; case Log.ERROR: case Log.ASSERT: logLevel = Console.MessageLevel.ERROR; break; default: logLevel = Console.MessageLevel.LOG; } CLog.writeToConsole( logLevel, Console.MessageSource.OTHER, message ); }
public static void init(Gson gsonInstance) { CLog.writeToConsole(Console.MessageLevel.LOG, Console.MessageSource.OTHER, "TaskLibStetho.init"); TaskLibStetho.gsonInstance = gsonInstance; TaskEventListener.setInstance(new StethoEventListener()); }
@Override protected void onExecuteCalled(BaseTask task, Object sourceAndTarget) { CLog.writeToConsole(Console.MessageLevel.LOG, Console.MessageSource.OTHER, "onExecuteCalled"); bridge.reportTaskExecuteCalled(task, sourceAndTarget); }
@Override protected void onResult(BaseTask task) { CLog.writeToConsole(Console.MessageLevel.LOG, Console.MessageSource.OTHER, "onResult"); bridge.reportTaskFinished(task); }
public Iterable<ChromeDevtoolsDomain> finish() { provideIfDesired(new Console()); provideIfDesired(new Debugger()); DocumentProviderFactory documentModel = resolveDocumentProvider(); if (documentModel != null) { Document document = new Document(documentModel); provideIfDesired(new DOM(document)); provideIfDesired(new CSS(document)); } provideIfDesired(new DOMStorage(mContext)); provideIfDesired(new HeapProfiler()); provideIfDesired(new Inspector()); provideIfDesired(new Network(mContext)); provideIfDesired(new Page(mContext)); provideIfDesired(new Profiler()); provideIfDesired( new Runtime( mRuntimeRepl != null ? mRuntimeRepl : new RhinoDetectingRuntimeReplFactory(mContext))); provideIfDesired(new Worker()); if (Build.VERSION.SDK_INT >= DatabaseConstants.MIN_API_LEVEL) { Database database = new Database(); boolean hasSqliteDatabaseDriver = false; if (mDatabaseDrivers != null) { for (DatabaseDriver2 databaseDriver : mDatabaseDrivers) { database.add(databaseDriver); if (databaseDriver instanceof SqliteDatabaseDriver) { hasSqliteDatabaseDriver = true; } } } if (!hasSqliteDatabaseDriver && !mExcludeSqliteDatabaseDriver) { database.add( new SqliteDatabaseDriver(mContext, mDatabaseFilesProvider != null ? mDatabaseFilesProvider : new DefaultDatabaseFilesProvider(mContext), new DefaultDatabaseConnectionProvider())); } provideIfDesired(database); } return mDelegate.finish(); }
@Override public InputStream interpretResponseStream( String requestId, @Nullable String contentType, @Nullable String contentEncoding, @Nullable InputStream availableInputStream, ResponseHandler responseHandler) { NetworkPeerManager peerManager = getPeerManagerIfEnabled(); if (peerManager != null) { if (availableInputStream == null) { responseHandler.onEOF(); return null; } Page.ResourceType resourceType = contentType != null ? getResourceTypeHelper().determineResourceType(contentType) : null; // There's this weird logic at play that only knows how to base64 decode certain kinds of // resources. boolean base64Encode = false; if (resourceType != null && resourceType == Page.ResourceType.IMAGE) { base64Encode = true; } try { OutputStream fileOutputStream = peerManager.getResponseBodyFileManager().openResponseBodyFile( requestId, base64Encode); return DecompressionHelper.teeInputWithDecompression( peerManager, requestId, availableInputStream, fileOutputStream, contentEncoding, responseHandler); } catch (IOException e) { CLog.writeToConsole( peerManager, Console.MessageLevel.ERROR, Console.MessageSource.NETWORK, "Error writing response body data for request #" + requestId); } } return availableInputStream; }