Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | "use strict"; |
michael@0 | 6 | |
michael@0 | 7 | const { Cu, Cc, Ci, components } = require("chrome"); |
michael@0 | 8 | |
michael@0 | 9 | const { |
michael@0 | 10 | PROFILE_IDLE, |
michael@0 | 11 | PROFILE_RUNNING, |
michael@0 | 12 | PROFILE_COMPLETED, |
michael@0 | 13 | SHOW_PLATFORM_DATA, |
michael@0 | 14 | L10N_BUNDLE |
michael@0 | 15 | } = require("devtools/profiler/consts"); |
michael@0 | 16 | |
michael@0 | 17 | const { TextEncoder } = Cu.import("resource://gre/modules/commonjs/toolkit/loader.js", {}); |
michael@0 | 18 | |
michael@0 | 19 | var EventEmitter = require("devtools/toolkit/event-emitter"); |
michael@0 | 20 | var Cleopatra = require("devtools/profiler/cleopatra"); |
michael@0 | 21 | var Sidebar = require("devtools/profiler/sidebar"); |
michael@0 | 22 | var ProfilerController = require("devtools/profiler/controller"); |
michael@0 | 23 | var { Promise: promise } = Cu.import("resource://gre/modules/Promise.jsm", {}); |
michael@0 | 24 | |
michael@0 | 25 | Cu.import("resource:///modules/devtools/gDevTools.jsm"); |
michael@0 | 26 | Cu.import("resource://gre/modules/devtools/Console.jsm"); |
michael@0 | 27 | Cu.import("resource://gre/modules/Services.jsm"); |
michael@0 | 28 | Cu.import("resource:///modules/devtools/ViewHelpers.jsm"); |
michael@0 | 29 | Cu.import("resource://gre/modules/osfile.jsm"); |
michael@0 | 30 | Cu.import("resource://gre/modules/NetUtil.jsm"); |
michael@0 | 31 | |
michael@0 | 32 | loader.lazyGetter(this, "L10N", () => new ViewHelpers.L10N(L10N_BUNDLE)); |
michael@0 | 33 | |
michael@0 | 34 | /** |
michael@0 | 35 | * Profiler panel. It is responsible for creating and managing |
michael@0 | 36 | * different profile instances (see cleopatra.js). |
michael@0 | 37 | * |
michael@0 | 38 | * ProfilerPanel is an event emitter. It can emit the following |
michael@0 | 39 | * events: |
michael@0 | 40 | * |
michael@0 | 41 | * - ready: after the panel is done loading everything, |
michael@0 | 42 | * including the default profile instance. |
michael@0 | 43 | * - started: after the panel successfuly starts our SPS |
michael@0 | 44 | * profiler. |
michael@0 | 45 | * - stopped: after the panel successfuly stops our SPS |
michael@0 | 46 | * profiler and is ready to hand over profiling |
michael@0 | 47 | * data |
michael@0 | 48 | * - parsed: after Cleopatra finishes parsing profiling |
michael@0 | 49 | * data. |
michael@0 | 50 | * - destroyed: after the panel cleans up after itself and |
michael@0 | 51 | * is ready to be destroyed. |
michael@0 | 52 | * |
michael@0 | 53 | * The following events are used mainly by tests to prevent |
michael@0 | 54 | * accidential oranges: |
michael@0 | 55 | * |
michael@0 | 56 | * - profileCreated: after a new profile is created. |
michael@0 | 57 | * - profileSwitched: after user switches to a different |
michael@0 | 58 | * profile. |
michael@0 | 59 | */ |
michael@0 | 60 | function ProfilerPanel(frame, toolbox) { |
michael@0 | 61 | this.isReady = false; |
michael@0 | 62 | this.window = frame.window; |
michael@0 | 63 | this.document = frame.document; |
michael@0 | 64 | this.target = toolbox.target; |
michael@0 | 65 | |
michael@0 | 66 | this.profiles = new Map(); |
michael@0 | 67 | this._uid = 0; |
michael@0 | 68 | this._msgQueue = {}; |
michael@0 | 69 | |
michael@0 | 70 | EventEmitter.decorate(this); |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | ProfilerPanel.prototype = { |
michael@0 | 74 | isReady: null, |
michael@0 | 75 | window: null, |
michael@0 | 76 | document: null, |
michael@0 | 77 | target: null, |
michael@0 | 78 | controller: null, |
michael@0 | 79 | profiles: null, |
michael@0 | 80 | sidebar: null, |
michael@0 | 81 | |
michael@0 | 82 | _uid: null, |
michael@0 | 83 | _activeUid: null, |
michael@0 | 84 | _runningUid: null, |
michael@0 | 85 | _browserWin: null, |
michael@0 | 86 | _msgQueue: null, |
michael@0 | 87 | |
michael@0 | 88 | get controls() { |
michael@0 | 89 | let doc = this.document; |
michael@0 | 90 | |
michael@0 | 91 | return { |
michael@0 | 92 | get record() doc.querySelector("#profiler-start"), |
michael@0 | 93 | get import() doc.querySelector("#profiler-import"), |
michael@0 | 94 | }; |
michael@0 | 95 | }, |
michael@0 | 96 | |
michael@0 | 97 | get activeProfile() { |
michael@0 | 98 | return this.profiles.get(this._activeUid); |
michael@0 | 99 | }, |
michael@0 | 100 | |
michael@0 | 101 | set activeProfile(profile) { |
michael@0 | 102 | if (this._activeUid === profile.uid) |
michael@0 | 103 | return; |
michael@0 | 104 | |
michael@0 | 105 | if (this.activeProfile) |
michael@0 | 106 | this.activeProfile.hide(); |
michael@0 | 107 | |
michael@0 | 108 | this._activeUid = profile.uid; |
michael@0 | 109 | profile.show(); |
michael@0 | 110 | }, |
michael@0 | 111 | |
michael@0 | 112 | set recordingProfile(profile) { |
michael@0 | 113 | let btn = this.controls.record; |
michael@0 | 114 | this._runningUid = profile ? profile.uid : null; |
michael@0 | 115 | |
michael@0 | 116 | if (this._runningUid) |
michael@0 | 117 | btn.setAttribute("checked", true); |
michael@0 | 118 | else |
michael@0 | 119 | btn.removeAttribute("checked"); |
michael@0 | 120 | }, |
michael@0 | 121 | |
michael@0 | 122 | get recordingProfile() { |
michael@0 | 123 | return this.profiles.get(this._runningUid); |
michael@0 | 124 | }, |
michael@0 | 125 | |
michael@0 | 126 | get browserWindow() { |
michael@0 | 127 | if (this._browserWin) { |
michael@0 | 128 | return this._browserWin; |
michael@0 | 129 | } |
michael@0 | 130 | |
michael@0 | 131 | let win = this.window.top; |
michael@0 | 132 | let type = win.document.documentElement.getAttribute("windowtype"); |
michael@0 | 133 | |
michael@0 | 134 | if (type !== "navigator:browser") { |
michael@0 | 135 | win = Services.wm.getMostRecentWindow("navigator:browser"); |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | return this._browserWin = win; |
michael@0 | 139 | }, |
michael@0 | 140 | |
michael@0 | 141 | get showPlatformData() { |
michael@0 | 142 | return Services.prefs.getBoolPref(SHOW_PLATFORM_DATA); |
michael@0 | 143 | }, |
michael@0 | 144 | |
michael@0 | 145 | set showPlatformData(enabled) { |
michael@0 | 146 | Services.prefs.setBoolPref(SHOW_PLATFORM_DATA, enabled); |
michael@0 | 147 | }, |
michael@0 | 148 | |
michael@0 | 149 | /** |
michael@0 | 150 | * Open a debug connection and, on success, switch to the newly created |
michael@0 | 151 | * profile. |
michael@0 | 152 | * |
michael@0 | 153 | * @return Promise |
michael@0 | 154 | */ |
michael@0 | 155 | open: function PP_open() { |
michael@0 | 156 | // Local profiling needs to make the target remote. |
michael@0 | 157 | let target = this.target; |
michael@0 | 158 | let targetPromise = !target.isRemote ? target.makeRemote() : promise.resolve(target); |
michael@0 | 159 | |
michael@0 | 160 | return targetPromise |
michael@0 | 161 | .then((target) => { |
michael@0 | 162 | let deferred = promise.defer(); |
michael@0 | 163 | |
michael@0 | 164 | this.controller = new ProfilerController(this.target); |
michael@0 | 165 | this.sidebar = new Sidebar(this.document.querySelector("#profiles-list")); |
michael@0 | 166 | |
michael@0 | 167 | this.sidebar.on("save", (_, uid) => { |
michael@0 | 168 | let profile = this.profiles.get(uid); |
michael@0 | 169 | |
michael@0 | 170 | if (!profile.data) |
michael@0 | 171 | return void Cu.reportError("Can't save profile because there's no data."); |
michael@0 | 172 | |
michael@0 | 173 | this.openFileDialog({ mode: "save", name: profile.name }).then((file) => { |
michael@0 | 174 | if (file) |
michael@0 | 175 | this.saveProfile(file, profile.data); |
michael@0 | 176 | }); |
michael@0 | 177 | }); |
michael@0 | 178 | |
michael@0 | 179 | this.sidebar.on("select", (_, uid) => { |
michael@0 | 180 | let profile = this.profiles.get(uid); |
michael@0 | 181 | this.activeProfile = profile; |
michael@0 | 182 | |
michael@0 | 183 | if (profile.isReady) { |
michael@0 | 184 | return void this.emit("profileSwitched", profile.uid); |
michael@0 | 185 | } |
michael@0 | 186 | |
michael@0 | 187 | profile.once("ready", () => { |
michael@0 | 188 | this.emit("profileSwitched", profile.uid); |
michael@0 | 189 | }); |
michael@0 | 190 | }); |
michael@0 | 191 | |
michael@0 | 192 | this.controller.connect(() => { |
michael@0 | 193 | let btn = this.controls.record; |
michael@0 | 194 | btn.addEventListener("click", () => this.toggleRecording(), false); |
michael@0 | 195 | btn.removeAttribute("disabled"); |
michael@0 | 196 | |
michael@0 | 197 | let imp = this.controls.import; |
michael@0 | 198 | imp.addEventListener("click", () => { |
michael@0 | 199 | this.openFileDialog({ mode: "open" }).then((file) => { |
michael@0 | 200 | if (file) |
michael@0 | 201 | this.loadProfile(file); |
michael@0 | 202 | }); |
michael@0 | 203 | }, false); |
michael@0 | 204 | imp.removeAttribute("disabled"); |
michael@0 | 205 | |
michael@0 | 206 | // Import queued profiles. |
michael@0 | 207 | for (let [name, data] of this.controller.profiles) { |
michael@0 | 208 | this.importProfile(name, data.data); |
michael@0 | 209 | } |
michael@0 | 210 | |
michael@0 | 211 | this.isReady = true; |
michael@0 | 212 | this.emit("ready"); |
michael@0 | 213 | deferred.resolve(this); |
michael@0 | 214 | }); |
michael@0 | 215 | |
michael@0 | 216 | this.controller.on("profileEnd", (_, data) => { |
michael@0 | 217 | this.importProfile(data.name, data.data); |
michael@0 | 218 | |
michael@0 | 219 | if (this.recordingProfile && !data.fromConsole) |
michael@0 | 220 | this.recordingProfile = null; |
michael@0 | 221 | |
michael@0 | 222 | this.emit("stopped"); |
michael@0 | 223 | }); |
michael@0 | 224 | |
michael@0 | 225 | return deferred.promise; |
michael@0 | 226 | }) |
michael@0 | 227 | .then(null, (reason) => |
michael@0 | 228 | Cu.reportError("ProfilePanel open failed: " + reason.message)); |
michael@0 | 229 | }, |
michael@0 | 230 | |
michael@0 | 231 | /** |
michael@0 | 232 | * Creates a new profile instance (see cleopatra.js) and |
michael@0 | 233 | * adds an appropriate item to the sidebar. Note that |
michael@0 | 234 | * this method doesn't automatically switch user to |
michael@0 | 235 | * the newly created profile, they have do to switch |
michael@0 | 236 | * explicitly. |
michael@0 | 237 | * |
michael@0 | 238 | * @param string name |
michael@0 | 239 | * (optional) name of the new profile |
michael@0 | 240 | * |
michael@0 | 241 | * @return Profile |
michael@0 | 242 | */ |
michael@0 | 243 | createProfile: function (name, opts={}) { |
michael@0 | 244 | if (name && this.getProfileByName(name)) { |
michael@0 | 245 | return this.getProfileByName(name); |
michael@0 | 246 | } |
michael@0 | 247 | |
michael@0 | 248 | let uid = ++this._uid; |
michael@0 | 249 | let name = name || this.controller.getProfileName(); |
michael@0 | 250 | let profile = new Cleopatra(this, { |
michael@0 | 251 | uid: uid, |
michael@0 | 252 | name: name, |
michael@0 | 253 | showPlatformData: this.showPlatformData, |
michael@0 | 254 | external: opts.external |
michael@0 | 255 | }); |
michael@0 | 256 | |
michael@0 | 257 | this.profiles.set(uid, profile); |
michael@0 | 258 | this.sidebar.addProfile(profile); |
michael@0 | 259 | this.emit("profileCreated", uid); |
michael@0 | 260 | |
michael@0 | 261 | return profile; |
michael@0 | 262 | }, |
michael@0 | 263 | |
michael@0 | 264 | /** |
michael@0 | 265 | * Imports profile data |
michael@0 | 266 | * |
michael@0 | 267 | * @param string name, new profile name |
michael@0 | 268 | * @param object data, profile data to import |
michael@0 | 269 | * @param object opts, (optional) if property 'external' is found |
michael@0 | 270 | * Cleopatra will hide arrow buttons. |
michael@0 | 271 | * |
michael@0 | 272 | * @return Profile |
michael@0 | 273 | */ |
michael@0 | 274 | importProfile: function (name, data, opts={}) { |
michael@0 | 275 | let profile = this.createProfile(name, { external: opts.external }); |
michael@0 | 276 | profile.isStarted = false; |
michael@0 | 277 | profile.isFinished = true; |
michael@0 | 278 | profile.data = data; |
michael@0 | 279 | profile.parse(data, () => this.emit("parsed")); |
michael@0 | 280 | |
michael@0 | 281 | this.sidebar.setProfileState(profile, PROFILE_COMPLETED); |
michael@0 | 282 | if (!this.sidebar.selectedItem) |
michael@0 | 283 | this.sidebar.selectedItem = this.sidebar.getItemByProfile(profile); |
michael@0 | 284 | |
michael@0 | 285 | return profile; |
michael@0 | 286 | }, |
michael@0 | 287 | |
michael@0 | 288 | /** |
michael@0 | 289 | * Starts or stops profile recording. |
michael@0 | 290 | */ |
michael@0 | 291 | toggleRecording: function () { |
michael@0 | 292 | let profile = this.recordingProfile; |
michael@0 | 293 | |
michael@0 | 294 | if (!profile) { |
michael@0 | 295 | profile = this.createProfile(); |
michael@0 | 296 | |
michael@0 | 297 | this.startProfiling(profile.name, () => { |
michael@0 | 298 | profile.isStarted = true; |
michael@0 | 299 | |
michael@0 | 300 | this.sidebar.setProfileState(profile, PROFILE_RUNNING); |
michael@0 | 301 | this.recordingProfile = profile; |
michael@0 | 302 | this.emit("started"); |
michael@0 | 303 | }); |
michael@0 | 304 | |
michael@0 | 305 | return; |
michael@0 | 306 | } |
michael@0 | 307 | |
michael@0 | 308 | this.stopProfiling(profile.name, (data) => { |
michael@0 | 309 | profile.isStarted = false; |
michael@0 | 310 | profile.isFinished = true; |
michael@0 | 311 | profile.data = data; |
michael@0 | 312 | profile.parse(data, () => this.emit("parsed")); |
michael@0 | 313 | |
michael@0 | 314 | this.sidebar.setProfileState(profile, PROFILE_COMPLETED); |
michael@0 | 315 | this.activeProfile = profile; |
michael@0 | 316 | this.sidebar.selectedItem = this.sidebar.getItemByProfile(profile); |
michael@0 | 317 | this.recordingProfile = null; |
michael@0 | 318 | this.emit("stopped"); |
michael@0 | 319 | }); |
michael@0 | 320 | }, |
michael@0 | 321 | |
michael@0 | 322 | /** |
michael@0 | 323 | * Start collecting profile data. |
michael@0 | 324 | * |
michael@0 | 325 | * @param function onStart |
michael@0 | 326 | * A function to call once we get the message |
michael@0 | 327 | * that profiling had been successfuly started. |
michael@0 | 328 | */ |
michael@0 | 329 | startProfiling: function (name, onStart) { |
michael@0 | 330 | this.controller.start(name, (err) => { |
michael@0 | 331 | if (err) { |
michael@0 | 332 | return void Cu.reportError("ProfilerController.start: " + err.message); |
michael@0 | 333 | } |
michael@0 | 334 | |
michael@0 | 335 | onStart(); |
michael@0 | 336 | this.emit("started"); |
michael@0 | 337 | }); |
michael@0 | 338 | }, |
michael@0 | 339 | |
michael@0 | 340 | /** |
michael@0 | 341 | * Stop collecting profile data. |
michael@0 | 342 | * |
michael@0 | 343 | * @param function onStop |
michael@0 | 344 | * A function to call once we get the message |
michael@0 | 345 | * that profiling had been successfuly stopped. |
michael@0 | 346 | */ |
michael@0 | 347 | stopProfiling: function (name, onStop) { |
michael@0 | 348 | this.controller.isActive((err, isActive) => { |
michael@0 | 349 | if (err) { |
michael@0 | 350 | Cu.reportError("ProfilerController.isActive: " + err.message); |
michael@0 | 351 | return; |
michael@0 | 352 | } |
michael@0 | 353 | |
michael@0 | 354 | if (!isActive) { |
michael@0 | 355 | return; |
michael@0 | 356 | } |
michael@0 | 357 | |
michael@0 | 358 | this.controller.stop(name, (err, data) => { |
michael@0 | 359 | if (err) { |
michael@0 | 360 | Cu.reportError("ProfilerController.stop: " + err.message); |
michael@0 | 361 | return; |
michael@0 | 362 | } |
michael@0 | 363 | |
michael@0 | 364 | onStop(data); |
michael@0 | 365 | this.emit("stopped", data); |
michael@0 | 366 | }); |
michael@0 | 367 | }); |
michael@0 | 368 | }, |
michael@0 | 369 | |
michael@0 | 370 | /** |
michael@0 | 371 | * Lookup an individual profile by its name. |
michael@0 | 372 | * |
michael@0 | 373 | * @param string name name of the profile |
michael@0 | 374 | * @return profile object or null |
michael@0 | 375 | */ |
michael@0 | 376 | getProfileByName: function PP_getProfileByName(name) { |
michael@0 | 377 | if (!this.profiles) { |
michael@0 | 378 | return null; |
michael@0 | 379 | } |
michael@0 | 380 | |
michael@0 | 381 | for (let [ uid, profile ] of this.profiles) { |
michael@0 | 382 | if (profile.name === name) { |
michael@0 | 383 | return profile; |
michael@0 | 384 | } |
michael@0 | 385 | } |
michael@0 | 386 | |
michael@0 | 387 | return null; |
michael@0 | 388 | }, |
michael@0 | 389 | |
michael@0 | 390 | /** |
michael@0 | 391 | * Lookup an individual profile by its UID. |
michael@0 | 392 | * |
michael@0 | 393 | * @param number uid UID of the profile |
michael@0 | 394 | * @return profile object or null |
michael@0 | 395 | */ |
michael@0 | 396 | getProfileByUID: function PP_getProfileByUID(uid) { |
michael@0 | 397 | if (!this.profiles) { |
michael@0 | 398 | return null; |
michael@0 | 399 | } |
michael@0 | 400 | |
michael@0 | 401 | return this.profiles.get(uid) || null; |
michael@0 | 402 | }, |
michael@0 | 403 | |
michael@0 | 404 | /** |
michael@0 | 405 | * Iterates over each available profile and calls |
michael@0 | 406 | * a callback with it as a parameter. |
michael@0 | 407 | * |
michael@0 | 408 | * @param function cb a callback to call |
michael@0 | 409 | */ |
michael@0 | 410 | eachProfile: function PP_eachProfile(cb) { |
michael@0 | 411 | let uid = this._uid; |
michael@0 | 412 | |
michael@0 | 413 | if (!this.profiles) { |
michael@0 | 414 | return; |
michael@0 | 415 | } |
michael@0 | 416 | |
michael@0 | 417 | while (uid >= 0) { |
michael@0 | 418 | if (this.profiles.has(uid)) { |
michael@0 | 419 | cb(this.profiles.get(uid)); |
michael@0 | 420 | } |
michael@0 | 421 | |
michael@0 | 422 | uid -= 1; |
michael@0 | 423 | } |
michael@0 | 424 | }, |
michael@0 | 425 | |
michael@0 | 426 | /** |
michael@0 | 427 | * Broadcast messages to all Cleopatra instances. |
michael@0 | 428 | * |
michael@0 | 429 | * @param number target |
michael@0 | 430 | * UID of the recepient profile. All profiles will receive the message |
michael@0 | 431 | * but the profile specified by 'target' will have a special property, |
michael@0 | 432 | * isCurrent, set to true. |
michael@0 | 433 | * @param object data |
michael@0 | 434 | * An object with a property 'task' that will be sent over to Cleopatra. |
michael@0 | 435 | */ |
michael@0 | 436 | broadcast: function PP_broadcast(target, data) { |
michael@0 | 437 | if (!this.profiles) { |
michael@0 | 438 | return; |
michael@0 | 439 | } |
michael@0 | 440 | |
michael@0 | 441 | this.eachProfile((profile) => { |
michael@0 | 442 | profile.message({ |
michael@0 | 443 | uid: target, |
michael@0 | 444 | isCurrent: target === profile.uid, |
michael@0 | 445 | task: data.task |
michael@0 | 446 | }); |
michael@0 | 447 | }); |
michael@0 | 448 | }, |
michael@0 | 449 | |
michael@0 | 450 | /** |
michael@0 | 451 | * Open file specified in data in either a debugger or view-source. |
michael@0 | 452 | * |
michael@0 | 453 | * @param object data |
michael@0 | 454 | * An object describing the file. It must have three properties: |
michael@0 | 455 | * - uri |
michael@0 | 456 | * - line |
michael@0 | 457 | * - isChrome (chrome files are opened via view-source) |
michael@0 | 458 | */ |
michael@0 | 459 | displaySource: function PP_displaySource(data) { |
michael@0 | 460 | let { browserWindow: win, document: doc } = this; |
michael@0 | 461 | let { uri, line, isChrome } = data; |
michael@0 | 462 | let deferred = promise.defer(); |
michael@0 | 463 | |
michael@0 | 464 | if (isChrome) { |
michael@0 | 465 | return void win.gViewSourceUtils.viewSource(uri, null, doc, line); |
michael@0 | 466 | } |
michael@0 | 467 | |
michael@0 | 468 | let showSource = ({ DebuggerView }) => { |
michael@0 | 469 | if (DebuggerView.Sources.containsValue(uri)) { |
michael@0 | 470 | DebuggerView.setEditorLocation(uri, line).then(deferred.resolve); |
michael@0 | 471 | } |
michael@0 | 472 | // XXX: What to do if the source isn't present in the Debugger? |
michael@0 | 473 | // Switch back to the Profiler panel and viewSource()? |
michael@0 | 474 | } |
michael@0 | 475 | |
michael@0 | 476 | // If the Debugger was already open, switch to it and try to show the |
michael@0 | 477 | // source immediately. Otherwise, initialize it and wait for the sources |
michael@0 | 478 | // to be added first. |
michael@0 | 479 | let toolbox = gDevTools.getToolbox(this.target); |
michael@0 | 480 | let debuggerAlreadyOpen = toolbox.getPanel("jsdebugger"); |
michael@0 | 481 | toolbox.selectTool("jsdebugger").then(({ panelWin: dbg }) => { |
michael@0 | 482 | if (debuggerAlreadyOpen) { |
michael@0 | 483 | showSource(dbg); |
michael@0 | 484 | } else { |
michael@0 | 485 | dbg.once(dbg.EVENTS.SOURCES_ADDED, () => showSource(dbg)); |
michael@0 | 486 | } |
michael@0 | 487 | }); |
michael@0 | 488 | |
michael@0 | 489 | return deferred.promise; |
michael@0 | 490 | }, |
michael@0 | 491 | |
michael@0 | 492 | /** |
michael@0 | 493 | * Opens a normal file dialog. |
michael@0 | 494 | * |
michael@0 | 495 | * @params object opts, (optional) property 'mode' can be used to |
michael@0 | 496 | * specify which dialog to open. Can be either |
michael@0 | 497 | * 'save' or 'open' (default is 'open'). |
michael@0 | 498 | * @return promise |
michael@0 | 499 | */ |
michael@0 | 500 | openFileDialog: function (opts={}) { |
michael@0 | 501 | let deferred = promise.defer(); |
michael@0 | 502 | |
michael@0 | 503 | let picker = Ci.nsIFilePicker; |
michael@0 | 504 | let fp = Cc["@mozilla.org/filepicker;1"].createInstance(picker); |
michael@0 | 505 | let { name, mode } = opts; |
michael@0 | 506 | let save = mode === "save"; |
michael@0 | 507 | let title = L10N.getStr(save ? "profiler.saveFileAs" : "profiler.openFile"); |
michael@0 | 508 | |
michael@0 | 509 | fp.init(this.window, title, save ? picker.modeSave : picker.modeOpen); |
michael@0 | 510 | fp.appendFilter("JSON", "*.json"); |
michael@0 | 511 | fp.appendFilters(picker.filterText | picker.filterAll); |
michael@0 | 512 | |
michael@0 | 513 | if (save) |
michael@0 | 514 | fp.defaultString = (name || "profile") + ".json"; |
michael@0 | 515 | |
michael@0 | 516 | fp.open((result) => { |
michael@0 | 517 | deferred.resolve(result === picker.returnCancel ? null : fp.file); |
michael@0 | 518 | }); |
michael@0 | 519 | |
michael@0 | 520 | return deferred.promise; |
michael@0 | 521 | }, |
michael@0 | 522 | |
michael@0 | 523 | /** |
michael@0 | 524 | * Saves profile data to disk |
michael@0 | 525 | * |
michael@0 | 526 | * @param File file |
michael@0 | 527 | * @param object data |
michael@0 | 528 | * |
michael@0 | 529 | * @return promise |
michael@0 | 530 | */ |
michael@0 | 531 | saveProfile: function (file, data) { |
michael@0 | 532 | let encoder = new TextEncoder(); |
michael@0 | 533 | let buffer = encoder.encode(JSON.stringify({ profile: data }, null, " ")); |
michael@0 | 534 | let opts = { tmpPath: file.path + ".tmp" }; |
michael@0 | 535 | |
michael@0 | 536 | return OS.File.writeAtomic(file.path, buffer, opts); |
michael@0 | 537 | }, |
michael@0 | 538 | |
michael@0 | 539 | /** |
michael@0 | 540 | * Reads profile data from disk |
michael@0 | 541 | * |
michael@0 | 542 | * @param File file |
michael@0 | 543 | * @return promise |
michael@0 | 544 | */ |
michael@0 | 545 | loadProfile: function (file) { |
michael@0 | 546 | let deferred = promise.defer(); |
michael@0 | 547 | let ch = NetUtil.newChannel(file); |
michael@0 | 548 | ch.contentType = "application/json"; |
michael@0 | 549 | |
michael@0 | 550 | NetUtil.asyncFetch(ch, (input, status) => { |
michael@0 | 551 | if (!components.isSuccessCode(status)) throw new Error(status); |
michael@0 | 552 | |
michael@0 | 553 | let conv = Cc["@mozilla.org/intl/scriptableunicodeconverter"] |
michael@0 | 554 | .createInstance(Ci.nsIScriptableUnicodeConverter); |
michael@0 | 555 | conv.charset = "UTF-8"; |
michael@0 | 556 | |
michael@0 | 557 | let data = NetUtil.readInputStreamToString(input, input.available()); |
michael@0 | 558 | data = conv.ConvertToUnicode(data); |
michael@0 | 559 | this.importProfile(file.leafName, JSON.parse(data).profile, { external: true }); |
michael@0 | 560 | |
michael@0 | 561 | deferred.resolve(); |
michael@0 | 562 | }); |
michael@0 | 563 | |
michael@0 | 564 | return deferred.promise; |
michael@0 | 565 | }, |
michael@0 | 566 | |
michael@0 | 567 | /** |
michael@0 | 568 | * Cleanup. |
michael@0 | 569 | */ |
michael@0 | 570 | destroy: function PP_destroy() { |
michael@0 | 571 | if (this.profiles) { |
michael@0 | 572 | let uid = this._uid; |
michael@0 | 573 | |
michael@0 | 574 | while (uid >= 0) { |
michael@0 | 575 | if (this.profiles.has(uid)) { |
michael@0 | 576 | this.profiles.get(uid).destroy(); |
michael@0 | 577 | this.profiles.delete(uid); |
michael@0 | 578 | } |
michael@0 | 579 | uid -= 1; |
michael@0 | 580 | } |
michael@0 | 581 | } |
michael@0 | 582 | |
michael@0 | 583 | if (this.controller) { |
michael@0 | 584 | this.controller.destroy(); |
michael@0 | 585 | } |
michael@0 | 586 | |
michael@0 | 587 | this.isReady = null; |
michael@0 | 588 | this.window = null; |
michael@0 | 589 | this.document = null; |
michael@0 | 590 | this.target = null; |
michael@0 | 591 | this.controller = null; |
michael@0 | 592 | this.profiles = null; |
michael@0 | 593 | this._uid = null; |
michael@0 | 594 | this._activeUid = null; |
michael@0 | 595 | |
michael@0 | 596 | this.emit("destroyed"); |
michael@0 | 597 | } |
michael@0 | 598 | }; |
michael@0 | 599 | |
michael@0 | 600 | module.exports = ProfilerPanel; |