|
1 // Copyright (c) 2009 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 package org.chromium.debug.core.model; |
|
6 |
|
7 import java.util.HashMap; |
|
8 import java.util.LinkedList; |
|
9 import java.util.List; |
|
10 import java.util.Map; |
|
11 |
|
12 import org.chromium.debug.core.ChromiumDebugPlugin; |
|
13 import org.chromium.debug.core.model.BreakpointRegistry.BreakpointEntry; |
|
14 import org.chromium.debug.core.model.BreakpointRegistry.ScriptIdentifier; |
|
15 import org.chromium.debug.core.util.ChromiumDebugPluginUtil; |
|
16 import org.chromium.sdk.Script; |
|
17 import org.eclipse.core.resources.IFile; |
|
18 import org.eclipse.core.resources.IProject; |
|
19 import org.eclipse.core.resources.ResourcesPlugin; |
|
20 import org.eclipse.core.runtime.CoreException; |
|
21 import org.eclipse.debug.core.DebugPlugin; |
|
22 |
|
23 /** |
|
24 * This object handles the mapping between {@link Script}s and their corresponding resources |
|
25 * inside Eclipse. |
|
26 */ |
|
27 public class ResourceManager implements IResourceManager { |
|
28 private final Map<IFile, Script> resourceToScript = new HashMap<IFile, Script>(); |
|
29 private final Map<ScriptIdentifier, IFile> scriptIdToResource = |
|
30 new HashMap<ScriptIdentifier, IFile>(); |
|
31 private final IProject debugProject; |
|
32 private final BreakpointRegistry breakpointRegistry; |
|
33 private Object fileBeingAdded; |
|
34 |
|
35 public ResourceManager(IProject debugProject, BreakpointRegistry breakpointRegistry) { |
|
36 this.debugProject = debugProject; |
|
37 this.breakpointRegistry = breakpointRegistry; |
|
38 } |
|
39 |
|
40 public synchronized void putScript(Script script, IFile resource) { |
|
41 ScriptIdentifier scriptId = ScriptIdentifier.forScript(script); |
|
42 resourceToScript.put(resource, script); |
|
43 scriptIdToResource.put(scriptId, resource); |
|
44 } |
|
45 |
|
46 public synchronized Script getScript(IFile resource) { |
|
47 return resourceToScript.get(resource); |
|
48 } |
|
49 |
|
50 public synchronized IFile getResource(Script script) { |
|
51 return scriptIdToResource.get(ScriptIdentifier.forScript(script)); |
|
52 } |
|
53 |
|
54 public synchronized boolean scriptHasResource(Script script) { |
|
55 return getResource(script) != null; |
|
56 } |
|
57 |
|
58 public synchronized void clear() { |
|
59 deleteAllScriptFiles(); |
|
60 resourceToScript.clear(); |
|
61 scriptIdToResource.clear(); |
|
62 } |
|
63 |
|
64 private void deleteAllScriptFiles() { |
|
65 if (!resourceToScript.isEmpty()) { |
|
66 try { |
|
67 ResourcesPlugin.getWorkspace().delete( |
|
68 resourceToScript.keySet().toArray(new IFile[resourceToScript.size()]), true, null); |
|
69 } catch (CoreException e) { |
|
70 ChromiumDebugPlugin.log(e); |
|
71 } |
|
72 } |
|
73 } |
|
74 |
|
75 public synchronized void addScript(Script script) { |
|
76 IFile scriptFile = getResource(script); |
|
77 if (scriptFile == null) { |
|
78 scriptFile = ChromiumDebugPluginUtil.createFile(debugProject, getScriptResourceName(script)); |
|
79 fileBeingAdded = scriptFile; |
|
80 try { |
|
81 putScript(script, scriptFile); |
|
82 writeScriptSource(script, scriptFile); |
|
83 // Perhaps restore breakpoints for the reloaded script |
|
84 List<ChromiumLineBreakpoint> breakpoints = new LinkedList<ChromiumLineBreakpoint>(); |
|
85 for (BreakpointEntry entry : breakpointRegistry.getBreakpointEntries(script)) { |
|
86 ChromiumLineBreakpoint lineBreakpoint; |
|
87 try { |
|
88 lineBreakpoint = new ChromiumLineBreakpoint(scriptFile, entry.line + 1); |
|
89 } catch (CoreException e) { |
|
90 ChromiumDebugPlugin.log(e); |
|
91 continue; |
|
92 } |
|
93 lineBreakpoint.setBreakpoint(entry.breakpoint); |
|
94 breakpoints.add(lineBreakpoint); |
|
95 } |
|
96 if (!breakpoints.isEmpty()) { |
|
97 try { |
|
98 DebugPlugin.getDefault().getBreakpointManager().addBreakpoints( |
|
99 breakpoints.toArray(new ChromiumLineBreakpoint[breakpoints.size()])); |
|
100 } catch (CoreException e) { |
|
101 ChromiumDebugPlugin.log(e); |
|
102 } |
|
103 } |
|
104 } finally { |
|
105 fileBeingAdded = null; |
|
106 } |
|
107 } |
|
108 } |
|
109 |
|
110 private String getScriptResourceName(Script script) { |
|
111 String name = script.getName(); |
|
112 if (name == null) { |
|
113 name = Messages.ResourceManager_UnnamedScriptName; |
|
114 } |
|
115 return name; |
|
116 } |
|
117 |
|
118 private static void writeScriptSource(Script script, IFile file) { |
|
119 if (script.hasSource()) { |
|
120 try { |
|
121 ChromiumDebugPluginUtil.writeFile(file, script.getSource()); |
|
122 } catch (final CoreException e) { |
|
123 ChromiumDebugPlugin.log(e); |
|
124 } |
|
125 } |
|
126 } |
|
127 |
|
128 /** |
|
129 * @return whether the given file is being added to the target project |
|
130 */ |
|
131 public boolean isAddingFile(IFile file) { |
|
132 return file.equals(fileBeingAdded); |
|
133 } |
|
134 } |