OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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.crash; |
| 6 |
| 7 import android.content.Context; |
| 8 import android.os.Handler; |
| 9 |
| 10 import org.chromium.base.annotations.SuppressFBWarnings; |
| 11 import org.chromium.chrome.browser.util.NonThreadSafe; |
| 12 import org.chromium.net.ConnectionType; |
| 13 import org.chromium.net.NetworkChangeNotifier; |
| 14 |
| 15 /** |
| 16 * This class listens to network changes and determine when it would good to |
| 17 * retry uploading minidumps. |
| 18 */ |
| 19 class MinidumpUploadRetry implements NetworkChangeNotifier.ConnectionTypeObserve
r { |
| 20 private final Context mContext; |
| 21 private static MinidumpUploadRetry sSingleton = null; |
| 22 |
| 23 private static class Scheduler implements Runnable { |
| 24 private static NonThreadSafe sThreadCheck; |
| 25 private final Context mContext; |
| 26 |
| 27 private Scheduler(Context context) { |
| 28 this.mContext = context; |
| 29 } |
| 30 |
| 31 @Override |
| 32 public void run() { |
| 33 if (sThreadCheck == null) { |
| 34 sThreadCheck = new NonThreadSafe(); |
| 35 } |
| 36 // Make sure this is called on the same thread all the time. |
| 37 assert sThreadCheck.calledOnValidThread(); |
| 38 if (!NetworkChangeNotifier.isInitialized()) { |
| 39 return; |
| 40 } |
| 41 if (sSingleton == null) { |
| 42 sSingleton = new MinidumpUploadRetry(mContext); |
| 43 } |
| 44 } |
| 45 } |
| 46 |
| 47 /** |
| 48 * Schedule a retry. If there is already one schedule, this is NO-OP. |
| 49 */ |
| 50 static void scheduleRetry(Context context) { |
| 51 // NetworkChangeNotifier is not thread safe. We will post to UI thread |
| 52 // instead since that's where it fires off notification changes. |
| 53 new Handler(context.getMainLooper()).post(new Scheduler(context)); |
| 54 } |
| 55 |
| 56 private MinidumpUploadRetry(Context context) { |
| 57 this.mContext = context; |
| 58 NetworkChangeNotifier.addConnectionTypeObserver(this); |
| 59 } |
| 60 |
| 61 @SuppressFBWarnings("ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD") |
| 62 @Override |
| 63 public void onConnectionTypeChanged(int connectionType) { |
| 64 // Look for "favorable" connections. Note that we never |
| 65 // know what the user's crash upload preference is until |
| 66 // the time when we are actually uploading. |
| 67 if (connectionType == ConnectionType.CONNECTION_NONE) { |
| 68 return; |
| 69 } |
| 70 MinidumpUploadService.tryUploadAllCrashDumps(mContext); |
| 71 NetworkChangeNotifier.removeConnectionTypeObserver(this); |
| 72 sSingleton = null; |
| 73 } |
| 74 } |
OLD | NEW |