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

Side by Side Diff: chrome/common/extensions/docs/templates/articles/app_codelab8_webresources.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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 <h1 id="lab_8_web_resources">Lab 8 - Web Resources</h1>
2
3 <p>Chrome apps have a strict <a href="http://developer.chrome.com/trunk/apps/app _csp.html">Content Security Policy</a> which will not let the user execute code or load resources that are hosted remotely.</p>
4
5 <p>Many applications, however, need to be able to load and display content from a remote location. A News Reader, for example, needs to display remote content i nline or load and show images from a remote URL.</p>
6
7 <h2 id="you_should_also_read">You should also read</h2>
8
9 <ul>
10 <li><a href="http://developer.chrome.com/apps/app_external.html">Embed Content</ a> in Chrome app docs</li>
11 </ul>
12
13 <h2 id="loading_external_web_content_into_an_element">Loading external web conte nt into an element</h2>
14
15 <p>Sites on the internet are inherently a security risk and rendering arbitrary web pages directly into your application with elevated privileges would be a pot ential source of exploits.</p>
16
17 <p>Chrome apps offer developers the ability to securely render third-party conte nt in the <code>&lt;webview&gt;</code> tag. A WebView is like an iframe that you can control with greater flexibility and added security.
18 It runs in a separate sandboxed process and can&#39;t communicate directly with the application.</p>
19
20 <p class="note"><b>Tip:</b> The WebView has a very simple API. From your app y ou can:</p>
21
22 <ul>
23 <li> Change the URL of the WebView.</li>
24 <li> Navigate forwards and backward, stop loading and reload.</li>
25 <li> Check if the WebView has finished loading and if it is possible, go back an d forward in the history stack.</li>
26 </ul></p>
27
28 <p>We will change our code to render the content of URLs dropped in the drag-and -drop operations in a WebView when the user clicks on a link.</p>
29
30 <ol>
31 <li><p>Request a new permission, &quot;webview&quot;, in <a href="https://github .com/GoogleChrome/chrome-app-codelab/blob/master/lab8_webresources/angularjs/1_w ebview/manifest.json">manifest.json</a>:
32 <pre>
33 &quot;permissions&quot;: [&quot;storage&quot;, &quot;webview&quot;]
34 </pre></p></li>
35 <li><p>Add a WebView tag and a link to <a href="https://github.com/GoogleChrome/ chrome-app-codelab/blob/master/lab8_webresources/angularjs/1_webview/index.html" >index.html</a>:
36 ```html
37 &lt;!-- in TODO item --&gt;
38 <a ng-show="todo.uri" href="" ng-click="showUri(todo.uri)">(view url)</a></p>
mkearney1 2013/02/12 22:22:11 The (view url) is coming up as a link, and I don't
Renato Mangini (chromium) 2013/02/13 22:55:33 Done.
39
40 <!-- at the bottom, below the end of body -->
41
42 <p><webview></webview>
43 </pre></p></li><li><p>Set an appropriate width and height to the webview tag in <a href="https://github.com/GoogleChrome/chrome-app-codelab/blob/master/lab8_web resources/angularjs/1_webview/todo.css">todo.css</a> (it has zero size by defaul t):
44 <pre>
45 webview {
46 width: 100%;
47 height: 200px;
48 }
49 </pre></p></li>
50 <li><p>Thanks to AngularJS, we now only need to add the <code>showUri</code> met hod to our <a href="https://github.com/GoogleChrome/chrome-app-codelab/blob/mast er/lab8_webresources/angularjs/1_webview/controller.js">controller.js</a> and we &#39;re done:
51 <pre>
52 $scope.showUri = function(uri) {
53 var webview=document.querySelector(&quot;webview&quot;);
54 webview.src=uri;
55 };
56 </pre></p></li>
57 </ol>
58
59 <p>To test, open the app, right-click, and select Reload App.
60 You should be able to click on the &quot;view url&quot; link on any dropped URL Todo item, and the corresponding web page will show in the webview.
61 If it&#39;s not showing, inspect the page and check if you set the webview size appropriately.</p>
62
63 <p class="note"><b>Note:</b> If you get stuck and want to see the app in action , go to <code>chrome://extensions</code>,
64 load the unpacked <a href="https://github.com/GoogleChrome/chrome-app-codelab/tr ee/master/lab8_webresources/angularjs/1_webview">1_webview</a>, and launch the a pp from a new tab.</p>
65
66 <h2 id="loading_external_images">Loading external images</h2>
67
68 <p>If you try to add an <code>&lt;img&gt;</code> tag to your index.html, and poi nt its <code>src</code> attribute to any site on the web, the following exceptio n is thrown in the console and the image isn&#39;t loaded:</p>
mkearney1 2013/02/12 22:22:11 Put index.html in <code>.
Renato Mangini (chromium) 2013/02/13 22:55:33 Done.
69
70 <p class="note"><b></b> Refused to load the image &#39;http://angularjs.org/img/ AngularJS-large.png&#39; because it violates the following Content Security Poli cy directive: &quot;img-src &#39;self&#39; data: chrome-extension-resource:&quot ;.</p>
71
72 <p>Chrome apps cannot load any external resource directly in the DOM, because of the <a href="http://developer.chrome.com/apps/app_csp.html">CSP restrictions</a >.</p>
73
74 <p>To avoid this restriction, you can use XHR requests, grab the blob correspond ing to the remote file and use it appropriately.
75 For example, <code>&lt;img&gt;</code> tags can use a blob URL.
76 Let&#39;s change our application to show a small icon in the Todo list if the dr opped URL represents an image:</p>
77
78 <ol>
79 <li><p>Before you start firing XHR requests, you must request permissions.
80 Since we want to allow users to drag and drop images from any server, we need to request permission to XHR to any URL.
81 Change <a href="https://github.com/GoogleChrome/chrome-app-codelab/blob/master/l ab8_webresources/angularjs/2_loading_resources/manifest.json">manifest.json</a>:
82 <pre>
83 &quot;permissions&quot;: [&quot;storage&quot;, &quot;webview&quot;, &quot;&lt;al l_urls&gt;&quot;]
84 </pre></p></li>
85 <li><p>Add to your project a placeholder image <img src="https://github.com/Goog leChrome/chrome-app-codelab/raw/master/lab8_webresources/angularjs/2_loading_res ources/loading.gif" alt="loading.gif"> that will be shown while we are loading t he proper image.</p></li>
86 <li><p>Add the <code>&lt;img&gt;</code> tag to the Todo item on the <a href="htt ps://github.com/GoogleChrome/chrome-app-codelab/blob/master/lab8_webresources/an gularjs/2_loading_resources/index.html">index.html</a>:
87 <pre>
88 &lt;img style=&quot;max-height: 48px; max-width: 120px;&quot; ng-show=&quot;todo .validImage&quot; ng-src=&quot;&#123;&#123;todo.imageUrl&#125;&#125;&quot;&gt;&l t;/img&gt;
89 </pre>
90 As you will see soon, this element is only shown when the validImage attribute o f the Todo item is true.</p></li>
91 <li><p>Add the method loadImage (either in <a href="https://github.com/GoogleChr ome/chrome-app-codelab/blob/master/lab8_webresources/angularjs/2_loading_resourc es/controller.js">controller.js</a> or in a <a href="https://github.com/GoogleCh rome/chrome-app-codelab/blob/master/lab8_webresources/angularjs/2_loading_resour ces/loader.js">separate script file</a> as we did), that will start a XHR reques t and execute a callback with a Blob URL:
92 <pre>
93 var loadImage = function(uri, callback) {
94 var xhr = new XMLHttpRequest();
95 xhr.responseType = &#39;blob&#39;;
96 xhr.onload = function() {
97 callback(window.webkitURL.createObjectURL(xhr.response), uri);
98 }
99 xhr.open(&#39;GET&#39;, uri, true);
100 xhr.send();
101 }
102 </pre></p></li>
103 <li><p>In the <a href="https://github.com/GoogleChrome/chrome-app-codelab/blob/m aster/lab8_webresources/angularjs/2_loading_resources/controller.js">controller. js</a>, add a new method that will search the scope.todolist looking for images that are not loaded yet:
104 <pre>
105 // for each image with no imageUrl, start a new loader
106 $scope.loadImages = function() {
107 for (var i=0; i&lt;$scope.todos.length; i++) {
108 var todo=$scope.todos[i];
109 if (todo.validImage &amp;&amp; todo.imageUrl===PLACEHOLDER_IMAGE) {
110 loadImage(todo.uri, function(blob_uri, requested_uri) {
111 $scope.$apply(function(scope) {
112 for (var k=0; k&lt;scope.todos.length; k++) {
113 if (scope.todos[k].uri==requested_uri) {
114 scope.todos[k].imageUrl = blob_uri;
115 }
116 }
117 });
118 });
119 }
120 }
121 };
122 </pre></p></li>
123 <li><p>In the controller.js, drop() method, change the handling of URIs to appro priately detect a valid image. For simplicity sake, we only tested for png and j pg extensions. Feel free to have a better coverage in your code.
mkearney1 2013/02/12 22:22:11 Wrap in <code>.
Renato Mangini (chromium) 2013/02/13 22:55:33 Done.
124 <pre>
125 var uri=e.dataTransfer.getData(&quot;text/uri-list&quot;);
126 var todo = {text:uri, done:false, uri: uri};
127 if (/.png$/.test(uri) || /.jpg$/.test(uri)) {
128 hasImage = true;
129 todo.validImage = true;
130 todo.imageUrl = PLACEHOLDER_IMAGE;
131 }
132 newTodos.push(todo);
133
134 // [...] inside the $apply method, before save(), call the loadImages method:
135 $scope.loadImages();
136 </pre></p></li><li><p>And, finally, we will change the load method to reset the Blob URLs, since Blob URLs don&#39;t span through sessions.
137 Setting Todo&#39;s imageUrls to the PLACEHOLDER_IMAGE will force the loadImages method to request them again:
138 <pre>
139 // If there is saved data in storage, use it. Otherwise, bootstrap with sample t odos
140 $scope.load = function(value) {
141 if (value &amp;&amp; value.todolist) {
142 // ObjectURLs are revoked when the document is removed from memory,
143 // so we need to reload all images.
144 for (var i=0; i&lt;value.todolist.length; i++) {
145 value.todolist[i].imageUrl = PLACEHOLDER_IMAGE;
146 }
147 $scope.todos = value.todolist;
148 $scope.loadImages();
149 } else {
150 $scope.todos = [
151 {text:&#39;learn angular&#39;, done:true},
152 {text:&#39;build an angular app&#39;, done:false}];
153 }
154 }
155 </pre></p></li>
156 </ol>
157
158 <p>To test, open the app, right-click, and select Reload App.
159 Go to <a href="https://www.google.com/imghp?hl=en&amp;tab=wi&amp;authuser=0">Goo gle images</a>, search for and select an image,
160 then drag and drop the image into the Todo list app.
161 Assuming no mistakes were made, you should now have a thumbnail of every image U RL dropped into the Todo list app.</p>
162
163 <p class="note"><b>Note:</b> If you get stuck and want to see the app in action , go to <code>chrome://extensions</code>,
164 load the unpacked <a href="https://github.com/GoogleChrome/chrome-app-codelab/tr ee/master/lab8_webresources/angularjs/2_loading_resources">2_loading_resources</ a>, and launch the app from a new tab.</p>
165
166 <p>The loadImage() method above is not the best solution for this problem, becau se it doesn&#39;t handle errors correctly and it could cache images in a local f ilesystem.
mkearney1 2013/02/12 22:22:11 Put method in <code>.
Renato Mangini (chromium) 2013/02/13 22:55:33 Done.
167 We are working on a library that will be much more robust and easier to use.</p>
168
169 <h1 id="takeaways_">Takeaways:</h1>
170
171 <ul>
172 <li><p>The <code>&lt;webview&gt;</code> tag allows you to have a controlled brow ser inside your app.
173 You can use it if you have part of your application that is not CSP compatible a nd you don&#39;t have resources to migrate it immediately, for example.
174 One feature we didn&#39;t mention here is that WebViews can communicate with you r app and vice-versa using asynchronous <a href="https://developer.chrome.com/tr unk/apps/app_external.html#postMessage">postMessages</a>.</p></li>
175 <li><p>Loading resources like images from the web is not straightforward compare d to a standard web page.
176 But it&#39;s not too different from traditional native platforms, where you need to handle the resource download and, only when it is correctly downloaded, you can render it in the UI. We have also developed <a href="https://github.com/Goog leChrome/apps-resource-loader">a sample library</a> to assyncronously handle res ource loading from XHR calls. Feel free to use it in your projects.</p></li>
177 </ul>
178
179 <h1 id="what_39_s_next_">What&#39;s next?</h1>
180
181 <p>In <a href="app_codelab9_multipleviews.html">lab9_multipleviews</a>,
182 you will see how an app can have multiple windows that talk to each other and th e event page directly.</p>
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698