1 // Copyright 2020 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 package org.chromium.chrome.browser.safety_check; 6 7 import android.content.Context; 8 9 import org.chromium.base.Callback; 10 import org.chromium.base.task.PostTask; 11 import org.chromium.base.task.TaskTraits; 12 import org.chromium.chrome.browser.omaha.OmahaBase.UpdateStatus; 13 import org.chromium.chrome.browser.omaha.OmahaService; 14 import org.chromium.chrome.browser.safety_check.SafetyCheckProperties.UpdatesState; 15 import org.chromium.content_public.browser.UiThreadTaskTraits; 16 17 import java.lang.ref.WeakReference; 18 19 /** 20 * Glue code for interactions between Safety check and Omaha on Android. 21 * 22 * This class is needed because {@link OmahaService} is in //chrome/android, 23 * while Safety check is modularized in //chrome/browser. Once Omaha is 24 * modularized as well, this class will not be needed anymore. 25 */ 26 public class SafetyCheckUpdatesDelegateImpl implements SafetyCheckUpdatesDelegate { 27 private OmahaService mOmaha; 28 29 /** 30 * Creates a new instance of the glue class to be passed to 31 * {@link SafetyCheckSettingsFragment}. 32 * @param context A {@link Context} object, used by Omaha. 33 */ SafetyCheckUpdatesDelegateImpl(Context context)34 public SafetyCheckUpdatesDelegateImpl(Context context) { 35 mOmaha = OmahaService.getInstance(context); 36 } 37 38 /** 39 * Converts Omaha's {@link UpdateStatus} into a 40 * {@link SafetyCheckModel.Updates} enum value. 41 * @param status Update status returned by Omaha. 42 * @return A corresponding {@link SafetyCheckModel.Updates} value. 43 */ convertOmahaUpdateStatus(@pdateStatus int status)44 public static @UpdatesState int convertOmahaUpdateStatus(@UpdateStatus int status) { 45 switch (status) { 46 case UpdateStatus.UPDATED: 47 return UpdatesState.UPDATED; 48 case UpdateStatus.OUTDATED: 49 return UpdatesState.OUTDATED; 50 case UpdateStatus.OFFLINE: 51 return UpdatesState.OFFLINE; 52 case UpdateStatus.FAILED: // Intentional fall through. 53 default: 54 return UpdatesState.ERROR; 55 } 56 } 57 58 /** 59 * Asynchronously checks for updates and invokes the provided callback with 60 * the result. 61 * @param statusCallback A callback to invoke with the result. Takes an element of 62 * {@link SafetyCheckProperties.UpdatesState} as an argument. 63 */ 64 @Override checkForUpdates(WeakReference<Callback<Integer>> statusCallback)65 public void checkForUpdates(WeakReference<Callback<Integer>> statusCallback) { 66 PostTask.postTask(TaskTraits.USER_VISIBLE, () -> { 67 @UpdateStatus 68 int status = mOmaha.checkForUpdates(); 69 // Post the results back to the UI thread. 70 PostTask.postTask(UiThreadTaskTraits.DEFAULT, () -> { 71 Callback<Integer> strongRef = statusCallback.get(); 72 if (strongRef != null) { 73 strongRef.onResult(convertOmahaUpdateStatus(status)); 74 } 75 }); 76 }); 77 } 78 } 79