|
1 // Copyright (c) 2010 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.ui.actions; |
|
6 |
|
7 import java.io.ByteArrayOutputStream; |
|
8 import java.io.IOException; |
|
9 import java.io.InputStream; |
|
10 |
|
11 import org.chromium.debug.core.ChromiumDebugPlugin; |
|
12 import org.chromium.sdk.LiveEditExtension; |
|
13 import org.chromium.sdk.UpdatableScript; |
|
14 import org.eclipse.core.resources.IFile; |
|
15 import org.eclipse.core.runtime.CoreException; |
|
16 import org.eclipse.core.runtime.IStatus; |
|
17 import org.eclipse.core.runtime.Status; |
|
18 |
|
19 /** |
|
20 * The main action of LiveEdit feature. It gets the current state of a working file and pushes |
|
21 * it into running V8 VM. |
|
22 */ |
|
23 public class PushChangesAction extends V8ScriptAction { |
|
24 @Override |
|
25 protected void execute(final FilePair filePair) { |
|
26 UpdatableScript updatableScript = |
|
27 LiveEditExtension.castToUpdatableScript(filePair.getVmResource().getScript()); |
|
28 |
|
29 if (updatableScript == null) { |
|
30 throw new RuntimeException(); |
|
31 } |
|
32 |
|
33 byte[] fileData; |
|
34 try { |
|
35 fileData = readFileContents(filePair.getFile()); |
|
36 } catch (IOException e) { |
|
37 throw new RuntimeException(e); |
|
38 } catch (CoreException e) { |
|
39 throw new RuntimeException(e); |
|
40 } |
|
41 |
|
42 // We are using default charset here like usually. |
|
43 String newSource = new String(fileData); |
|
44 |
|
45 UpdatableScript.UpdateCallback callback = new UpdatableScript.UpdateCallback() { |
|
46 public void success(Object report) { |
|
47 ChromiumDebugPlugin.log(new Status(IStatus.OK, ChromiumDebugPlugin.PLUGIN_ID, |
|
48 "Script has been successfully updated on remote: " + report)); //$NON-NLS-1$ |
|
49 } |
|
50 public void failure(String message) { |
|
51 ChromiumDebugPlugin.log(new Status(IStatus.ERROR, ChromiumDebugPlugin.PLUGIN_ID, |
|
52 "Failed to change script on remote: " + message)); //$NON-NLS-1$ |
|
53 } |
|
54 }; |
|
55 |
|
56 updatableScript.setSourceOnRemote(newSource, callback, null); |
|
57 } |
|
58 |
|
59 |
|
60 private static byte[] readFileContents(IFile file) throws IOException, CoreException { |
|
61 InputStream inputStream = file.getContents(); |
|
62 try { |
|
63 return readBytes(inputStream); |
|
64 } finally { |
|
65 inputStream.close(); |
|
66 } |
|
67 } |
|
68 |
|
69 private static byte[] readBytes(InputStream inputStream) throws IOException { |
|
70 ByteArrayOutputStream buffer = new ByteArrayOutputStream(); |
|
71 byte[] array = new byte[1024]; |
|
72 while (true) { |
|
73 int len = inputStream.read(array); |
|
74 if (len == -1) { |
|
75 break; |
|
76 } |
|
77 buffer.write(array, 0, len); |
|
78 } |
|
79 return buffer.toByteArray(); |
|
80 } |
|
81 } |