- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / common / extensions / docs / examples / api / downloads / download_open / background.js
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 function getOpeningIds() {
6   var ids = [];
7   try {
8     ids = JSON.parse(localStorage.openWhenComplete);
9   } catch (e) {
10     localStorage.openWhenComplete = JSON.stringify(ids);
11   }
12   return ids;
13 }
14
15 function setOpeningIds(ids) {
16   localStorage.openWhenComplete = JSON.stringify(ids);
17 }
18
19 chrome.downloads.onChanged.addListener(function(delta) {
20   if (!delta.state ||
21       (delta.state.current != 'complete')) {
22     return;
23   }
24   var ids = getOpeningIds();
25   if (ids.indexOf(delta.id) < 0) {
26     return;
27   }
28   chrome.downloads.open(delta.id);
29   ids.splice(ids.indexOf(delta.id), 1);
30   setOpeningIds(ids);
31 });
32
33 chrome.contextMenus.onClicked.addListener(function(info, tab) {
34   chrome.downloads.download({url: info.linkUrl}, function(downloadId) {
35     var ids = getOpeningIds();
36     if (ids.indexOf(downloadId) >= 0) {
37       return;
38     }
39     ids.push(downloadId);
40     setOpeningIds(ids);
41   });
42 });
43
44 chrome.contextMenus.create({
45   id: 'open',
46   title: chrome.i18n.getMessage('openContextMenuTitle'),
47   contexts: ['link'],
48 });