- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / subscribe_page_action / background.js
1 // Copyright (c) 2012 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 chrome.extension.onMessage.addListener(function(request, sender) {
6   if (request.msg == "feedIcon") {
7     // First validate that all the URLs have the right schema.
8     var input = [];
9     for (var i = 0; i < request.feeds.length; ++i) {
10       var a = document.createElement('a');
11       a.href = request.feeds[i].href;
12       if (a.protocol == "http:" || a.protocol == "https:") {
13         input.push(request.feeds[i]);
14       } else {
15         console.log('Warning: feed source rejected (wrong protocol): ' +
16                     request.feeds[i].href);
17       }
18     }
19
20     if (input.length == 0)
21       return;  // We've rejected all the input, so abort.
22
23     // We have received a list of feed urls found on the page.
24     var feeds = {};
25     feeds[sender.tab.id] = input;
26     chrome.storage.local.set(feeds, function() {
27       // Enable the page action icon.
28       chrome.pageAction.setTitle(
29         { tabId: sender.tab.id,
30           title: chrome.i18n.getMessage("rss_subscription_action_title")
31         });
32       chrome.pageAction.show(sender.tab.id);
33     });
34   } else if (request.msg == "feedDocument") {
35     // We received word from the content script that this document
36     // is an RSS feed (not just a document linking to the feed).
37     // So, we go straight to the subscribe page in a new tab and
38     // navigate back on the current page (to get out of the xml page).
39     // We don't want to navigate in-place because trying to go back
40     // from the subscribe page takes us back to the xml page, which
41     // will redirect to the subscribe page again (we don't support a
42     // location.replace equivalant in the Tab navigation system).
43     chrome.tabs.executeScript(sender.tab.id,
44         { code: "if (history.length > 1) " +
45                  "history.go(-1); else window.close();"
46         });
47     var url = "subscribe.html?" + encodeURIComponent(request.href);
48     url = chrome.extension.getURL(url);
49     chrome.tabs.create({ url: url, index: sender.tab.index });
50   }
51 });
52
53 chrome.tabs.onRemoved.addListener(function(tabId) {
54   chrome.storage.local.remove(tabId.toString());
55 });