Closed Bug 1219016 Opened 10 years ago Closed 10 years ago

Create service for running Gecko

Categories

(Firefox for Android Graveyard :: General, defect)

All
Android
defect
Not set
normal

Tracking

(firefox45 fixed)

RESOLVED FIXED
Firefox 45
Tracking Status
firefox45 --- fixed

People

(Reporter: jchen, Assigned: jchen)

References

Details

Attachments

(5 files)

Create an Android service that's capable of running Gecko in headless mode.
Right now we use the GeckoThread state to detect whether GeckoApp is first launching or is being restored after being destroyed. However, because of headless mode, GeckoThread could already be running on GeckoApp's first launch, so we need a separate way to detect relaunching.
Attachment #8679702 - Flags: review?(snorp)
This patch adds separate setter and getter for the application context in GeckoAppShell. The existing getContext method is misused for both application and activity contexts, so new methods are added to improve consistency.
Attachment #8679704 - Flags: review?(snorp)
This patch makes code use the application context from GeckoAppShell instead of the activity context. Enough changes are made to let headless mode work.
Attachment #8679705 - Flags: review?(snorp)
The code in WebappsUpdateTimer.js tried to detect a webapp process by using the context class name, which is extremely fragile. This patch makes it detect the current Gecko profile instead.
Attachment #8679706 - Flags: review?(mark.finkle)
This patch adds a Service that is able to launch Gecko in a headless state. To test it in a local build, add "android:exported=true" to GeckoService in AndroidManifest.xml.in, make sure the Fennec process is not running, and run something like > adb shell am startservice -n org.mozilla.fennec_$USER/org.mozilla.gecko.GeckoService You should then see Gecko output in the logcat.
Attachment #8679707 - Flags: review?(snorp)
Comment on attachment 8679705 [details] [diff] [review] Use application context where appropriate (v1) Review of attachment 8679705 [details] [diff] [review]: ----------------------------------------------------------------- Everything about this patch is 'wrong', in that you shouldn't need to do this, and you shouldn't use the application context in any case you can avoid it. Some old Stack Overflow wisdom is in the top-voted answer here: http://stackoverflow.com/questions/7298731/when-to-call-activity-context-or-application-context This is the expedient solution, but we put a bunch of effort into eliminating uses of GeckoAppShell's context code in general, and IMO this is a step in the wrong direction. I'd also like Sebastian to take a look at the RestrictedProfiles change. ::: mobile/android/base/GeckoAppShell.java @@ +1407,5 @@ > } > > @WrapForJNI > public static float getDensity() { > + return getApplicationContext().getResources().getDisplayMetrics().density; I have a sneaking horrible suspicion that we want an activity context in some of these situations… @@ +1463,5 @@ > } > > private static Vibrator vibrator() { > LayerView layerView = getLayerView(); > + return (Vibrator) getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE); This makes `layerView` redundant, no? @@ +2629,5 @@ > > // Don't fail silently, tell the user that we weren't able to share the image > private static final void showImageShareFailureToast() { > + Toast toast = Toast.makeText( > + getApplicationContext(), Toasts are supposed to use an activity context.
Attachment #8679705 - Flags: review?(s.kaspari)
Attachment #8679705 - Flags: feedback-
(In reply to Richard Newman [:rnewman] from comment #6) > Comment on attachment 8679705 [details] [diff] [review] > Use application context where appropriate (v1) > > Review of attachment 8679705 [details] [diff] [review]: > ----------------------------------------------------------------- > > Everything about this patch is 'wrong', in that you shouldn't need to do > this, and you shouldn't use the application context in any case you can > avoid it. > > Some old Stack Overflow wisdom is in the top-voted answer here: > > http://stackoverflow.com/questions/7298731/when-to-call-activity-context-or- > application-context > > This is the expedient solution, but we put a bunch of effort into > eliminating uses of GeckoAppShell's context code in general, and IMO this is > a step in the wrong direction. > > I'd also like Sebastian to take a look at the RestrictedProfiles change. For front-end code, I agree that there is almost always a more appropriate Context you can use, as explained by that StackOverflow post. However, for back-end code, the situation is completely different. Gecko has the same lifetime as the process itself, and for Gecko background tasks that require a Context (accessing the alarm manager, for example), it is almost certainly more appropriate to use the application context rather than another context. In fact, there may not be another context when Fennec is not in the foreground. That StackOverflow answer seems to make the argument that people shouldn't use application context because a lot of beginners don't know how to use it. That would be a pretty weak argument IMO. > ::: mobile/android/base/GeckoAppShell.java > @@ +1407,5 @@ > > } > > > > @WrapForJNI > > public static float getDensity() { > > + return getApplicationContext().getResources().getDisplayMetrics().density; > > I have a sneaking horrible suspicion that we want an activity context in > some of these situations… I did go through each change to make sure it made sense to be using an application context (for example, getting a system service). I could have overlooked some though. > @@ +1463,5 @@ > > } > > > > private static Vibrator vibrator() { > > LayerView layerView = getLayerView(); > > + return (Vibrator) getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE); > > This makes `layerView` redundant, no? Good catch. > @@ +2629,5 @@ > > > > // Don't fail silently, tell the user that we weren't able to share the image > > private static final void showImageShareFailureToast() { > > + Toast toast = Toast.makeText( > > + getApplicationContext(), > > Toasts are supposed to use an activity context. Toasts can use either application or activity contexts, as explained by the second answer in your StackOverflow question.
Comment on attachment 8679706 [details] [diff] [review] Don't use context class name to detect webapp process (V1) Good change
Attachment #8679706 - Flags: review?(mark.finkle) → review+
Comment on attachment 8679705 [details] [diff] [review] Use application context where appropriate (v1) Review of attachment 8679705 [details] [diff] [review]: ----------------------------------------------------------------- r+ for the restricted profiles related changes. We are using the Context for two things: Accessing Android's UserManager and calling GeckoProfile.get(). For the UserManager the context does not matter and for GeckoProfile.get().. it seems like you changed other calls to that in this patch as well - so I guess that's okay. ::: mobile/android/base/GeckoAppShell.java @@ +1650,5 @@ > > int[] result = new int[attrsAppearance.length]; > > final ContextThemeWrapper contextThemeWrapper = > + new ContextThemeWrapper(getApplicationContext(), android.R.style.TextAppearance); This /could/ yield different results depending on what theme is assigned to the application (context) or activity (context) in this situation.
Attachment #8679705 - Flags: review?(s.kaspari) → review+
Attachment #8679702 - Flags: review?(snorp) → review+
Comment on attachment 8679704 [details] [diff] [review] Store application context in GeckoAppShell (v1) Review of attachment 8679704 [details] [diff] [review]: ----------------------------------------------------------------- We should just call getApplicationContext() on whatever Context we have (service, activity, etc).
Attachment #8679704 - Flags: review?(snorp) → review-
Attachment #8679707 - Flags: review?(snorp) → review+
(In reply to James Willcox (:snorp) (jwillcox@mozilla.com) from comment #10) > Comment on attachment 8679704 [details] [diff] [review] > Store application context in GeckoAppShell (v1) > > Review of attachment 8679704 [details] [diff] [review]: > ----------------------------------------------------------------- > > We should just call getApplicationContext() on whatever Context we have > (service, activity, etc). We don't have a Context when it's a JNI call from Gecko, and if we're storing a Context for Gecko to use, we should store the application context instead of a service/activity context, which has shorter lifetime than Gecko.
Comment on attachment 8679704 [details] [diff] [review] Store application context in GeckoAppShell (v1) Review of attachment 8679704 [details] [diff] [review]: ----------------------------------------------------------------- Fair enough I guess
Attachment #8679704 - Flags: review- → review+
Attachment #8679705 - Flags: review?(snorp) → review+
rnewman: thoughts on comment 7?
Flags: needinfo?(rnewman)
(In reply to Jim Chen [:jchen] [:darchons] from comment #13) > rnewman: thoughts on comment 7? 303 mcomella :D
Flags: needinfo?(michael.l.comella)
Flags: needinfo?(rnewman)
I'll get back to this tomorrow but worth noting that I have no previous knowledge and will be largely looking this up. Sebastian, if you know anything off-hand about application context vs. activity context and how that might apply to a long-lived headless Gecko Service, feel free to chime in. Otherwise, feel free to clear it immediately – we both don't need to spend time on this. :)
Flags: needinfo?(s.kaspari)
(In reply to Michael Comella (:mcomella) from comment #15) > Sebastian, if you know anything off-hand about application context vs. activity context and how > that might apply to a long-lived headless Gecko Service, feel free to chime > in. My own guidelines are - in a nutshell: * Everything that needs to handle UI, themes, layouts, dialogs, .. (everything that happens inside an activity) most likely needs or requires an activity context. Some of those things you just can't create/call with an application context (e.g. dialog) but some of them work fine but might have undesirable results (e.g. LayoutInflater with application context). Toasts are an exception from that. * For system features it mostly does not matter: Sending broadcasts, getting system services (with at least the exception of LAYOUT_INFLATER_SERVICE), ... * The application context (Actually the Application object) lives as long as the process lives. So you can keep (static) references to it. You don't want to do that with an Activity (context) because this means you are keeping the activity and all resources in memory even if this (instance of) activity is not visible anymore. It heavily depends on the calls made inside GeckoAppShell. I guess in a perfect world GeckoView would bubble up calls to its activity if needed and background code uses the application context (or the service context) because it does not touch UI code. Briefly looking at the patch: * Calls to getSystemService() for getting LocationManager, AlarmManager, SensorManager, ActivityManager should yield the same result with both contexts. * For the calls to get the density, or use the density (via WindowManager, getResources(), bitmap decoding): I'm not 100% sure if there could be multiple windows with different densities and we actually want to get the values of the window of the current activity. * For Intents: For broadcasts it does not matter. For activities: They can only be started in a new task from the application context. I think the patch is mostly safe (I only did a quick scan) but let's discuss actual lines of code that might or might not be problematic.
Flags: needinfo?(s.kaspari)
Sorry for the delay. I don't think I can provide much more over Sebastian's answer, but I did find this old mobile-firefox-dev post [1] on the issue with a linked article [2], that has a similar message to Sebastian. [1]: https://mail.mozilla.org/pipermail/mobile-firefox-dev/2014-February/000518.html [2]: https://possiblemobile.com/2013/06/context/
Flags: needinfo?(michael.l.comella)
OK, let's get this landed and watch for context-related bustage. We are still early in the cycle. Jim - Can you do a simple post to mobile-firefox-dev to give a heads up?
Depends on: 1236049
No longer depends on: 1236049
Depends on: 1257304
Product: Firefox for Android → Firefox for Android Graveyard
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: