forked from Pissandshittium/pissandshittium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose TrafficStats API to native code
This change exposes TrafficStats API to native code. Also, provides a pure virtual class that will be used on non Android platforms. Right now, only one API from TrafficStats is exposed. More APIs will be exposed in forthcoming changes. BUG=521646 Review URL: https://codereview.chromium.org/1286233003 Cr-Commit-Position: refs/heads/master@{#348646}
- Loading branch information
tbansal
authored and
Commit bot
committed
Sep 14, 2015
1 parent
f76ae11
commit 59a1ddc
Showing
9 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
29 changes: 29 additions & 0 deletions
29
net/android/java/src/org/chromium/net/AndroidTrafficStats.java
This file contains 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,29 @@ | ||
// Copyright 2015 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 org.chromium.net; | ||
|
||
import android.net.TrafficStats; | ||
|
||
import org.chromium.base.annotations.CalledByNative; | ||
import org.chromium.base.annotations.JNINamespace; | ||
|
||
/** | ||
* This class interacts with TrafficStats API provided by Android. | ||
*/ | ||
@JNINamespace("net::android::traffic_stats") | ||
public class AndroidTrafficStats { | ||
private AndroidTrafficStats() {} | ||
|
||
/** | ||
* @return Number of bytes transmitted since device boot. Counts packets across all network | ||
* interfaces, and always increases monotonically since device boot. Statistics are | ||
* measured at the network layer, so they include both TCP and UDP usage. | ||
*/ | ||
@CalledByNative | ||
private static long getTotalTxBytes() { | ||
long bytes = TrafficStats.getTotalTxBytes(); | ||
return bytes != TrafficStats.UNSUPPORTED ? bytes : TrafficStatsError.ERROR_NOT_SUPPORTED; | ||
} | ||
} |
This file contains 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
This file contains 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,36 @@ | ||
// Copyright 2015 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. | ||
|
||
#include "net/android/traffic_stats.h" | ||
|
||
#include "jni/AndroidTrafficStats_jni.h" | ||
|
||
namespace net { | ||
|
||
namespace android { | ||
|
||
namespace traffic_stats { | ||
|
||
// GENERATED_JAVA_ENUM_PACKAGE: org.chromium.net | ||
enum TrafficStatsError { | ||
// Value returned by AndroidTrafficStats APIs when a valid value is | ||
// unavailable. | ||
ERROR_NOT_SUPPORTED = 0, | ||
}; | ||
|
||
bool GetTotalTxBytes(int64_t* bytes) { | ||
JNIEnv* env = base::android::AttachCurrentThread(); | ||
*bytes = Java_AndroidTrafficStats_getTotalTxBytes(env); | ||
return *bytes != ERROR_NOT_SUPPORTED; | ||
} | ||
|
||
bool Register(JNIEnv* env) { | ||
return RegisterNativesImpl(env); | ||
} | ||
|
||
} // namespace traffic_stats | ||
|
||
} // namespace android | ||
|
||
} // namespace net |
This file contains 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,37 @@ | ||
// Copyright 2015 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. | ||
|
||
#ifndef NET_ANDROID_TRAFFIC_STATS_H_ | ||
#define NET_ANDROID_TRAFFIC_STATS_H_ | ||
|
||
// This file provides functions that interact with TrafficStats APIs that are | ||
// provided on Android. | ||
|
||
#include <jni.h> | ||
#include <stdint.h> | ||
|
||
#include "net/base/net_export.h" | ||
|
||
namespace net { | ||
|
||
namespace android { | ||
|
||
namespace traffic_stats { | ||
|
||
// Returns true if the number of bytes transmitted since device boot is | ||
// available and sets |*bytes| to that value. Counts packets across all network | ||
// interfaces, and always increases monotonically since device boot. | ||
// Statistics are measured at the network layer, so they include both TCP and | ||
// UDP usage. |bytes| must not be nullptr. | ||
NET_EXPORT bool GetTotalTxBytes(int64_t* bytes); | ||
|
||
bool Register(JNIEnv* env); | ||
|
||
} // namespace traffic_stats | ||
|
||
} // namespace android | ||
|
||
} // namespace net | ||
|
||
#endif // NET_ANDROID_TRAFFIC_STATS_H_ |
This file contains 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,44 @@ | ||
// Copyright 2015 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. | ||
|
||
#include "net/android/traffic_stats.h" | ||
|
||
#include "base/run_loop.h" | ||
#include "net/test/embedded_test_server/embedded_test_server.h" | ||
#include "net/url_request/url_request_test_util.h" | ||
#include "testing/gtest/include/gtest/gtest.h" | ||
#include "url/gurl.h" | ||
|
||
namespace net { | ||
|
||
namespace { | ||
|
||
TEST(TrafficStatsAndroidTest, BasicsTest) { | ||
test_server::EmbeddedTestServer embedded_test_server; | ||
embedded_test_server.ServeFilesFromDirectory( | ||
base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); | ||
ASSERT_TRUE(embedded_test_server.InitializeAndWaitUntilReady()); | ||
|
||
int64_t bytes_before_request = -1; | ||
EXPECT_TRUE(android::traffic_stats::GetTotalTxBytes(&bytes_before_request)); | ||
EXPECT_GE(bytes_before_request, 0); | ||
|
||
TestDelegate test_delegate; | ||
TestURLRequestContext context(false); | ||
|
||
scoped_ptr<URLRequest> request( | ||
context.CreateRequest(embedded_test_server.GetURL("/echo.html"), | ||
DEFAULT_PRIORITY, &test_delegate)); | ||
request->Start(); | ||
base::RunLoop().Run(); | ||
|
||
// Bytes should increase because of the network traffic. | ||
int64_t bytes_after_request; | ||
EXPECT_TRUE(android::traffic_stats::GetTotalTxBytes(&bytes_after_request)); | ||
DCHECK_GT(bytes_after_request, bytes_before_request); | ||
} | ||
|
||
} // namespace | ||
|
||
} // namespace net |
This file contains 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
This file contains 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