/chromium-webcl/src/chrome/common/extensions/docs/templates/articles/app_lifecycle.html

https://bitbucket.org/peixuan/chromium_r197479_base · HTML · 206 lines · 173 code · 33 blank · 0 comment · 0 complexity · c380e2ddf468671161f69c51e0bf0a39 MD5 · raw file

  1. <meta name="doc-family" content="apps">
  2. <h1>Manage App Lifecycle</h1>
  3. <p>
  4. The app runtime and event page are responsible
  5. for managing the app lifecycle.
  6. The app runtime manages app installation,
  7. controls the event page,
  8. and can shutdown the app at anytime.
  9. The event page listens out for events from the app runtime
  10. and manages what gets launched and how.
  11. </p>
  12. <h2 id="lifecycle">How the lifecycle works</h2>
  13. <p>
  14. The app runtime loads the event page
  15. from a user's desktop and
  16. the <code>onLaunch()</code> event is fired.
  17. This event tells the event page what windows
  18. to launch and their dimensions.
  19. The lifecycle diagram here isn't the nicest to look at,
  20. but it's practical (and we will make it nicer soon).
  21. </p>
  22. <img src="{{static}}/images/applifecycle.png"
  23. width="444"
  24. height="329"
  25. alt="how app lifecycle works">
  26. <p>
  27. When the event page has no executing JavaScript,
  28. no pending callbacks, and no open windows,
  29. the runtime unloads the event page and closes the app.
  30. Before unloading the event page,
  31. the <code>onSuspend()</code> event is fired.
  32. This gives the event page opportunity
  33. to do simple clean-up tasks
  34. before the app is closed.
  35. </p>
  36. <h2 id="eventpage">Create event page and windows</h2>
  37. <p>
  38. All apps must have an event page.
  39. This page contains the top-level logic of the application
  40. with none of its own UI and is responsible
  41. for creating the windows for all other app pages.
  42. </p>
  43. <h3 id="create_event_page">Create event page</h3>
  44. <p>
  45. To create the event page,
  46. include the "background" field in the app manifest
  47. and include the <code>background.js</code> in the scripts array.
  48. Any library scripts used by the event page need to be added
  49. to the "background" field first:
  50. </p>
  51. <pre>
  52. "background": {
  53. "scripts": [
  54. "foo.js",
  55. "background.js"
  56. ]
  57. }
  58. </pre>
  59. <p>
  60. Your event page must include the <code>onLaunched()</code> function.
  61. This function is called
  62. when your application is launched in any way:
  63. </p>
  64. <pre>
  65. chrome.app.runtime.onLaunched.addListener(function() {
  66. // Tell your app what to launch and how.
  67. });
  68. </pre>
  69. <h3 id="create_windows">Create windows</h3>
  70. <p>
  71. An event page may create one or more windows at its discretion.
  72. By default, these windows are created with a script connection
  73. to the event page and are directly scriptable by the event page.
  74. </p>
  75. <p>
  76. Windows have an optional frame with title bar and size controls. They are
  77. not associated with any Chrome browser windows.
  78. </p>
  79. <p>Here's a sample window created from <code>background.js</code>:</p>
  80. <pre>
  81. chrome.app.runtime.onLaunched.addListener(function() {
  82. chrome.app.window.create('main.html', {
  83. bounds: {
  84. width: 800,
  85. height: 600,
  86. left: 100,
  87. top: 100
  88. },
  89. minWidth: 800,
  90. minHeight: 600
  91. });
  92. });
  93. </pre>
  94. <h3 id="launch_data">Including Launch Data</h3>
  95. <p>
  96. Depending on how your app is launched,
  97. you may need to handle launch data
  98. in your event page.
  99. By default, there is no launch data
  100. when the app is started by the app launcher.
  101. For apps that have file handlers,
  102. you need to handle the
  103. <code>launchData.items</code> parameter to allow
  104. them to be launched with files.
  105. </p>
  106. <h2 id="runtime">Listening for app runtime events</h2>
  107. <p>
  108. The app runtime controls the app installs, updates, and uninstalls.
  109. You don't need to do anything to set up the app runtime,
  110. but your event page can listen out for the <code>onInstalled()</code> event
  111. to store local settings and the
  112. <code>onSuspend()</code> event to do simple clean-up tasks
  113. before the event page is unloaded.
  114. </p>
  115. <h3 id="local_settings">Storing local settings</h3>
  116. <p>
  117. <code>chrome.runtime.onInstalled()</code>
  118. is called when your app has first been installed,
  119. or when it has been updated.
  120. Any time this function is called,
  121. the <code>onInstalled</code> event is fired.
  122. The event page can listen for this event and use the
  123. <a href="storage.html">Storage API</a>
  124. to store and update local settings
  125. (see also <a href="app_storage.html#options">Storage options</a>).
  126. </p>
  127. <pre>
  128. chrome.runtime.onInstalled.addListener(function() {
  129. chrome.storage.local.set(object items, function callback);
  130. });
  131. </pre>
  132. <h3 id="preventing_loss">Preventing data loss</h3>
  133. <p>
  134. Users can uninstall your app at any time.
  135. When uninstalled,
  136. no executing code or private data is left behind.
  137. This can lead to data loss
  138. since the users may be uninstalling an app
  139. that has locally edited, unsynchronized data.
  140. You should stash data to prevent data loss.
  141. </p>
  142. <p>
  143. At a minimum,
  144. you should store user settings
  145. so that if users reinstall your app,
  146. their information is still available for reuse.
  147. Using the Storage API
  148. ($ref:storage.sync),
  149. user data can be automatically synced
  150. with Chrome sync.
  151. </p>
  152. <h3 id="clean-up">Clean-up before app closes</h3>
  153. <p>
  154. The app runtime sends the <code>onSuspend()</code>
  155. event to the event page before unloading it.
  156. Your event page can listen out for this event and
  157. do clean-up tasks before the app closes.
  158. </p>
  159. <p>
  160. Once this event is fired,
  161. the app runtime starts the process of closing the app:
  162. all events stop firing and
  163. JavaScript execution is halted.
  164. Any asynchronous operations started
  165. while handling this event are not guaranteed to complete.
  166. Keep the clean-up tasks synchronous and simple.
  167. </p>
  168. <pre>
  169. chrome.runtime.onSuspend.addListener(function() {
  170. // Do some simple clean-up tasks.
  171. });
  172. </pre>
  173. <p class="backtotop"><a href="#top">Back to top</a></p>