OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser.precache; | 5 package org.chromium.chrome.browser.precache; |
6 | 6 |
7 import android.app.Service; | 7 import android.app.Service; |
8 import android.content.BroadcastReceiver; | 8 import android.content.BroadcastReceiver; |
9 import android.content.Context; | 9 import android.content.Context; |
10 import android.content.Intent; | 10 import android.content.Intent; |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
65 } | 65 } |
66 } | 66 } |
67 }; | 67 }; |
68 | 68 |
69 @VisibleForTesting | 69 @VisibleForTesting |
70 BroadcastReceiver getDeviceStateReceiver() { | 70 BroadcastReceiver getDeviceStateReceiver() { |
71 return mDeviceStateReceiver; | 71 return mDeviceStateReceiver; |
72 } | 72 } |
73 | 73 |
74 @VisibleForTesting | 74 @VisibleForTesting |
75 void handlePrecacheCompleted() { | 75 void handlePrecacheCompleted(boolean precacheStarted) { |
76 if (mIsPrecaching) finishPrecaching(); | 76 if (mIsPrecaching) finishPrecaching(precacheStarted); |
77 } | 77 } |
78 | 78 |
79 /** PrecacheLauncher used to run precaching. */ | 79 /** PrecacheLauncher used to run precaching. */ |
80 private PrecacheLauncher mPrecacheLauncher = new PrecacheLauncher() { | 80 private PrecacheLauncher mPrecacheLauncher = new PrecacheLauncher() { |
81 @Override | 81 @Override |
82 protected void onPrecacheCompleted() { | 82 protected void onPrecacheCompleted(boolean precacheStarted) { |
bengr
2015/08/05 23:25:35
Why would onPrecachCompleted be called if precache
twifkak
2015/08/06 00:09:46
There are two cases:
https://code.google.com/p/chr
bengr
2015/08/07 18:41:57
It's not ideal. What would it take to prevent it?
twifkak
2015/08/07 21:48:15
Done. I renamed the parameter since it's a misnome
| |
83 handlePrecacheCompleted(); | 83 handlePrecacheCompleted(precacheStarted); |
84 } | 84 } |
85 }; | 85 }; |
86 | 86 |
87 @VisibleForTesting | 87 @VisibleForTesting |
88 void setPrecacheLauncher(PrecacheLauncher precacheLauncher) { | 88 void setPrecacheLauncher(PrecacheLauncher precacheLauncher) { |
89 mPrecacheLauncher = precacheLauncher; | 89 mPrecacheLauncher = precacheLauncher; |
90 } | 90 } |
91 | 91 |
92 @Override | 92 @Override |
93 public void onCreate() { | 93 public void onCreate() { |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
154 acquirePrecachingWakeLock(); | 154 acquirePrecachingWakeLock(); |
155 | 155 |
156 // In certain cases, the PrecacheLauncher will skip precaching entirely and call | 156 // In certain cases, the PrecacheLauncher will skip precaching entirely and call |
157 // finishPrecaching() before this call to mPrecacheLauncher.start() retu rns, so the call to | 157 // finishPrecaching() before this call to mPrecacheLauncher.start() retu rns, so the call to |
158 // mPrecacheLauncher.start() must happen after acquiring the wake lock t o ensure that the | 158 // mPrecacheLauncher.start() must happen after acquiring the wake lock t o ensure that the |
159 // wake lock is released properly. | 159 // wake lock is released properly. |
160 mPrecacheLauncher.start(); | 160 mPrecacheLauncher.start(); |
161 } | 161 } |
162 | 162 |
163 /** End a precache cycle. */ | 163 /** End a precache cycle. */ |
164 private void finishPrecaching() { | 164 private void finishPrecaching(boolean precacheStarted) { |
165 Log.v(TAG, "Finish precaching"); | 165 Log.v(TAG, "Finish precaching"); |
166 shutdownPrecaching(); | 166 shutdownPrecaching(precacheStarted); |
167 } | 167 } |
168 | 168 |
169 /** Cancel a precache cycle. */ | 169 /** Cancel a precache cycle. */ |
170 private void cancelPrecaching() { | 170 private void cancelPrecaching() { |
171 Log.v(TAG, "Cancel precaching"); | 171 Log.v(TAG, "Cancel precaching"); |
172 prepareNativeLibraries(); | 172 prepareNativeLibraries(); |
173 mPrecacheLauncher.cancel(); | 173 mPrecacheLauncher.cancel(); |
174 | 174 |
175 shutdownPrecaching(); | 175 shutdownPrecaching(true); |
176 } | 176 } |
177 | 177 |
178 /** | 178 /** |
179 * Update state to indicate that precaching is no longer in progress, and st op the service. | 179 * Update state to indicate that precaching is no longer in progress, and st op the service. |
180 */ | 180 */ |
181 private void shutdownPrecaching() { | 181 private void shutdownPrecaching(boolean precacheStarted) { |
182 mIsPrecaching = false; | 182 mIsPrecaching = false; |
183 releasePrecachingWakeLock(); | 183 releasePrecachingWakeLock(); |
184 PrecacheServiceLauncher.precachingFinished(getApplicationContext(), prec acheStarted); | |
184 stopSelf(); | 185 stopSelf(); |
185 } | 186 } |
186 | 187 |
187 /** Register a BroadcastReceiver to detect when conditions become wrong for precaching. */ | 188 /** Register a BroadcastReceiver to detect when conditions become wrong for precaching. */ |
188 private void registerDeviceStateReceiver() { | 189 private void registerDeviceStateReceiver() { |
189 IntentFilter filter = new IntentFilter(); | 190 IntentFilter filter = new IntentFilter(); |
190 filter.addAction(Intent.ACTION_POWER_DISCONNECTED); | 191 filter.addAction(Intent.ACTION_POWER_DISCONNECTED); |
191 filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); | 192 filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); |
192 filter.addAction(Intent.ACTION_SCREEN_ON); | 193 filter.addAction(Intent.ACTION_SCREEN_ON); |
193 registerReceiver(mDeviceStateReceiver, filter); | 194 registerReceiver(mDeviceStateReceiver, filter); |
194 } | 195 } |
195 | 196 |
196 /** Acquire the precaching WakeLock. */ | 197 /** Acquire the precaching WakeLock. */ |
197 private void acquirePrecachingWakeLock() { | 198 private void acquirePrecachingWakeLock() { |
198 if (mPrecachingWakeLock == null) { | 199 if (mPrecachingWakeLock == null) { |
199 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERV ICE); | 200 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERV ICE); |
200 mPrecachingWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG); | 201 mPrecachingWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG); |
201 } | 202 } |
202 mPrecachingWakeLock.acquire(); | 203 mPrecachingWakeLock.acquire(); |
203 } | 204 } |
204 | 205 |
205 /** Release the precaching WakeLock if it is held. */ | 206 /** Release the precaching WakeLock if it is held. */ |
206 private void releasePrecachingWakeLock() { | 207 private void releasePrecachingWakeLock() { |
207 if (mPrecachingWakeLock != null && mPrecachingWakeLock.isHeld()) { | 208 if (mPrecachingWakeLock != null && mPrecachingWakeLock.isHeld()) { |
208 mPrecachingWakeLock.release(); | 209 mPrecachingWakeLock.release(); |
209 } | 210 } |
210 } | 211 } |
211 } | 212 } |
OLD | NEW |