Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(115)

Unified Diff: chrome/common/extensions/docs/templates/articles/app_codelab6_lifecycle.html

Issue 12221067: Move Chrome Apps Codelab docs to developer.chrome.com (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fixed references to github, accordingly to the new github angularjs subdirectories; added a link to… Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/common/extensions/docs/templates/articles/app_codelab6_lifecycle.html
diff --git a/chrome/common/extensions/docs/templates/articles/app_codelab6_lifecycle.html b/chrome/common/extensions/docs/templates/articles/app_codelab6_lifecycle.html
new file mode 100644
index 0000000000000000000000000000000000000000..0d7563c6d3467211e31653a3dd2040e86726fbae
--- /dev/null
+++ b/chrome/common/extensions/docs/templates/articles/app_codelab6_lifecycle.html
@@ -0,0 +1,129 @@
+<h1 id="lab_6_lifecycle">Lab 6 - Lifecycle</h1>
+
+<p>Like everything in this world, apps have a lifecycle. They are installed, launched, restarted, suspended when the system needs to free up resources and uninstalled. This lab will show you the basics of the Chrome app lifecycle and how its heart, the event page (aka background script), is used.</p>
+
+<h2 id="you_should_also_read">You should also read</h2>
+
+<p><a href="http://developer.chrome.com/apps/app_lifecycle.html">Manage App Lifecycle</a> in Chrome app docs</p>
+
+<h2 id="the_event_page">The event page</h2>
+
+<p>The event page is one of the most important pieces of a Chrome app. It&#39;s responsible for what gets launched, when, and how.
+For example, if your app is an instant messenger, you might want your event page to only show a UI when there is a new notification.</p>
+
+<p>For simpler apps, the event page listens to the app lifecycle events and reacts appropriately.
+There are two important lifecycle events, <a href="http://developer.chrome.com/trunk/apps/app.runtime.html#event-onLaunched">onLaunched</a> and <a href="http://developer.chrome.com/trunk/apps/app.runtime.html#event-onRestarted">onRestarted</a>.</p>
+
+<h2 id="the_onlaunched_event_and_the_chrome_app_window_create_method">The onLaunched event and the chrome.app.window.create method</h2>
+
+<p>onLaunched is the most important event. It fires when the user clicks on your app&#39;s icon with the intent of launching it. For most simpler apps, the event page will listen for this event and open a window when it fires. See our <code>main.js</code> for the most common usage.</p>
mkearney1 2013/02/12 22:22:11 This should be a link to the onLaunched API.
mkearney1 2013/02/12 22:22:11 This should really link to main.js
Renato Mangini (chromium) 2013/02/13 22:55:33 Done.
Renato Mangini (chromium) 2013/02/13 22:55:33 Done.
+
+<h3 id="windows_with_ids">Windows with IDs</h3>
+
+<p>The <a href="http://developer.chrome.com/trunk/apps/app.window.html#method-create">chrome.app.window.create</a> method can associate an ID to the window being opened. Currently, the most interesting use for this is to restore a window&#39;s width, height and location and its associated Developer Tools window, if opened, when the app is launched. </p>
+
+<p>Execute your app as it is now, move and resize the window, close and restart it. The app will reopen in the original location, right? Now add a property <code>id</code> to the <a href="https://github.com/GoogleChrome/chrome-app-codelab/blob/master/lab6_lifecycle/angularjs/main.js">main.js</a>, reload the app and test again:</p>
+
+<p><pre>js
+chrome.app.runtime.onLaunched.addListener(function() {
+ chrome.app.window.create(&#39;index.html&#39;,
+ {id: &#39;mainwindow&#39;, width: 500, height: 309});
+ });
+</pre></p>
+
+<p>If your application requires, you can open more than one window.</p>
+
+<h2 id="the_onrestarted_event">The onRestarted event</h2>
+
+<p>The onRestarted event is not as essential as onLaunched, but it might be relevant to certain types of apps.
mkearney1 2013/02/12 22:22:11 This should link to the onRestarted and onLaunche
Renato Mangini (chromium) 2013/02/13 22:55:33 Where are these docs? For onLaunched, I've linked
mkearney1 2013/02/13 23:17:57 There in app.runtime api: http://developer.chrome.
Renato Mangini (chromium) 2013/02/14 15:45:27 Done.
+This event is executed when the app is restarted, for example, when Chrome quits, restarts, and the app is launched again.
+You can use this event to restore a transient state. </p>
+
+<p>For example, if your app has a form with several fields, you won&#39;t always want to save the partial form while the user is typing.
+If the user quits the app on purpose, she might not be interested keeping the partial data.
+If the Chrome runtime restarted for some reason other than by a user&#39;s intention, the user will want that data when the app is restarted.</p>
+
+<p>Let&#39;s change our code to save the Todo input field in <a href="http://developer.chrome.com/trunk/apps/storage.html">chrome.storage.local</a> as the user types, only restoring it if the onRestarted event is triggered.</p>
mkearney1 2013/02/12 22:22:11 Wrap onRestarted in <code>
Renato Mangini (chromium) 2013/02/13 22:55:33 Done.
+
+<p class="note"><b>Note:</b> We learned about chrome.storage.sync before, but chrome.storage.local wasn&#39;t mentioned until now. Both have exactly the same syntax, but the semantics of chrome.storage.local is, as the name says, completely local.
mkearney1 2013/02/12 22:22:11 Link to storage API. Also wrap API in <code>.
Renato Mangini (chromium) 2013/02/13 22:55:33 Done.
+There&#39;s no attempt to synchronize or to save the data in the cloud.</p>
+
+<ul>
+<li><p>Event page: <a href="https://github.com/GoogleChrome/chrome-app-codelab/blob/master/lab6_lifecycle/angularjs/main.js">main.js</a>
+<pre>
+chrome.app.runtime.onLaunched.addListener(function() {
+ // normal launch initiated by the user, let&#39;s start clean.
+ // note that this is not related to the persistent state, which is
+ // appropriately handled in the window code.
+ runApp(false);
+});
+
+chrome.app.runtime.onRestarted.addListener(function() {
+ // if restarted, try to get the transient saved state
+ runApp(true);
+});
+
+function runApp(readInitialState) {
+ chrome.app.window.create(&#39;index.html&#39;,
+ {id: &#39;mainwindow&#39;, width: 500, height: 309},
+ // the create callback gets a reference to the AppWindow obj
+ function(win) {
+ // when the callback is executed, the DOM is loaded but no script was
+ // loaded yet. So, let&#39;s attach to the load event.
+ win.contentWindow.addEventListener(&#39;load&#39;, function() {
+ if (readInitialState) {
+ win.contentWindow.setInitialState();
+ } else {
+ win.contentWindow.clearInitialState();
+ }
+ });
+ });
+}
+</pre></li><li>Controller: add to existing <a href="https://github.com/GoogleChrome/chrome-app-codelab/blob/master/lab6_lifecycle/angularjs/controller.js">controller.js</a>
+<pre>
+var newTodoInput = null;
+
+var clearInitialState = function() {
+ chrome.storage.local.set({&#39;newtodo&#39;: null});
+}
+
+var setInitialState = function() {
+ chrome.storage.local.get(&#39;newtodo&#39;, function(data) {
+ if (newTodoInput &amp;&amp; data &amp;&amp; data.newtodo) {
+ newTodoInput.value = data.newtodo;
+ newTodoInput.focus();
+ }
+ });
+}
+
+window.addEventListener(&#39;load&#39;, function() {
+ var saveTransientState = function() {
+ chrome.storage.local.set({&#39;newtodo&#39;: newTodoInput.value});
+ };
+ newTodoInput = document.querySelector(&#39;input[type=&quot;text&quot;]&#39;);
+ newTodoInput.addEventListener(&#39;keypress&#39; , function() {
+ saveTransientState();<br>
+ })
+})
+</pre></p></li><li><p>Save the changes by reloading the app: open the app, right-click and select Reload.</p></li>
+</ul>
+
+<p>If Chrome and the app shuts down for any reason (other than a user-gesture), the onRestarted event is fired.
mkearney1 2013/02/12 22:22:11 onRestarted should be in <code>.
Renato Mangini (chromium) 2013/02/13 22:55:33 Done.
+Any text entered in the input field (but not yet saved as a Todo item) will reappear when Chrome and the app are reopened.</p>
+
+<p class="note"><b>Note:</b> If you get stuck and want to see the app in action, go to <code>chrome://extensions</code>,
+load the unpacked <a href="app_codelab6_lifecycle.html">lab6_lifecycle</a>, and launch the app from a new tab.</p>
+
+<h1 id="takeaways_">Takeaways:</h1>
+
+<ul>
+<li>The event page may continue to run even when your windows are closed.
+You can move logic that is shared among windows to the event page, as we will see in <a href="app_codelab9_multipleviews.html">lab9</a>.</li>
+</ul>
mkearney1 2013/02/12 22:22:11 The font looks a bit small here. Any idea why?
Renato Mangini (chromium) 2013/02/13 22:55:33 Done.
+
+<h1 id="what_39_s_next_">What&#39;s next?</h1>
+
+<p>In <a href="app_codelab7_useridentification.html">lab7_useridentification</a>,
+you will learn how to identify users and use OAuth2.0 to access Google and other third party services.</p>
+
+<p class="note"><b>Note:</b> The <a href="http://developer.chrome.com/trunk/apps/app_identity.html">identify API</a> covered in lab 7 is still experimental.</p>

Powered by Google App Engine
This is Rietveld 408576698