1/*
2 * Copyright (C) 2013 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26WebInspector.BranchManager = function()
27{
28    WebInspector.Object.call(this);
29
30    WebInspector.Frame.addEventListener(WebInspector.Frame.Event.MainResourceDidChange, this._mainResourceDidChange, this);
31
32    this.initialize();
33}
34
35WebInspector.BranchManager.prototype = {
36    constructor: WebInspector.BranchManager,
37
38    // Public
39
40    initialize: function()
41    {
42        this._originalBranch = new WebInspector.Branch(WebInspector.UIString("Original"), null, true);
43        this._currentBranch = this._originalBranch.fork(WebInspector.UIString("Working Copy"));
44        this._branches = [this._originalBranch, this._currentBranch];
45    },
46
47    get branches()
48    {
49        return this._branches;
50    },
51
52    get currentBranch()
53    {
54        return this._currentBranch;
55    },
56
57    set currentBranch(branch)
58    {
59        console.assert(branch instanceof WebInspector.Branch);
60        if (!(branch instanceof WebInspector.Branch))
61            return;
62
63        this._currentBranch.revert();
64
65        this._currentBranch = branch;
66
67        this._currentBranch.apply();
68    },
69
70    createBranch: function(displayName, fromBranch)
71    {
72        if (!fromBranch)
73            fromBranch = this._originalBranch;
74
75        console.assert(fromBranch instanceof WebInspector.Branch);
76        if (!(fromBranch instanceof WebInspector.Branch))
77            return;
78
79        var newBranch = fromBranch.fork(displayName);
80        this._branches.push(newBranch);
81        return newBranch;
82    },
83
84    deleteBranch: function(branch)
85    {
86        console.assert(branch instanceof WebInspector.Branch);
87        if (!(branch instanceof WebInspector.Branch))
88            return;
89
90        console.assert(branch !== this._originalBranch);
91        if (branch === this._originalBranch)
92            return;
93
94        this._branches.remove(branch);
95
96        if (branch === this._currentBranch)
97            this._currentBranch = this._originalBranch;
98    },
99
100    // Private
101
102    _mainResourceDidChange: function(event)
103    {
104        console.assert(event.target instanceof WebInspector.Frame);
105
106        if (!event.target.isMainFrame())
107            return;
108
109        this.initialize();
110    }
111};
112
113WebInspector.BranchManager.prototype.__proto__ = WebInspector.Object.prototype;
114