This repository was archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[firebase_performance] Java side of FirebasePerformance rewrite #1529
Closed
Closed
Changes from 7 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
66ada54
Java side of FirebasePerformance rewrite
bparrishMines f20ad54
Merge branch 'master' of github.com:flutter/plugins into perf_rewrite
bparrishMines cfa200c
Formatting and suppressing warnings
bparrishMines e3aa0e9
Update metric take individual parameters
bparrishMines 588c7fb
Unregister MethodCallHandler
bparrishMines 55d6999
Parse HttpMethod
bparrishMines 08d976f
Extra space
bparrishMines 353641e
Change method name to setMetric
bparrishMines c07c810
Redesign of flutter plugin objects
bparrishMines f9f05b7
Fix some method calls
bparrishMines 1a35bde
Move ConstantConditions and generic handler call
bparrishMines 2a3f0b2
attribute to name
bparrishMines File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
...roid/src/main/java/io/flutter/plugins/firebaseperformance/FlutterFirebasePerformance.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| // Copyright 2019 The Chromium Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| package io.flutter.plugins.firebaseperformance; | ||
|
|
||
| import com.google.firebase.perf.FirebasePerformance; | ||
| import io.flutter.plugin.common.BinaryMessenger; | ||
| import io.flutter.plugin.common.MethodCall; | ||
| import io.flutter.plugin.common.MethodChannel; | ||
|
|
||
| public class FlutterFirebasePerformance implements MethodChannel.MethodCallHandler { | ||
| private final BinaryMessenger binaryMessenger; | ||
| private final FirebasePerformance performance; | ||
|
|
||
| static FlutterFirebasePerformance getInstance(BinaryMessenger messenger) { | ||
| return new FlutterFirebasePerformance(messenger); | ||
| } | ||
|
|
||
| private FlutterFirebasePerformance(BinaryMessenger messenger) { | ||
| this.binaryMessenger = messenger; | ||
| this.performance = FirebasePerformance.getInstance(); | ||
| } | ||
|
|
||
| @Override | ||
| public void onMethodCall(MethodCall call, MethodChannel.Result result) { | ||
| switch (call.method) { | ||
| case "FirebasePerformance#isPerformanceCollectionEnabled": | ||
| isPerformanceCollectionEnabled(result); | ||
| break; | ||
| case "FirebasePerformance#setPerformanceCollectionEnabled": | ||
| setPerformanceCollectionEnabled(call, result); | ||
| break; | ||
| case "FirebasePerformance#newTrace": | ||
| newTrace(call, result); | ||
| break; | ||
| case "FirebasePerformance#newHttpMetric": | ||
| newHttpMetric(call, result); | ||
| break; | ||
| default: | ||
| result.notImplemented(); | ||
| } | ||
| } | ||
|
|
||
| private void isPerformanceCollectionEnabled(MethodChannel.Result result) { | ||
| result.success(performance.isPerformanceCollectionEnabled()); | ||
| } | ||
|
|
||
| private void setPerformanceCollectionEnabled(MethodCall call, MethodChannel.Result result) { | ||
| final boolean enabled = (Boolean) call.arguments; | ||
| performance.setPerformanceCollectionEnabled(enabled); | ||
| result.success(null); | ||
| } | ||
|
|
||
| private void newTrace(MethodCall call, MethodChannel.Result result) { | ||
| new FlutterTrace(performance, binaryMessenger, call, result); | ||
| } | ||
|
|
||
| private void newHttpMetric(MethodCall call, MethodChannel.Result result) { | ||
| new FlutterHttpMetric(performance, binaryMessenger, call, result); | ||
| } | ||
| } | ||
149 changes: 149 additions & 0 deletions
149
...mance/android/src/main/java/io/flutter/plugins/firebaseperformance/FlutterHttpMetric.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| // Copyright 2019 The Chromium Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| package io.flutter.plugins.firebaseperformance; | ||
|
|
||
| import com.google.firebase.perf.FirebasePerformance; | ||
| import com.google.firebase.perf.metrics.HttpMetric; | ||
| import io.flutter.plugin.common.BinaryMessenger; | ||
| import io.flutter.plugin.common.MethodCall; | ||
| import io.flutter.plugin.common.MethodChannel; | ||
|
|
||
| @SuppressWarnings("ConstantConditions") | ||
| public class FlutterHttpMetric implements MethodChannel.MethodCallHandler { | ||
| private static String parseHttpMethod(String httpMethod) { | ||
| switch (httpMethod) { | ||
| case "HttpMethod.Connect": | ||
| return FirebasePerformance.HttpMethod.CONNECT; | ||
| case "HttpMethod.Delete": | ||
| return FirebasePerformance.HttpMethod.DELETE; | ||
| case "HttpMethod.Get": | ||
| return FirebasePerformance.HttpMethod.GET; | ||
| case "HttpMethod.Head": | ||
| return FirebasePerformance.HttpMethod.HEAD; | ||
| case "HttpMethod.Options": | ||
| return FirebasePerformance.HttpMethod.OPTIONS; | ||
| case "HttpMethod.Patch": | ||
| return FirebasePerformance.HttpMethod.PATCH; | ||
| case "HttpMethod.Post": | ||
| return FirebasePerformance.HttpMethod.POST; | ||
| case "HttpMethod.Put": | ||
| return FirebasePerformance.HttpMethod.PUT; | ||
| case "HttpMethod.Trace": | ||
| return FirebasePerformance.HttpMethod.TRACE; | ||
| default: | ||
| throw new IllegalArgumentException(String.format("No HttpMethod for: %s", httpMethod)); | ||
| } | ||
| } | ||
|
|
||
| private final HttpMetric httpMetric; | ||
| private final MethodChannel channel; | ||
|
|
||
| FlutterHttpMetric( | ||
| FirebasePerformance performance, | ||
| BinaryMessenger messenger, | ||
| MethodCall call, | ||
| MethodChannel.Result result) { | ||
| final String channelName = call.argument("channelName"); | ||
| final String url = call.argument("url"); | ||
| final String httpMethod = call.argument("httpMethod"); | ||
|
|
||
| this.httpMetric = performance.newHttpMetric(url, parseHttpMethod(httpMethod)); | ||
|
|
||
| this.channel = new MethodChannel(messenger, channelName); | ||
| channel.setMethodCallHandler(this); | ||
|
|
||
| result.success(null); | ||
| } | ||
|
|
||
| @Override | ||
| public void onMethodCall(MethodCall call, MethodChannel.Result result) { | ||
| switch (call.method) { | ||
| case "HttpMetric#start": | ||
| start(result); | ||
| break; | ||
| case "HttpMetric#stop": | ||
| stop(result); | ||
| break; | ||
| case "HttpMetric#httpResponseCode": | ||
| setHttpResponseCode(call, result); | ||
| break; | ||
| case "HttpMetric#requestPayloadSize": | ||
| setRequestPayloadSize(call, result); | ||
| break; | ||
| case "HttpMetric#responseContentType": | ||
| setResponseContentType(call, result); | ||
| break; | ||
| case "HttpMetric#responsePayloadSize": | ||
| setResponsePayloadSize(call, result); | ||
| break; | ||
| case "PerformanceAttributes#putAttribute": | ||
| putAttribute(call, result); | ||
| break; | ||
| case "PerformanceAttributes#removeAttribute": | ||
| removeAttribute(call, result); | ||
| break; | ||
| case "PerformanceAttributes#getAttributes": | ||
| getAttributes(result); | ||
| break; | ||
| default: | ||
| result.notImplemented(); | ||
| } | ||
| } | ||
|
|
||
| private void start(MethodChannel.Result result) { | ||
| httpMetric.start(); | ||
| result.success(null); | ||
| } | ||
|
|
||
| private void stop(MethodChannel.Result result) { | ||
| httpMetric.stop(); | ||
| channel.setMethodCallHandler(null); | ||
| result.success(null); | ||
| } | ||
|
|
||
| private void setHttpResponseCode(MethodCall call, MethodChannel.Result result) { | ||
| final Integer httpResponseCode = call.argument("httpResponseCode"); | ||
| httpMetric.setHttpResponseCode(httpResponseCode); | ||
| result.success(null); | ||
| } | ||
|
|
||
| private void setRequestPayloadSize(MethodCall call, MethodChannel.Result result) { | ||
| final Number payloadSize = call.argument("requestPayloadSize"); | ||
| httpMetric.setRequestPayloadSize(payloadSize.longValue()); | ||
| result.success(null); | ||
| } | ||
|
|
||
| private void setResponseContentType(MethodCall call, MethodChannel.Result result) { | ||
| final String contentType = call.argument("responseContentType"); | ||
| httpMetric.setResponseContentType(contentType); | ||
| result.success(null); | ||
| } | ||
|
|
||
| private void setResponsePayloadSize(MethodCall call, MethodChannel.Result result) { | ||
| final Number payloadSize = call.argument("responsePayloadSize"); | ||
| httpMetric.setResponsePayloadSize(payloadSize.longValue()); | ||
| result.success(null); | ||
| } | ||
|
|
||
| private void putAttribute(MethodCall call, MethodChannel.Result result) { | ||
| final String attribute = call.argument("attribute"); | ||
| final String value = call.argument("value"); | ||
|
|
||
| httpMetric.putAttribute(attribute, value); | ||
|
|
||
| result.success(null); | ||
| } | ||
|
|
||
| private void removeAttribute(MethodCall call, MethodChannel.Result result) { | ||
| final String attribute = call.argument("attribute"); | ||
| httpMetric.removeAttribute(attribute); | ||
|
|
||
| result.success(null); | ||
| } | ||
|
|
||
| private void getAttributes(MethodChannel.Result result) { | ||
| result.success(httpMetric.getAttributes()); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like you split the plugin class into two parts which is different from what we're doing with other first-party plugins. Is this a new convention that you'd like to see applied to all of Flutterfire and plugins generally? If so we could file an issue to update the
flutter create -t pluginbehavior. Or if not, it might be better to leave it the way it is for consistency.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, I only separated this part as the FirebasePerformance plugin doesn't have any other classes that you can instantiate on there own. (e.g. to create an object you use
FirebasePeformance.newTrace()orFirebasePeformance.newHttpMetric(). ML Kit works the same way, but Firebase Dynamic Links doesn't.I created this PR to get feedback on creating separate
MethodChannels for each class. Although its require more code, i think it's easier to read/maintain.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm on board with splitting things into more classes, generally. I'm ok with using it for this plugin as a one-off and we can discuss offline if we want to adopt the approach for plugins generally.