Skip to content
This repository
Browse code

Add a sample for the optional permissions API

  • Loading branch information...
commit 1cb6928d33ceca584384fd64cc38aa816ed2b95b 1 parent f818cff
Mihai Parparita mihaip authored
10 optional-permissions/README.md
Source Rendered
... ... @@ -0,0 +1,10 @@
  1 +# Optional permissions API
  2 +
  3 +This app shows to use the permissions API to request optional permissions. It can list the attached serial ports, but only if the "serial" permission is granted. The app shows how to check if the permission is granted and how to request it if it is not.
  4 +
  5 +## APIs
  6 +
  7 +* [Serial API](http://developer.chrome.com/apps/app.hardware.html#serial)
  8 +* [Runtime](http://developer.chrome.com/apps/app.runtime.html)
  9 +* [Window](http://developer.chrome.com/apps/app.window.html)
  10 +* [Permissions](http://developer.chrome.com/apps/permissions.html)
7 optional-permissions/main.js
... ... @@ -0,0 +1,7 @@
  1 +chrome.app.runtime.onLaunched.addListener(function() {
  2 + chrome.app.window.create('permissions.html', {
  3 + id: 'permissions',
  4 + width: 640,
  5 + height: 480
  6 + });
  7 +})
17 optional-permissions/manifest.json
... ... @@ -0,0 +1,17 @@
  1 +{
  2 + "name": "Optional permissions",
  3 + "version": "1",
  4 + "manifest_version": 2,
  5 + "minimum_chrome_version": "23",
  6 + "description": "Shows how to use the optional permissions API",
  7 +
  8 + "app": {
  9 + "background": {
  10 + "scripts": [ "main.js" ]
  11 + }
  12 + },
  13 +
  14 + "optional_permissions": [
  15 + "serial"
  16 + ]
  17 +}
33 optional-permissions/permissions.html
... ... @@ -0,0 +1,33 @@
  1 +<!DOCTYPE html>
  2 +<html>
  3 +<head>
  4 + <style>
  5 + body {
  6 + margin: 0;
  7 + padding: 0;
  8 + }
  9 +
  10 + #controls {
  11 + border-bottom: solid 1px #ccc;
  12 + padding: 2px;
  13 + }
  14 +
  15 + #log {
  16 + white-space: pre-wrap;
  17 + font-family: monospace;
  18 + padding: 2px;
  19 + }
  20 + </style>
  21 +</head>
  22 +<body>
  23 + <div id="controls">
  24 + <button id="list-ports">List Serial Ports</button>
  25 + <button id="request-permission">Request "serial" permission</button>
  26 + <button id="check-permission">Check "serial" permission</button>
  27 + <script src="permissions.js"></script>
  28 + </div>
  29 +
  30 + <div id="log"></div>
  31 +
  32 +</body>
  33 +</html>
40 optional-permissions/permissions.js
... ... @@ -0,0 +1,40 @@
  1 +function log(message) {
  2 + document.querySelector('#log').textContent += message + '\n';
  3 +}
  4 +
  5 +document.querySelector('#list-ports').onclick = function() {
  6 + try {
  7 + chrome.serial.getPorts(function(ports) {
  8 + if (chrome.runtime.lastError) {
  9 + log('Error when listing ports: ' + chrome.runtime.lastError.message);
  10 + return;
  11 + }
  12 + log('Serial ports: ');
  13 + ports.forEach(function(port) {
  14 + log(' ' + port);
  15 + });
  16 + });
  17 + } catch (ex) {
  18 + log('Exception when listing ports: ' + ex);
  19 + }
  20 +};
  21 +
  22 +document.querySelector('#request-permission').onclick = function() {
  23 + chrome.permissions.request({permissions: ['serial']}, function(result) {
  24 + if (result) {
  25 + log('App was granted the "serial" permission.');
  26 + } else {
  27 + log('App was not granted the "serial" permission.');
  28 + }
  29 + });
  30 +};
  31 +
  32 +document.querySelector('#check-permission').onclick = function() {
  33 + chrome.permissions.contains({permissions: ['serial']}, function(result) {
  34 + if (result) {
  35 + log('App has the "serial" permission.');
  36 + } else {
  37 + log('App does not have the "serial" permission.');
  38 + }
  39 + });
  40 +};

0 comments on commit 1cb6928

Please sign in to comment.
Something went wrong with that request. Please try again.