forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make BackgroundTaskScheduler become a pure virtual interface.
The BackgroundTaskScheduler is currently a concrete class, which hampers the ability to use it in tests outside of the component itself. This CL moves the BackgroundTaskScheduler implementation to a new class BackgroundTaskScedulerImpl, and makes BackgroundTaskScheduler become just pure virtual interface. This made it possible to make both the *Impl and *Delegate classes become package protected as well, and simplified some tests in the //chrome layer. Lastly, it creates a new class BackgroundTaskReflection which contains functionality related to inspect and create BackgroundTask instances, and it was used outside of the BackgroundTaskSchedulerImpl itself. BUG=729156 Change-Id: Ide01a6e12176fb4cf910634f06f5681c04ec38e8 Reviewed-on: https://chromium-review.googlesource.com/612127 Reviewed-by: Filip Gorski <fgorski@chromium.org> Commit-Queue: Tommy Nyquist <nyquist@chromium.org> Cr-Commit-Position: refs/heads/master@{#494887}
- Loading branch information
1 parent
05b71cb
commit c2501b4
Showing
12 changed files
with
224 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
.../java/src/org/chromium/components/background_task_scheduler/BackgroundTaskReflection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Copyright 2017 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
package org.chromium.components.background_task_scheduler; | ||
|
||
import android.support.annotation.Nullable; | ||
|
||
import org.chromium.base.Log; | ||
|
||
import java.lang.reflect.Constructor; | ||
|
||
/** | ||
* BakgroundTaskReflection contains functionality to construct {@link BackgroundTask} instances from | ||
* their classname and to inspect whether a particular {@link BackgroundTask} has a parameterless | ||
* constructor, which is required for any {@link BackgroundTask}. | ||
*/ | ||
final class BackgroundTaskReflection { | ||
private static final String TAG = "BkgrdTaskReflect"; | ||
|
||
/** | ||
* Uses reflection to find the given class and instantiates a {@link BackgroundTask} if the | ||
* class is valid. | ||
* | ||
* @param backgroundTaskClassName the full class name to look for. | ||
* @return a new {@link BackgroundTask} instance if successful or null when a failure occurs. | ||
*/ | ||
@Nullable | ||
static BackgroundTask getBackgroundTaskFromClassName(String backgroundTaskClassName) { | ||
if (backgroundTaskClassName == null) return null; | ||
|
||
Class<?> clazz; | ||
try { | ||
clazz = Class.forName(backgroundTaskClassName); | ||
} catch (ClassNotFoundException e) { | ||
Log.w(TAG, "Unable to find BackgroundTask class with name " + backgroundTaskClassName); | ||
return null; | ||
} | ||
|
||
if (!BackgroundTask.class.isAssignableFrom(clazz)) { | ||
Log.w(TAG, "Class " + clazz + " is not a BackgroundTask"); | ||
return null; | ||
} | ||
|
||
try { | ||
return (BackgroundTask) clazz.newInstance(); | ||
} catch (InstantiationException e) { | ||
Log.w(TAG, "Unable to instantiate class (InstExc) " + clazz); | ||
return null; | ||
} catch (IllegalAccessException e) { | ||
Log.w(TAG, "Unable to instantiate class (IllAccExc) " + clazz); | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* Inspects all public constructors of the given class, and returns true if one of them is | ||
* parameterless. | ||
* | ||
* @param clazz the class to inspect. | ||
* @return whether the class has a parameterless constructor. | ||
*/ | ||
static boolean hasParameterlessPublicConstructor(Class<? extends BackgroundTask> clazz) { | ||
for (Constructor<?> constructor : clazz.getConstructors()) { | ||
if (constructor.getParameterTypes().length == 0) return true; | ||
} | ||
return false; | ||
} | ||
|
||
private BackgroundTaskReflection() { | ||
// No instantiation. | ||
} | ||
} |
Oops, something went wrong.