As crazy as it sounds, I like this idea. Stuffing iframes is a fairly perfect memory management model.
I've seen multiple developers use this in mobile app development because once you trash an iframe, you get rid of all its memory very easily.
Implementing page transitions feels like it would be really simple.
I feel like the tough part would be transferring bits of data between frames, though I think we have a few options available to us here. We might even be able to sneak into some tiny shared data in `window.parent` somehow with a sandbox flag.
On Wednesday, June 12, 2013 10:06:06 AM UTC-4, Vivien wrote:
> Hello folks,
>
>
>
> Last year or so many Gaia apps has been built using a framework-less
>
> based approach, using a combination of libraries + css building blocks.
>
> In parallel, during last year a lot of things has been learned about
>
> device constraints, mobile constraints, app constraints,
>
> cross-teams-work constraints.
>
>
>
> -----------------
>
> Based on this here are some of those constraints (some are already
>
> partially solved) I would like to reach primarily:
>
> 1. *Fast loading application*
>
> Andreas said it a few times already. Apps should load in less than 600ms.
>
>
>
> 2. *Low memory consumption*
>
> The memory of the device is limited. A limited number of applications
>
> can run at the same time. Less memory used means more applications can
>
> run at the same time.
>
>
>
> 3. *Stable memory consumption*
>
> It is OK to consume an extra memory while making a particular task of
>
> an application. But this memory needs to be freed once the task has been
>
> finished.
>
>
>
> 4. *Smooth panel transitions*
>
> Transitions should happen really quick after a hit on a button.
>
> Ideally a panel should be fully loaded when the transition is complete,
>
> but it sounds OK if informations such as the list of wifi networks
>
> around, loads delayed.
>
>
>
> 5. *Inline activities friendly*
>
> The whole application should not be required to be loaded for
>
> handling a particular operation.
>
>
>
> 6. *Scoped Panels*
>
> Panels should be independent. So a panel is directly responsible of
>
> its own load time and of any JS breakage it can generate. Also, an
>
> individual panel should not be able to break the application. Different
>
> authors can work on the same application and each of them would be
>
> directly responsible of one or many panels.
>
>
>
> It sounds me that is could be a good set of constraints to solve with a
>
> framewor-less based approach, using a combination of libraries + css
>
> building blocks... and html.
>
>
>
> (There are other issues obviously and some of them are already handled
>
> by brave folks, like Tim and others in Taipei working on multiple
>
> resolutions support for example. Also, I have omit voluntarily the
>
> specific set of issues of the System app, which is similar in some
>
> cases, but has some special cases too IMHO)
>
>
>
> -----------------
>
> The proposal:
>
>
>
> <iframe mozbrowser mozapp="app://
myapp.gaiamobile.org/myapp.manifest"
>
> src="app://
myapp.gaiamobile.org/index.html">
>
> <!doctype html>
>
> <html>
>
> <head>
>
> <!-- This is a dirty little js lib that handle navigation between
>
> panels with a local postMessage protocol -->
>
> <!-- XXX Please don't focus tptionoo much on it - there are some
>
> plans to get rid of it in a potential near future but this
>
> discussion is way out of the scope of this proposal and I
>
> don't want to mix both discussions -->
>
> <script src="shared/js/navigation.js"></script>
>
> </head>
>
>
>
> <body>
>
> <!-- This panel will be created only once the application has
>
> loaded. Or it could be directly included in the html if the author wants
>
> to delay the mozbrowserloadend event for some reasons -->
>
> <iframe sandbox="allow-scripts allow-forms"
>
> src="main-panel.html"></iframe>
>
>
>
> <!-- This iframe will be created only once the user has made a
>
> choice in the main-panel -->
>
> <iframe sandbox="allow-scripts allow-forms"
>
> src="next-panel.html"></iframe>
>
>
>
> <!-- this iframe will be created only once the user has made a
>
> choice in the next panel -->
>
> <iframe sandbox="allow-scripts allow-forms"
>
> src="next-next-panel.html"></iframe>
>
>
>
> <!-- Back buttons that lives outside of the panels. A user will
>
> always be able to exit a panel even if this one breaks -->
>
> <button id="back">
>
> </body>
>
> </html>
>
> </iframe>
>
>
>
> This is mostly it.
>
>
>
> Basically the <navigation.js> helper creates sandbox-ed <iframe>s on
>
> demand and handle the navigation, as well as the back button.
>
>
>
> As long the user navigates forward, new sandbox-ed <iframe>s are
>
> created. As soon the user hit the back button the last <iframe> is
>
> removed in order to free the relevant part of memory. Since <iframe>s
>
> are sandboxed without the allow-same-origin flag, the only way to
>
> communicate should be postMessage and data would be serialized that
>
> should prevent references to each other and guarantee that the memory
>
> will be freed.
>
>
>
> I feel like this will address 1., 2. and 3. since the memory used will
>
> be directly related to the real user activity and it will be freed once
>
> it is done. Also, an application will be able to detach the previous
>
> state of the application to save some memory if needed. (For example the
>
> settings app would be able to detach the <iframe src="main-panel.html">
>
> if the user has finished the activity but, instead of going back to the
>
> application starts using the back button, he/she hits the home button,
>
> leaving completely the app to do something else.
>
>
>
> 1. Will be addressed because nothing else than the main code would be
>
> loaded on cold launch. If the app is slow it will be trivial to identify
>
> the responsible panel and to fix it.
>
>
>
> 5. Will be addressed since it will be easy to declare in the manifest
>
> which urls correlate with a particular inline activity.
>
> 6. Will be addressed since each panels will not be able to access others
>
> because of the sandbox. Also, the back button being part of the main app
>
> container will ensure the user will always be able to go back and reload
>
> the panel to see if it works finally (poor's man solution to race
>
> condition, bugs that appear only if your follow a specific code path).
>
>
>
> About 4. This model will let us get rid of the <!-- html comment -->
>
> hack used in different applications as well as a big part of
>
> <lazy_loader.js> since only the necessary part of a panel should be
>
> loaded with it. Everything that is not vital and can be delayed like
>
> getting the list of wifi networks can be loaded using <script async>.
>
> As a result Gaia apps will be split in many html files. The directory
>
> organization may reflect it.
>
>
>
> How much is it going to impatc load time of a specific panel? I feel
>
> like it should be fast since this is not the equivalent of opening a new
>
> process (it has already been opened), but just spawning a new
>
> compartment and parse js/css file again (see next paragraph about that).
>
>
>
> -----------------
>
> Possible issues:
>
> a. Global states cannot be shared.
>
> b. Load time for each panels
>
> 1. JavaScript files / libraries needs to be replicated.
>
> 2. CSS files need to be replicated.
>
> 3. Assets needs to be replicated.
>
> c. Memory overhead for each compartments
>
>
>
> I have probably forgot some obvious things, please feel free to add to
>
> the list!
>
>
>
> -----------------
>
> Possible answers:
>
> For *a* and *b1*.
>
> Four things comes to my mind.
>
> - Shared Workers -
https://bugzilla.mozilla.org/show_bug.cgi?id=643325
>
> - IndexedDB support in Workers -
>
>
https://bugzilla.mozilla.org/show_bug.cgi?id=701634
>
> - Lazy bytecode generation -
>
>
https://bugzilla.mozilla.org/show_bug.cgi?id=678037
>
> - JSScript sharing -
https://bugzilla.mozilla.org/show_bug.cgi?id=679940
>
>
>
> First I feel like states should not be shared between panels, but data
>
> should be. I can imagine that there are some cases where some kind of
>
> state needs to be shared, but I'm not convinced this is the most common
>
> case. Shared worker and indexedDB support in workers should solve many
>
> issues and help a lot here. Many different panels would be able to
>
> access the worker that will have been loaded once. I cannot tell how
>
> glad I am to have seen a recent path on the SharedWorker implementation.
>
> bent++.
>
>
>
> Lazy bytecode generation will help save some startup time / memory. If a
>
> utility library is replicated, but some part are not used, that will be
>
> less an issue.
>
>
>
> JSScript sharing. As far as I have understood those sharing of JSScript
>
> data is mostly for the self-hosted methods, like Array.map and so on. I
>
> wonder if some JSScript data can be shared for scripts with the same
>
> mozapp context, same origin as long as they come from offline cache /
>
> packaged content.
>
>
>
> For *b2*, *b3*.
>
> Two things come to my mind:
>
> - Seamless iframe -
https://bugzilla.mozilla.org/show_bug.cgi?id=631218
>
> - Similarly to JSScript sharing I wonder if some data can be shared as
>
> well for offline cache/packaged same origin css files ? (obviously I
>
> don't know much about layout/ and it this is possible).
>
>
>
> As far as I understand the specification of the 'seamless' attribute,
>
> the sub-iframes will inherit the parent rule. If that means the
>
> style-set rules are shared that would be a good fit to avoid extra load
>
> time and fit our memory constraints.
>
>
>
> For c. I made a little measurement and found an extra JS overhead of
>
> 100k per compartment on my laptop using about:memory. It is not always
>
> easy to read it so I could have made a mistake. Layout data were adding
>
> an overhead of ~180k per iframes. (140k on the 180k seems to be
>
> html.css/forms.css/... recomputed for each iframes. If seamless is what
>
> I think I wonder it this could be solved in the same bucket partially).
>
>
>
>
>
> -----------------
>
> Possible Implementation plan
>
>
>
> Obviously some part of Gecko are not ready, Shared Worker has only
>
> started the review process, IndexedDB is not accessible from worker,
>
> seamless is not implemented, there is no lazy bytecode evaluation yet,
>
> ... But that should not prevent to implement the model for many parts in
>
> many apps.
>
>
>
> In the first time of the implementation the sandbox attribute can be
>
> added only based on a whitelist in <navigation.js> in order to allow a
>
> smooth transition. Then, some apps should be able to load some custom
>
> scripts in the main context of their applications until SharedWorker lands.
>
>
>
> -----------------
>
>
>
> I have more details in my head, but I want to dump this somewhere while
>
> the roadmap for the next version is not final in order to include this
>
> engineering works to the workload for the next coming months if that
>
> makes sense.
>
>
>
> Feel free to comments / criticize / etc.
>
>
>
> Vivien.