SBRemoteAPIHandler.js
Go to the documentation of this file.
1 // vim: set sw=2 :miv
2 //
3 // BEGIN SONGBIRD GPL
4 //
5 // This file is part of the Songbird web player.
6 //
7 // Copyright(c) 2005-2008 POTI, Inc.
8 // http://songbirdnest.com
9 //
10 // This file may be licensed under the terms of of the
11 // GNU General Public License Version 2 (the "GPL").
12 //
13 // Software distributed under the License is distributed
14 // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
15 // express or implied. See the GPL for the specific language
16 // governing rights and limitations.
17 //
18 // You should have received a copy of the GPL along with this
19 // program. If not, go to http://www.gnu.org/licenses/gpl.html
20 // or write to the Free Software Foundation, Inc.,
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 //
23 // END SONGBIRD GPL
24 //
25 
36 const Cc = Components.classes;
37 const Ci = Components.interfaces;
38 
39 if ("undefined" == typeof(SBString)) {
40  Components.utils.import("resource://app/jsmodules/StringUtils.jsm");
41 }
42 
43 function onRemoteAPI(event) {
44  // todo:
45  // hook up an action to do when the user clicks through, probably launch prepanel
46 
47  try {
48  var sbs = Cc["@mozilla.org/intl/stringbundle;1"]
49  .getService(Ci.nsIStringBundleService);
50  var songbirdStrings = sbs.createBundle("chrome://songbird/locale/songbird.properties");
51  var brandingStrings = sbs.createBundle("chrome://branding/locale/brand.properties");
52  } catch (e) {
53  /* just abort if we can't find strings. It's doubtful if we would ever
54  * get here anyway, but if we do, do the safest thing possible even if it
55  * sucks.
56  */
57  return;
58  }
59 
60  var notificationName = "remoteapi-called";
61  var message = SBString("rapi.access.message." + event.categoryID,
62  "Web Page has accessed %S directly",
63  songbirdStrings);
64  var editOptionsLabel = SBString("rapi.access.button.label.options",
65  "Edit Options...",
66  songbirdStrings);
67  var editOptionsAccessKey = SBString("rapi.access.button.accessKey.options",
68  "O",
69  songbirdStrings);
70  var allowAlwaysLabel = SBString("rapi.access.button.label.allow.always",
71  "Always Allow Site",
72  songbirdStrings);
73  var iconURL = SBString("rapi.access.iconURL", "", brandingStrings);
74  var appName = SBString("brandShortName", "Songbird", songbirdStrings);
75 
76  message = message.replace(/\%S/, appName);
77 
78  var closure = event;
79  var doc = event.currentTarget.document;
80 
81  var allowAlwaysCallback = function(aHat, aButtonInfo) {
82  var nsIPermissionManager = Components.interfaces.nsIPermissionManager;
83  var permManager = Components.classes["@mozilla.org/permissionmanager;1"]
84  .getService(nsIPermissionManager);
85  permManager.add(closure.siteScope, "rapi." + closure.categoryID, nsIPermissionManager.ALLOW_ACTION);
86 
87  var allowEvt = Components.classes["@songbirdnest.com/remoteapi/security-event;1"]
88  .createInstance(Components.interfaces.sbIMutableRemoteSecurityEvent);
89 
90  allowEvt.initSecurityEvent( doc,
91  closure.siteScope,
92  closure.category,
93  closure.categoryID,
94  true);
95 
96  aHat.closeEvent = allowEvt;
97  }
98 
99  var editOptionsCallback = function(aHat, aButtonInfo) {
100  var editEvt = Components.classes["@songbirdnest.com/remoteapi/security-event;1"]
101  .createInstance(Components.interfaces.sbIMutableRemoteSecurityEvent);
102 
103  // this causes an access denied event to be sent to the page
104  editEvt.initSecurityEvent( doc,
105  closure.siteScope,
106  closure.category,
107  closure.categoryID,
108  false);
109 
110  aHat.closeEvent = editEvt;
111 
112  var prefWindow = SBOpenPreferences("paneRemoteAPI");
113  if (prefWindow.gRemoteAPIPane) {
114  /* if the window is already open ask it to configure the
115  whitelist */
116  prefWindow.gRemoteAPIPane.configureWhitelist(closure.categoryID,
117  closure.siteScope.spec);
118  } else {
119  /* if it's not yet open, ask it to open the whitelist when
120  it's ready */
121  prefWindow.pleaseConfigureWhitelist =
122  [closure.categoryID, closure.siteScope.spec];
123  }
124  }
125 
126  var buttons = [
127  {
128  label: editOptionsLabel,
129  accessKey: editOptionsAccessKey,
130  popup: null,
131  callback: editOptionsCallback
132  },
133  {
134  label: allowAlwaysLabel,
135  accessKey: null,
136  popup: null,
137  callback: allowAlwaysCallback
138  }
139  ];
140 
141  var tabbrowser = event.currentTarget.gBrowser;
142  var browser = tabbrowser.getBrowserForDocument(event.target);
143  var notificationBox = tabbrowser.getNotificationBox(browser);
144  const priority = notificationBox.PRIORITY_WARNING_MEDIUM;
145  var notification = tabbrowser.showNotification(browser, notificationName,
146  message, iconURL, priority,
147  buttons);
148 
149  var defaultEvt = Components.classes["@songbirdnest.com/remoteapi/security-event;1"]
150  .createInstance(Components.interfaces.sbIMutableRemoteSecurityEvent);
151 
152  defaultEvt.initSecurityEvent( doc,
153  closure.siteScope,
154  closure.category,
155  closure.categoryID,
156  false);
157 
158  notification.closeEvent = defaultEvt;
159 }
const Cc
function doc() browser.contentDocument
function onRemoteAPI(event)
var event
function SBString(aKey, aDefault, aStringBundle)
Definition: StringUtils.jsm:93
this _contentSandbox label
Definition: FeedWriter.js:814
var tabbrowser
const Ci
grep callback
GstMessage * message
return null
Definition: FeedWriter.js:1143
const nsIPermissionManager
Definition: pageInfo.js:202
var browser
Definition: openLocation.js:42