1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License
15  */
16 
17 package com.android.server.backup;
18 
19 import static com.android.server.backup.BackupManagerService.DEBUG_SCHEDULING;
20 
21 import android.content.ContentResolver;
22 import android.os.Handler;
23 import android.provider.Settings;
24 import android.util.KeyValueListParser;
25 import android.util.KeyValueSettingObserver;
26 import android.util.Slog;
27 
28 import com.android.internal.annotations.GuardedBy;
29 import com.android.internal.annotations.VisibleForTesting;
30 
31 /**
32  * Configure backup and restore agent timeouts.
33  *
34  * <p>These timeout parameters are stored in Settings.Global to be configurable flags with P/H. They
35  * are represented as a comma-delimited key value list.
36  */
37 public class BackupAgentTimeoutParameters extends KeyValueSettingObserver {
38     private static final String TAG = "BackupAgentTimeout";
39 
40     @VisibleForTesting
41     public static final String SETTING = Settings.Global.BACKUP_AGENT_TIMEOUT_PARAMETERS;
42 
43     @VisibleForTesting
44     public static final String SETTING_KV_BACKUP_AGENT_TIMEOUT_MILLIS =
45             "kv_backup_agent_timeout_millis";
46 
47     @VisibleForTesting
48     public static final String SETTING_FULL_BACKUP_AGENT_TIMEOUT_MILLIS =
49             "full_backup_agent_timeout_millis";
50 
51     @VisibleForTesting
52     public static final String SETTING_SHARED_BACKUP_AGENT_TIMEOUT_MILLIS =
53             "shared_backup_agent_timeout_millis";
54 
55     @VisibleForTesting
56     public static final String SETTING_RESTORE_AGENT_TIMEOUT_MILLIS =
57             "restore_agent_timeout_millis";
58 
59     @VisibleForTesting
60     public static final String SETTING_RESTORE_AGENT_FINISHED_TIMEOUT_MILLIS =
61             "restore_agent_finished_timeout_millis";
62 
63     @VisibleForTesting
64     public static final String SETTING_QUOTA_EXCEEDED_TIMEOUT_MILLIS =
65             "quota_exceeded_timeout_millis";
66 
67     // Default values
68     @VisibleForTesting public static final long DEFAULT_KV_BACKUP_AGENT_TIMEOUT_MILLIS = 30 * 1000;
69 
70     @VisibleForTesting
71     public static final long DEFAULT_FULL_BACKUP_AGENT_TIMEOUT_MILLIS = 5 * 60 * 1000;
72 
73     @VisibleForTesting
74     public static final long DEFAULT_SHARED_BACKUP_AGENT_TIMEOUT_MILLIS = 30 * 60 * 1000;
75 
76     @VisibleForTesting public static final long DEFAULT_RESTORE_AGENT_TIMEOUT_MILLIS = 60 * 1000;
77 
78     @VisibleForTesting
79     public static final long DEFAULT_RESTORE_AGENT_FINISHED_TIMEOUT_MILLIS = 30 * 1000;
80 
81     @VisibleForTesting
82     public static final long DEFAULT_QUOTA_EXCEEDED_TIMEOUT_MILLIS = 3 * 1000;
83 
84     @GuardedBy("mLock")
85     private long mKvBackupAgentTimeoutMillis;
86 
87     @GuardedBy("mLock")
88     private long mFullBackupAgentTimeoutMillis;
89 
90     @GuardedBy("mLock")
91     private long mSharedBackupAgentTimeoutMillis;
92 
93     @GuardedBy("mLock")
94     private long mRestoreAgentTimeoutMillis;
95 
96     @GuardedBy("mLock")
97     private long mRestoreAgentFinishedTimeoutMillis;
98 
99     @GuardedBy("mLock")
100     private long mQuotaExceededTimeoutMillis;
101 
102     private final Object mLock = new Object();
103 
BackupAgentTimeoutParameters(Handler handler, ContentResolver resolver)104     public BackupAgentTimeoutParameters(Handler handler, ContentResolver resolver) {
105         super(handler, resolver, Settings.Global.getUriFor(SETTING));
106     }
107 
getSettingValue(ContentResolver resolver)108     public String getSettingValue(ContentResolver resolver) {
109         return Settings.Global.getString(resolver, SETTING);
110     }
111 
update(KeyValueListParser parser)112     public void update(KeyValueListParser parser) {
113         synchronized (mLock) {
114             mKvBackupAgentTimeoutMillis =
115                     parser.getLong(
116                             SETTING_KV_BACKUP_AGENT_TIMEOUT_MILLIS,
117                             DEFAULT_KV_BACKUP_AGENT_TIMEOUT_MILLIS);
118             mFullBackupAgentTimeoutMillis =
119                     parser.getLong(
120                             SETTING_FULL_BACKUP_AGENT_TIMEOUT_MILLIS,
121                             DEFAULT_FULL_BACKUP_AGENT_TIMEOUT_MILLIS);
122             mSharedBackupAgentTimeoutMillis =
123                     parser.getLong(
124                             SETTING_SHARED_BACKUP_AGENT_TIMEOUT_MILLIS,
125                             DEFAULT_SHARED_BACKUP_AGENT_TIMEOUT_MILLIS);
126             mRestoreAgentTimeoutMillis =
127                     parser.getLong(
128                             SETTING_RESTORE_AGENT_TIMEOUT_MILLIS,
129                             DEFAULT_RESTORE_AGENT_TIMEOUT_MILLIS);
130             mRestoreAgentFinishedTimeoutMillis =
131                     parser.getLong(
132                             SETTING_RESTORE_AGENT_FINISHED_TIMEOUT_MILLIS,
133                             DEFAULT_RESTORE_AGENT_FINISHED_TIMEOUT_MILLIS);
134             mQuotaExceededTimeoutMillis =
135                     parser.getLong(
136                             SETTING_QUOTA_EXCEEDED_TIMEOUT_MILLIS,
137                             DEFAULT_QUOTA_EXCEEDED_TIMEOUT_MILLIS);
138         }
139     }
140 
getKvBackupAgentTimeoutMillis()141     public long getKvBackupAgentTimeoutMillis() {
142         synchronized (mLock) {
143             if (DEBUG_SCHEDULING) {
144                 Slog.v(TAG, "getKvBackupAgentTimeoutMillis(): " + mKvBackupAgentTimeoutMillis);
145             }
146             return mKvBackupAgentTimeoutMillis;
147         }
148     }
149 
getFullBackupAgentTimeoutMillis()150     public long getFullBackupAgentTimeoutMillis() {
151         synchronized (mLock) {
152             if (DEBUG_SCHEDULING) {
153                 Slog.v(TAG, "getFullBackupAgentTimeoutMillis(): " + mFullBackupAgentTimeoutMillis);
154             }
155             return mFullBackupAgentTimeoutMillis;
156         }
157     }
158 
getSharedBackupAgentTimeoutMillis()159     public long getSharedBackupAgentTimeoutMillis() {
160         synchronized (mLock) {
161             if (DEBUG_SCHEDULING) {
162                 Slog.v(
163                         TAG,
164                         "getSharedBackupAgentTimeoutMillis(): " + mSharedBackupAgentTimeoutMillis);
165             }
166             return mSharedBackupAgentTimeoutMillis;
167         }
168     }
169 
getRestoreAgentTimeoutMillis()170     public long getRestoreAgentTimeoutMillis() {
171         synchronized (mLock) {
172             if (DEBUG_SCHEDULING) {
173                 Slog.v(TAG, "getRestoreAgentTimeoutMillis(): " + mRestoreAgentTimeoutMillis);
174             }
175             return mRestoreAgentTimeoutMillis;
176         }
177     }
178 
getRestoreAgentFinishedTimeoutMillis()179     public long getRestoreAgentFinishedTimeoutMillis() {
180         synchronized (mLock) {
181             if (DEBUG_SCHEDULING) {
182                 Slog.v(
183                         TAG,
184                         "getRestoreAgentFinishedTimeoutMillis(): "
185                                 + mRestoreAgentFinishedTimeoutMillis);
186             }
187             return mRestoreAgentFinishedTimeoutMillis;
188         }
189     }
190 
getQuotaExceededTimeoutMillis()191     public long getQuotaExceededTimeoutMillis() {
192         synchronized (mLock) {
193             if (DEBUG_SCHEDULING) {
194                 Slog.v(
195                         TAG,
196                         "getQuotaExceededTimeoutMillis(): "
197                                 + mQuotaExceededTimeoutMillis);
198             }
199             return mQuotaExceededTimeoutMillis;
200         }
201     }
202 }
203