data_reduction_proxy_settings_android.h revision 4e180b6a0b4720a9b8e9e959a882386f690f08ff
1// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CHROME_BROWSER_NET_SPDYPROXY_DATA_REDUCTION_PROXY_SETTINGS_ANDROID_H_
6#define CHROME_BROWSER_NET_SPDYPROXY_DATA_REDUCTION_PROXY_SETTINGS_ANDROID_H_
7
8#include "base/android/jni_helper.h"
9#include "base/android/jni_string.h"
10#include "base/android/scoped_java_ref.h"
11#include "base/basictypes.h"
12#include "base/compiler_specific.h"
13#include "base/gtest_prod_util.h"
14#include "base/memory/scoped_ptr.h"
15#include "base/prefs/pref_member.h"
16#include "net/base/network_change_notifier.h"
17#include "net/url_request/url_fetcher_delegate.h"
18
19using base::android::ScopedJavaLocalRef;
20
21class PrefService;
22
23namespace net {
24class URLFetcher;
25}
26
27namespace spdyproxy {
28
29// The number of days of bandwidth usage statistics that are tracked.
30const unsigned int kNumDaysInHistory = 60;
31
32// The number of days of bandwidth usage statistics that are presented.
33const unsigned int kNumDaysInHistorySummary = 30;
34
35COMPILE_ASSERT(kNumDaysInHistorySummary <= kNumDaysInHistory,
36               DataReductionProxySettings_summary_too_long);
37
38}  // namespace spdyproxy
39
40// Central point for configuring the data reduction proxy on Android.
41// This object lives on the UI thread and all of its methods are expected to
42// be called from there.
43class DataReductionProxySettingsAndroid
44    : public net::URLFetcherDelegate,
45      public net::NetworkChangeNotifier::IPAddressObserver {
46 public:
47  DataReductionProxySettingsAndroid(JNIEnv* env, jobject obj);
48  virtual ~DataReductionProxySettingsAndroid();
49
50  void InitDataReductionProxySettings(JNIEnv* env, jobject obj);
51
52  // Add a host or URL pattern to bypass the proxy, respectively. Wildcards
53  // should be compatible with the JavaScript function shExpMatch, which can be
54  // used in proxy PAC resolution. These function must only be called before the
55  // proxy is used.
56  void BypassHostPattern(JNIEnv* env, jobject obj, jstring pattern);
57  void BypassURLPattern(JNIEnv* env, jobject obj, jstring pattern);
58
59  // Returns true if the data reduction proxy is allowed to be used on this
60  // instance of Chrome. This could return false, for example, if this instance
61  // is not part of the field trial, or if the proxy name is not configured
62  // via gyp.
63  jboolean IsDataReductionProxyAllowed(JNIEnv* env, jobject obj);
64
65  // Returns true if a screen promoting the data reduction proxy is allowed to
66  // be shown. Logic that decides when to show the promo should check its
67  // availability. This would return false if not part of a separate field
68  // trial that governs the use of the promotion.
69  jboolean IsDataReductionProxyPromoAllowed(JNIEnv* env, jobject obj);
70
71  // Returns the origin of the data reduction proxy.
72  ScopedJavaLocalRef<jstring> GetDataReductionProxyOrigin(JNIEnv* env,
73                                                          jobject obj);
74
75  // Returns a configuration string for the proxy.
76  ScopedJavaLocalRef<jstring> GetDataReductionProxyAuth(JNIEnv* env,
77                                                        jobject obj);
78
79  // Returns true if the proxy is enabled.
80  jboolean IsDataReductionProxyEnabled(JNIEnv* env, jobject obj);
81
82  // Returns true if the proxy is managed by an adminstrator's policy.
83  jboolean IsDataReductionProxyManaged(JNIEnv* env, jobject obj);
84
85  // Enables or disables the data reduction proxy. If a probe URL is available,
86  // and a probe request fails at some point, the proxy won't be used until a
87  // probe succeeds.
88  void SetDataReductionProxyEnabled(JNIEnv* env, jobject obj, jboolean enabled);
89
90  // Returns the time in microseconds that the last update was made to the
91  // daily original and received content lengths.
92  jlong GetDataReductionLastUpdateTime(JNIEnv* env, jobject obj);
93
94  // Return a Java |ContentLengths| object containing the total number of bytes
95  // of all received content, before and after compression by the data
96  // reduction proxy.
97  base::android::ScopedJavaLocalRef<jobject> GetContentLengths(JNIEnv* env,
98                                                               jobject obj);
99
100  // Returns an array containing the total size of all HTTP content that was
101  // received over the last |kNumDaysInHistory| before any compression by the
102  // data reduction proxy. Each element in the array contains one day of data.
103  ScopedJavaLocalRef<jlongArray> GetDailyOriginalContentLengths(JNIEnv* env,
104                                                                jobject obj);
105
106  // Returns an array containing the aggregate received HTTP content in the last
107  // |kNumDaysInHistory| days.
108  ScopedJavaLocalRef<jlongArray> GetDailyReceivedContentLengths(JNIEnv* env,
109                                                                jobject obj);
110
111  // Registers the native methods to be call from Java.
112  static bool Register(JNIEnv* env);
113
114  // net::URLFetcherDelegate:
115  virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
116
117 protected:
118  void InitPrefMembers();
119  virtual net::URLFetcher* GetURLFetcher();
120  virtual PrefService* GetOriginalProfilePrefs();
121  virtual PrefService* GetLocalStatePrefs();
122
123 private:
124  friend class DataReductionProxySettingsAndroidTest;
125  FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsAndroidTest,
126                           TestBypassRules);
127  FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsAndroidTest,
128                           TestResetDataReductionStatistics);
129  FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsAndroidTest,
130                           TestIsProxyEnabledOrManaged);
131  FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsAndroidTest,
132                           TestSetProxyPac);
133  FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsAndroidTest,
134                           TestGetDailyContentLengths);
135  FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsAndroidTest,
136                           TestContentLengthsInternal);
137  FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsAndroidTest,
138                           TestMaybeActivateDataReductionProxy);
139  FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsAndroidTest,
140                           TestOnIPAddressChanged);
141  FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsAndroidTest,
142                           TestOnProxyEnabledPrefChange);
143  FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsAndroidTest,
144                           TestInitDataReductionProxyOn);
145  FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsAndroidTest,
146                           TestInitDataReductionProxyOff);
147
148
149  // NetworkChangeNotifier::IPAddressObserver:
150  virtual void OnIPAddressChanged() OVERRIDE;
151
152  void OnProxyEnabledPrefChange();
153
154  void AddDefaultProxyBypassRules();
155  void AddURLPatternToBypass(const std::string& pattern);
156  void AddHostPatternToBypass(const std::string& pattern);
157  void AddPatternToBypass(const std::string& url_or_host,
158                          const std::string& pattern);
159  void AddHostToBypass(const std::string& host);
160
161  void ResetDataReductionStatistics();
162
163  void MaybeActivateDataReductionProxy(bool at_startup);
164  void SetProxyPac(bool enable_spdy_proxy, bool at_startup);
165
166  ScopedJavaLocalRef<jlongArray> GetDailyContentLengths(JNIEnv* env,
167                                                        const char* pref_name);
168  void GetContentLengthsInternal(unsigned int days,
169                                 int64* original_content_length,
170                                 int64* received_content_length,
171                                 int64* last_update_time);
172
173  // Requests the proxy probe URL, if one is set.  If unable to do so, disables
174  // the proxy, if enabled. Otherwise enables the proxy if disabled by a probe
175  // failure.
176  void ProbeWhetherDataReductionProxyIsAvailable();
177
178  std::string GetProxyPacScript();
179
180  std::vector<std::string> bypass_rules_;
181
182  // Indicate whether a user has turned on the data reduction proxy previously
183  // in this session.
184  bool has_turned_on_;
185
186  // Indicate whether a user has turned off the data reduction proxy previously
187  // in this session.
188  bool has_turned_off_;
189
190  bool disabled_by_carrier_;
191  bool enabled_by_user_;
192
193  scoped_ptr<net::URLFetcher> fetcher_;
194  BooleanPrefMember spdy_proxy_auth_enabled_;
195
196  DISALLOW_COPY_AND_ASSIGN(DataReductionProxySettingsAndroid);
197};
198
199#endif  // CHROME_BROWSER_NET_SPDYPROXY_DATA_REDUCTION_PROXY_SETTINGS_ANDROID_H_
200