Permalink
Browse files

Add a sample for the optional permissions API

  • Loading branch information...
mihaip committed Oct 17, 2012
1 parent f818cff commit 1cb6928d33ceca584384fd64cc38aa816ed2b95b
@@ -0,0 +1,10 @@
# Optional permissions API
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.
## APIs
* [Serial API](http://developer.chrome.com/apps/app.hardware.html#serial)
* [Runtime](http://developer.chrome.com/apps/app.runtime.html)
* [Window](http://developer.chrome.com/apps/app.window.html)
* [Permissions](http://developer.chrome.com/apps/permissions.html)
@@ -0,0 +1,7 @@
chrome.app.runtime.onLaunched.addListener(function() {
chrome.app.window.create('permissions.html', {
id: 'permissions',
width: 640,
height: 480
});
})
@@ -0,0 +1,17 @@
{
"name": "Optional permissions",
"version": "1",
"manifest_version": 2,
"minimum_chrome_version": "23",
"description": "Shows how to use the optional permissions API",
"app": {
"background": {
"scripts": [ "main.js" ]
}
},
"optional_permissions": [
"serial"
]
}
@@ -0,0 +1,33 @@
<!DOCTYPE html>
<html>
<head>
<style>
body {
margin: 0;
padding: 0;
}
#controls {
border-bottom: solid 1px #ccc;
padding: 2px;
}
#log {
white-space: pre-wrap;
font-family: monospace;
padding: 2px;
}
</style>
</head>
<body>
<div id="controls">
<button id="list-ports">List Serial Ports</button>
<button id="request-permission">Request "serial" permission</button>
<button id="check-permission">Check "serial" permission</button>
<script src="permissions.js"></script>
</div>
<div id="log"></div>
</body>
</html>
@@ -0,0 +1,40 @@
function log(message) {
document.querySelector('#log').textContent += message + '\n';
}
document.querySelector('#list-ports').onclick = function() {
try {
chrome.serial.getPorts(function(ports) {
if (chrome.runtime.lastError) {
log('Error when listing ports: ' + chrome.runtime.lastError.message);
return;
}
log('Serial ports: ');
ports.forEach(function(port) {
log(' ' + port);
});
});
} catch (ex) {
log('Exception when listing ports: ' + ex);
}
};
document.querySelector('#request-permission').onclick = function() {
chrome.permissions.request({permissions: ['serial']}, function(result) {
if (result) {
log('App was granted the "serial" permission.');
} else {
log('App was not granted the "serial" permission.');
}
});
};
document.querySelector('#check-permission').onclick = function() {
chrome.permissions.contains({permissions: ['serial']}, function(result) {
if (result) {
log('App has the "serial" permission.');
} else {
log('App does not have the "serial" permission.');
}
});
};

0 comments on commit 1cb6928

Please sign in to comment.