blob: f5f35612679f781d5f491225ae57035b51d654b2 [file] [log] [blame]
Christopher Tate487529a2009-04-29 14:03:25 -07001/*
2 * Copyright (C) 2009 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
17package com.android.server;
18
19import android.backup.BackupService;
20import android.backup.IBackupService;
21import android.content.ComponentName;
22import android.content.Context;
23import android.content.Intent;
24import android.content.ServiceConnection;
25import android.content.pm.PackageManager;
26import android.content.pm.ResolveInfo;
27import android.content.pm.ServiceInfo;
28import android.os.Binder;
Christopher Tate22b87872009-05-04 16:41:53 -070029import android.os.Environment;
Christopher Tate487529a2009-04-29 14:03:25 -070030import android.os.Handler;
31import android.os.IBinder;
32import android.os.Message;
Christopher Tate22b87872009-05-04 16:41:53 -070033import android.os.ParcelFileDescriptor;
Christopher Tate487529a2009-04-29 14:03:25 -070034import android.os.RemoteException;
35import android.util.Log;
36import android.util.SparseArray;
37
38import android.backup.IBackupManager;
39
Christopher Tate22b87872009-05-04 16:41:53 -070040import java.io.File;
41import java.io.FileNotFoundException;
Christopher Tate487529a2009-04-29 14:03:25 -070042import java.lang.String;
43import java.util.HashSet;
44import java.util.List;
45
46class BackupManagerService extends IBackupManager.Stub {
47 private static final String TAG = "BackupManagerService";
48 private static final boolean DEBUG = true;
49
50 private static final long COLLECTION_INTERVAL = 3 * 60 * 1000;
51
52 private static final int MSG_RUN_BACKUP = 1;
53
54 private Context mContext;
55 private PackageManager mPackageManager;
56 private final BackupHandler mBackupHandler = new BackupHandler();
57 // map UIDs to the set of backup client services within that UID's app set
58 private SparseArray<HashSet<ServiceInfo>> mBackupParticipants
59 = new SparseArray<HashSet<ServiceInfo>>();
60 // set of backup services that have pending changes
61 private HashSet<ServiceInfo> mPendingBackups = new HashSet<ServiceInfo>();
62 private final Object mQueueLock = new Object();
63
Christopher Tate22b87872009-05-04 16:41:53 -070064 private File mStateDir;
Christopher Tate487529a2009-04-29 14:03:25 -070065
66 // ----- Handler that runs the actual backup process asynchronously -----
67
68 private class BackupHandler extends Handler implements ServiceConnection {
69 private volatile Object mBindSignaller = new Object();
70 private volatile boolean mBinding = false;
71 private IBackupService mTargetService = null;
72
73 public void handleMessage(Message msg) {
74
75 switch (msg.what) {
76 case MSG_RUN_BACKUP:
77 {
78 // snapshot the pending-backup set and work on that
79 HashSet<ServiceInfo> queue;
80 synchronized (mQueueLock) {
81 queue = mPendingBackups;
82 mPendingBackups = new HashSet<ServiceInfo>();
83 // !!! TODO: start a new backup-queue journal file too
84 }
85
86 // Walk the set of pending backups, setting up the relevant files and
87 // invoking the backup service in each participant
88 Intent backupIntent = new Intent(BackupService.SERVICE_ACTION);
89 for (ServiceInfo service : queue) {
90 mBinding = true;
91 mTargetService = null;
92
93 backupIntent.setClassName(service.packageName, service.name);
94 Log.d(TAG, "binding to " + backupIntent);
95 if (mContext.bindService(backupIntent, this, 0)) {
96 synchronized (mBindSignaller) {
97 while (mTargetService == null && mBinding == true) {
98 try {
99 mBindSignaller.wait();
100 } catch (InterruptedException e) {
101 }
102 }
103 }
104 if (mTargetService != null) {
105 try {
106 Log.d(TAG, "invoking doBackup() on " + backupIntent);
Christopher Tate22b87872009-05-04 16:41:53 -0700107
108 File savedStateName = new File(mStateDir, service.packageName);
109 File backupDataName = new File(mStateDir, service.packageName + ".data");
110 File newStateName = new File(mStateDir, service.packageName + ".new");
111
112 ParcelFileDescriptor savedState =
113 ParcelFileDescriptor.open(savedStateName,
114 ParcelFileDescriptor.MODE_READ_ONLY |
115 ParcelFileDescriptor.MODE_CREATE);
116 ParcelFileDescriptor backupData =
117 ParcelFileDescriptor.open(backupDataName,
118 ParcelFileDescriptor.MODE_READ_WRITE |
119 ParcelFileDescriptor.MODE_CREATE);
120 ParcelFileDescriptor newState =
121 ParcelFileDescriptor.open(newStateName,
122 ParcelFileDescriptor.MODE_READ_WRITE |
123 ParcelFileDescriptor.MODE_CREATE);
124
125 mTargetService.doBackup(savedState, backupData, newState);
126
127 // !!! TODO: Now propagate the newly-backed-up data to the transport
128
129 // !!! TODO: After successful transport, juggle the files so that
130 // next time the new state is used as the old state
131
132 } catch (FileNotFoundException fnf) {
133 Log.d(TAG, "File not found on backup: ");
134 fnf.printStackTrace();
Christopher Tate487529a2009-04-29 14:03:25 -0700135 } catch (RemoteException e) {
136 Log.d(TAG, "Remote target " + backupIntent
137 + " threw during backup:");
138 e.printStackTrace();
Christopher Tate22b87872009-05-04 16:41:53 -0700139 } catch (Exception e) {
140 Log.w(TAG, "Final exception guard in backup: ");
141 e.printStackTrace();
Christopher Tate487529a2009-04-29 14:03:25 -0700142 }
143 mContext.unbindService(this);
144 }
145 } else {
146 Log.d(TAG, "Unable to bind to " + backupIntent);
147 }
148 }
149 }
150 break;
151 }
152 }
153
154 public void onServiceConnected(ComponentName name, IBinder service) {
155 synchronized (mBindSignaller) {
156 mTargetService = IBackupService.Stub.asInterface(service);
157 mBinding = false;
158 mBindSignaller.notifyAll();
159 }
160 }
161
162 public void onServiceDisconnected(ComponentName name) {
163 synchronized (mBindSignaller) {
164 mTargetService = null;
165 mBinding = false;
166 mBindSignaller.notifyAll();
167 }
168 }
169 }
170
171 public BackupManagerService(Context context) {
172 mContext = context;
173 mPackageManager = context.getPackageManager();
174
Christopher Tate22b87872009-05-04 16:41:53 -0700175 // Set up our bookkeeping
176 File dataDir = Environment.getDataDirectory();
177 mStateDir = new File(dataDir, "backup");
178 mStateDir.mkdirs();
179
Christopher Tate487529a2009-04-29 14:03:25 -0700180 // Identify the backup participants
181 // !!! TODO: also watch package-install to keep this up to date
182 List<ResolveInfo> services = mPackageManager.queryIntentServices(
183 new Intent(BackupService.SERVICE_ACTION), 0);
184 if (DEBUG) {
185 Log.v(TAG, "Backup participants: " + services.size());
186 for (ResolveInfo ri : services) {
187 Log.v(TAG, " " + ri + " : " + ri.filter);
188 }
189 }
190
191 // Build our mapping of uid to backup client services
192 for (ResolveInfo ri : services) {
193 int uid = ri.serviceInfo.applicationInfo.uid;
194 HashSet<ServiceInfo> set = mBackupParticipants.get(uid);
195 if (set == null) {
196 set = new HashSet<ServiceInfo>();
197 mBackupParticipants.put(uid, set);
198 }
199 set.add(ri.serviceInfo);
200 }
201 }
202
203
204 // ----- IBackupManager binder interface -----
205
Christopher Tatea8bf8152009-04-30 11:36:21 -0700206 public void dataChanged(String packageName) throws RemoteException {
Christopher Tate487529a2009-04-29 14:03:25 -0700207 // Record that we need a backup pass for the caller. Since multiple callers
208 // may share a uid, we need to note all candidates within that uid and schedule
209 // a backup pass for each of them.
210
211 HashSet<ServiceInfo> targets = mBackupParticipants.get(Binder.getCallingUid());
212 if (targets != null) {
213 synchronized (mQueueLock) {
214 // Note that this client has made data changes that need to be backed up
Christopher Tate487529a2009-04-29 14:03:25 -0700215 for (ServiceInfo service : targets) {
Christopher Tatea8bf8152009-04-30 11:36:21 -0700216 // validate the caller-supplied package name against the known set of
217 // packages associated with this uid
218 if (service.packageName.equals(packageName)) {
219 // add the caller to the set of pending backups
220 if (mPendingBackups.add(service)) {
221 // !!! TODO: write to the pending-backup journal file in case of crash
222 }
Christopher Tate487529a2009-04-29 14:03:25 -0700223 }
224 }
225
226 // Schedule a backup pass in a few minutes. As backup-eligible data
227 // keeps changing, continue to defer the backup pass until things
228 // settle down, to avoid extra overhead.
229 mBackupHandler.removeMessages(MSG_RUN_BACKUP);
230 mBackupHandler.sendEmptyMessageDelayed(MSG_RUN_BACKUP, COLLECTION_INTERVAL);
231 }
232 }
233 }
234}