1/* Copyright 2012 Mozilla Foundation
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15/* jshint esnext:true */
16/* globals Components, Services, XPCOMUtils */
17
18'use strict';
19
20var EXPORTED_SYMBOLS = ['PdfjsContentUtils'];
21
22const Cc = Components.classes;
23const Ci = Components.interfaces;
24const Cr = Components.results;
25const Cu = Components.utils;
26
27Cu.import('resource://gre/modules/XPCOMUtils.jsm');
28Cu.import('resource://gre/modules/Services.jsm');
29
30var PdfjsContentUtils = {
31  _mm: null,
32
33  /*
34   * Public API
35   */
36
37  get isRemote() {
38    return (Services.appinfo.processType ===
39            Services.appinfo.PROCESS_TYPE_CONTENT);
40  },
41
42  init: function () {
43    // child *process* mm, or when loaded into the parent for in-content
44    // support the psuedo child process mm 'child PPMM'.
45    if (!this._mm) {
46      this._mm = Cc['@mozilla.org/childprocessmessagemanager;1'].
47        getService(Ci.nsISyncMessageSender);
48      this._mm.addMessageListener('PDFJS:Child:refreshSettings', this);
49      Services.obs.addObserver(this, 'quit-application', false);
50    }
51  },
52
53  uninit: function () {
54    if (this._mm) {
55      this._mm.removeMessageListener('PDFJS:Child:refreshSettings', this);
56      Services.obs.removeObserver(this, 'quit-application');
57    }
58    this._mm = null;
59  },
60
61  /*
62   * prefs utilities - the child does not have write access to prefs.
63   * note, the pref names here are cross-checked against a list of
64   * approved pdfjs prefs in chrome utils.
65   */
66
67  clearUserPref: function (aPrefName) {
68    this._mm.sendSyncMessage('PDFJS:Parent:clearUserPref', {
69      name: aPrefName
70    });
71  },
72
73  setIntPref: function (aPrefName, aPrefValue) {
74    this._mm.sendSyncMessage('PDFJS:Parent:setIntPref', {
75      name: aPrefName,
76      value: aPrefValue
77    });
78  },
79
80  setBoolPref: function (aPrefName, aPrefValue) {
81    this._mm.sendSyncMessage('PDFJS:Parent:setBoolPref', {
82      name: aPrefName,
83      value: aPrefValue
84    });
85  },
86
87  setCharPref: function (aPrefName, aPrefValue) {
88    this._mm.sendSyncMessage('PDFJS:Parent:setCharPref', {
89      name: aPrefName,
90      value: aPrefValue
91    });
92  },
93
94  setStringPref: function (aPrefName, aPrefValue) {
95    this._mm.sendSyncMessage('PDFJS:Parent:setStringPref', {
96      name: aPrefName,
97      value: aPrefValue
98    });
99  },
100
101  /*
102   * Forwards default app query to the parent where we check various
103   * handler app settings only available in the parent process.
104   */
105  isDefaultHandlerApp: function () {
106    return this._mm.sendSyncMessage('PDFJS:Parent:isDefaultHandlerApp')[0];
107  },
108
109  /*
110   * Request the display of a notification warning in the associated window
111   * when the renderer isn't sure a pdf displayed correctly.
112   */
113  displayWarning: function (aWindow, aMessage, aLabel, accessKey) {
114    // the child's dom frame mm associated with the window.
115    let winmm = aWindow.QueryInterface(Ci.nsIInterfaceRequestor)
116                       .getInterface(Ci.nsIDocShell)
117                       .QueryInterface(Ci.nsIInterfaceRequestor)
118                       .getInterface(Ci.nsIContentFrameMessageManager);
119    winmm.sendAsyncMessage('PDFJS:Parent:displayWarning', {
120      message: aMessage,
121      label: aLabel,
122      accessKey: accessKey
123    });
124  },
125
126  /*
127   * Events
128   */
129
130  observe: function(aSubject, aTopic, aData) {
131    if (aTopic === 'quit-application') {
132      this.uninit();
133    }
134  },
135
136  receiveMessage: function (aMsg) {
137    switch (aMsg.name) {
138      case 'PDFJS:Child:refreshSettings':
139        // Only react to this if we are remote.
140        if (Services.appinfo.processType ===
141            Services.appinfo.PROCESS_TYPE_CONTENT) {
142          let jsm = 'resource://pdf.js/PdfJs.jsm';
143          let pdfjs = Components.utils.import(jsm, {}).PdfJs;
144          pdfjs.updateRegistration();
145        }
146        break;
147    }
148  }
149};
150
151