提交记录修改internal/clipboardfiles/ownership.go+103 −4看文件
修改internal/clipboardfiles/ownership_test.go+21 −0看文件
修改internal/clipboardfiles/ownership_unix.go+11 −1看文件
修改internal/clipboardfiles/ownership_windows.go+13 −1看文件
修改internal/workbench/image_input.go+7 −0看文件
修改internal/workbench/server.go+1 −0看文件
修改internal/workbench/windows_browser.go+65 −0看文件
修改internal/workbench/windows_browser_client.go+22 −0看文件
修改internal/workbench/windows_browser_routes_test.go+35 −0看文件
| @@ -5,6 +5,7 @@ import ( | ||
| 5 | 5 | "encoding/hex" |
| 6 | 6 | "encoding/json" |
| 7 | 7 | "errors" |
| 8 | 新增: "io" | |
| 8 | 9 | "os" |
| 9 | 10 | "path/filepath" |
| 10 | 11 | "strings" |
| @@ -25,6 +26,8 @@ type cacheRecord struct { | ||
| 25 | 26 | Marker string `json:"marker"` |
| 26 | 27 | Owner int `json:"owner"` |
| 27 | 28 | Published bool `json:"published"` |
| 29 | 新增: Pending bool `json:"pending,omitempty"` | |
| 30 | 新增: Producer int `json:"producer,omitempty"` | |
| 28 | 31 | } |
| 29 | 32 | |
| 30 | 33 | type CacheStore struct{ Directory string } |
| @@ -34,6 +37,22 @@ type CacheRequest struct { | ||
| 34 | 37 | ID string `json:"cache_id,omitempty"` |
| 35 | 38 | } |
| 36 | 39 | |
| 40 | 新增:func (store CacheStore) lock() (func(), error) { | |
| 41 | 新增: if err := host.EnsurePrivateDir(store.Directory); err != nil { | |
| 42 | 新增: return nil, err | |
| 43 | 新增: } | |
| 44 | 新增: file, err := os.OpenFile(filepath.Join(store.Directory, ".lock"), os.O_CREATE|os.O_RDWR, 0600) | |
| 45 | 新增: if err != nil { | |
| 46 | 新增: return nil, err | |
| 47 | 新增: } | |
| 48 | 新增: unlock, err := lockCacheStore(file) | |
| 49 | 新增: if err != nil { | |
| 50 | 新增: file.Close() | |
| 51 | 新增: return nil, err | |
| 52 | 新增: } | |
| 53 | 新增: return func() { unlock(); file.Close() }, nil | |
| 54 | 新增:} | |
| 55 | 新增: | |
| 37 | 56 | func (store CacheStore) Control(request CacheRequest) (map[string]any, error) { |
| 38 | 57 | if request.Action == "list" { |
| 39 | 58 | selections, err := store.List() |
| @@ -61,6 +80,10 @@ func (store CacheStore) recordPath(id string) (string, error) { | ||
| 61 | 80 | // Register takes ownership only of a directory just returned by Read, Copy, |
| 62 | 81 | // Save or LinkCachedFiles. Existing user selections never enter this method. |
| 63 | 82 | func (store CacheStore) Register(paths []string) (CachedSelection, error) { |
| 83 | 新增: return store.register(paths, false) | |
| 84 | 新增:} | |
| 85 | 新增: | |
| 86 | 新增:func (store CacheStore) register(paths []string, pending bool) (CachedSelection, error) { | |
| 64 | 87 | var empty CachedSelection |
| 65 | 88 | if len(paths) == 0 || len(paths) > SelectionLimit { |
| 66 | 89 | return empty, errors.New("invalid clipboard cache selection") |
| @@ -71,9 +94,11 @@ func (store CacheStore) Register(paths []string) (CachedSelection, error) { | ||
| 71 | 94 | return empty, errors.New("invalid clipboard cache directory") |
| 72 | 95 | } |
| 73 | 96 | } |
| 74 | 删除: if err := host.EnsurePrivateDir(store.Directory); err != nil { | |
| 97 | 新增: unlock, err := store.lock() | |
| 98 | 新增: if err != nil { | |
| 75 | 99 | return empty, err |
| 76 | 100 | } |
| 101 | 新增: defer unlock() | |
| 77 | 102 | var token [16]byte |
| 78 | 103 | if _, err := rand.Read(token[:]); err != nil { |
| 79 | 104 | return empty, err |
| @@ -89,7 +114,7 @@ func (store CacheStore) Register(paths []string) (CachedSelection, error) { | ||
| 89 | 114 | _ = os.Remove(marker.Name()) |
| 90 | 115 | return empty, err |
| 91 | 116 | } |
| 92 | 删除: record := cacheRecord{CachedSelection: selection, Directory: directory, Marker: filepath.Base(marker.Name()), Owner: os.Getppid()} | |
| 117 | 新增: record := cacheRecord{CachedSelection: selection, Directory: directory, Marker: filepath.Base(marker.Name()), Owner: os.Getppid(), Pending: pending, Producer: os.Getpid()} | |
| 93 | 118 | if err := store.save(record); err != nil { |
| 94 | 119 | _ = os.Remove(marker.Name()) |
| 95 | 120 | return empty, err |
| @@ -97,6 +122,65 @@ func (store CacheStore) Register(paths []string) (CachedSelection, error) { | ||
| 97 | 122 | return selection, nil |
| 98 | 123 | } |
| 99 | 124 | |
| 125 | 新增:// create registers the new directory before any bytes are copied. A terminated | |
| 126 | 新增:// producer leaves an exact receipt that the native owner can recover. | |
| 127 | 新增:func (store CacheStore) create(operation func(func() (string, error)) ([]string, error)) (result CachedSelection, err error) { | |
| 128 | 新增: var created CachedSelection | |
| 129 | 新增: defer func() { | |
| 130 | 新增: if err != nil && created.ID != "" { | |
| 131 | 新增: _ = store.Release(created.ID) | |
| 132 | 新增: } | |
| 133 | 新增: }() | |
| 134 | 新增: paths, err := operation(func() (string, error) { | |
| 135 | 新增: root, err := temporaryDirectory() | |
| 136 | 新增: if err != nil { | |
| 137 | 新增: return "", err | |
| 138 | 新增: } | |
| 139 | 新增: created, err = store.register([]string{filepath.Join(root, ".pending")}, true) | |
| 140 | 新增: if err != nil { | |
| 141 | 新增: _ = os.RemoveAll(root) | |
| 142 | 新增: } | |
| 143 | 新增: return root, err | |
| 144 | 新增: }) | |
| 145 | 新增: if err != nil { | |
| 146 | 新增: return result, err | |
| 147 | 新增: } | |
| 148 | 新增: unlock, err := store.lock() | |
| 149 | 新增: if err != nil { | |
| 150 | 新增: return result, err | |
| 151 | 新增: } | |
| 152 | 新增: defer unlock() | |
| 153 | 新增: record, err := store.load(created.ID) | |
| 154 | 新增: if err != nil { | |
| 155 | 新增: return result, err | |
| 156 | 新增: } | |
| 157 | 新增: record.Paths = paths | |
| 158 | 新增: record.Pending = false | |
| 159 | 新增: if err := store.save(record); err != nil { | |
| 160 | 新增: return result, err | |
| 161 | 新增: } | |
| 162 | 新增: return record.CachedSelection, nil | |
| 163 | 新增:} | |
| 164 | 新增: | |
| 165 | 新增:func (store CacheStore) Read(input io.Reader) (CachedSelection, error) { | |
| 166 | 新增: return store.create(func(directory func() (string, error)) ([]string, error) { return read(input, directory) }) | |
| 167 | 新增:} | |
| 168 | 新增: | |
| 169 | 新增:func (store CacheStore) Copy(paths []string) (CachedSelection, error) { | |
| 170 | 新增: return store.create(func(directory func() (string, error)) ([]string, error) { return copyFiles(paths, directory) }) | |
| 171 | 新增:} | |
| 172 | 新增: | |
| 173 | 新增:func (store CacheStore) Save(input io.Reader, name string) (CachedSelection, error) { | |
| 174 | 新增: return store.create(func(directory func() (string, error)) ([]string, error) { | |
| 175 | 新增: path, err := save(input, name, directory) | |
| 176 | 新增: return []string{path}, err | |
| 177 | 新增: }) | |
| 178 | 新增:} | |
| 179 | 新增: | |
| 180 | 新增:func (store CacheStore) LinkCachedFiles(paths []string) (CachedSelection, error) { | |
| 181 | 新增: return store.create(func(directory func() (string, error)) ([]string, error) { return linkCachedFiles(paths, directory) }) | |
| 182 | 新增:} | |
| 183 | 新增: | |
| 100 | 184 | func (store CacheStore) save(record cacheRecord) error { |
| 101 | 185 | path, err := store.recordPath(record.ID) |
| 102 | 186 | if err != nil { |
| @@ -129,7 +213,7 @@ func (store CacheStore) load(id string) (cacheRecord, error) { | ||
| 129 | 213 | } |
| 130 | 214 | if json.Unmarshal(data, &record) != nil || record.ID != id || len(record.Paths) == 0 || |
| 131 | 215 | !filepath.IsAbs(record.Directory) || filepath.Clean(record.Directory) != record.Directory || |
| 132 | 删除: !validName(record.Marker) || record.Owner < 1 { | |
| 216 | 新增: !validName(record.Marker) || record.Owner < 1 || record.Pending && record.Producer < 1 { | |
| 133 | 217 | return record, errors.New("invalid clipboard cache record") |
| 134 | 218 | } |
| 135 | 219 | for _, path := range record.Paths { |
| @@ -141,6 +225,11 @@ func (store CacheStore) load(id string) (cacheRecord, error) { | ||
| 141 | 225 | } |
| 142 | 226 | |
| 143 | 227 | func (store CacheStore) Publish(id string) error { |
| 228 | 新增: unlock, err := store.lock() | |
| 229 | 新增: if err != nil { | |
| 230 | 新增: return err | |
| 231 | 新增: } | |
| 232 | 新增: defer unlock() | |
| 144 | 233 | record, err := store.load(id) |
| 145 | 234 | if err != nil { |
| 146 | 235 | return err |
| @@ -152,6 +241,11 @@ func (store CacheStore) Publish(id string) error { | ||
| 152 | 241 | // List recovers published copies and abandoned receives from their creation |
| 153 | 242 | // records. A live native owner retains admission for its unpublished transfers. |
| 154 | 243 | func (store CacheStore) List() ([]CachedSelection, error) { |
| 244 | 新增: unlock, err := store.lock() | |
| 245 | 新增: if err != nil { | |
| 246 | 新增: return nil, err | |
| 247 | 新增: } | |
| 248 | 新增: defer unlock() | |
| 155 | 249 | entries, err := os.ReadDir(store.Directory) |
| 156 | 250 | if errors.Is(err, os.ErrNotExist) { |
| 157 | 251 | return []CachedSelection{}, nil |
| @@ -168,7 +262,7 @@ func (store CacheStore) List() ([]CachedSelection, error) { | ||
| 168 | 262 | if err != nil { |
| 169 | 263 | return nil, err |
| 170 | 264 | } |
| 171 | 删除: if record.Published || !cacheOwnerAlive(record.Owner) { | |
| 265 | 新增: if record.Pending && !cacheOwnerAlive(record.Producer) || !record.Pending && (record.Published || !cacheOwnerAlive(record.Owner)) { | |
| 172 | 266 | selections = append(selections, record.CachedSelection) |
| 173 | 267 | } |
| 174 | 268 | } |
| @@ -178,6 +272,11 @@ func (store CacheStore) List() ([]CachedSelection, error) { | ||
| 178 | 272 | // Release runs after the native owner has checked the current OS selection and |
| 179 | 273 | // its in-flight readers. A Windows sharing violation keeps the receipt for retry. |
| 180 | 274 | func (store CacheStore) Release(id string) error { |
| 275 | 新增: unlock, err := store.lock() | |
| 276 | 新增: if err != nil { | |
| 277 | 新增: return err | |
| 278 | 新增: } | |
| 279 | 新增: defer unlock() | |
| 181 | 280 | record, err := store.load(id) |
| 182 | 281 | if errors.Is(err, os.ErrNotExist) { |
| 183 | 282 | return nil |
| @@ -99,3 +99,24 @@ func TestCacheRejectsForeignOrMalformedIdentifiers(t *testing.T) { | ||
| 99 | 99 | t.Fatal(err) |
| 100 | 100 | } |
| 101 | 101 | } |
| 102 | 新增: | |
| 103 | 新增:func TestCacheConcurrentPublishCannotRecreateAReleasedReceipt(t *testing.T) { | |
| 104 | 新增: for range 20 { | |
| 105 | 新增: store, selection := cacheFixture(t) | |
| 106 | 新增: start := make(chan struct{}) | |
| 107 | 新增: published := make(chan error, 1) | |
| 108 | 新增: released := make(chan error, 1) | |
| 109 | 新增: go func() { <-start; published <- store.Publish(selection.ID) }() | |
| 110 | 新增: go func() { <-start; released <- store.Release(selection.ID) }() | |
| 111 | 新增: close(start) | |
| 112 | 新增: if err := <-published; err != nil && !os.IsNotExist(err) { | |
| 113 | 新增: t.Fatal(err) | |
| 114 | 新增: } | |
| 115 | 新增: if err := <-released; err != nil { | |
| 116 | 新增: t.Fatal(err) | |
| 117 | 新增: } | |
| 118 | 新增: if _, err := store.load(selection.ID); !os.IsNotExist(err) { | |
| 119 | 新增: t.Fatal("a concurrent publication recreated a retired receipt", err) | |
| 120 | 新增: } | |
| 121 | 新增: } | |
| 122 | 新增:} | |
| @@ -2,7 +2,17 @@ | ||
| 2 | 2 | |
| 3 | 3 | package clipboardfiles |
| 4 | 4 | |
| 5 | 删除:import "syscall" | |
| 5 | 新增:import ( | |
| 6 | 新增: "os" | |
| 7 | 新增: "syscall" | |
| 8 | 新增:) | |
| 9 | 新增: | |
| 10 | 新增:func lockCacheStore(file *os.File) (func(), error) { | |
| 11 | 新增: if err := syscall.Flock(int(file.Fd()), syscall.LOCK_EX); err != nil { | |
| 12 | 新增: return nil, err | |
| 13 | 新增: } | |
| 14 | 新增: return func() { _ = syscall.Flock(int(file.Fd()), syscall.LOCK_UN) }, nil | |
| 15 | 新增:} | |
| 6 | 16 | |
| 7 | 17 | func cacheOwnerAlive(pid int) bool { |
| 8 | 18 | err := syscall.Kill(pid, 0) |
| @@ -1,6 +1,18 @@ | ||
| 1 | 1 | package clipboardfiles |
| 2 | 2 | |
| 3 | 删除:import "golang.org/x/sys/windows" | |
| 3 | 新增:import ( | |
| 4 | 新增: "os" | |
| 5 | 新增: | |
| 6 | 新增: "golang.org/x/sys/windows" | |
| 7 | 新增:) | |
| 8 | 新增: | |
| 9 | 新增:func lockCacheStore(file *os.File) (func(), error) { | |
| 10 | 新增: var overlapped windows.Overlapped | |
| 11 | 新增: if err := windows.LockFileEx(windows.Handle(file.Fd()), windows.LOCKFILE_EXCLUSIVE_LOCK, 0, 1, 0, &overlapped); err != nil { | |
| 12 | 新增: return nil, err | |
| 13 | 新增: } | |
| 14 | 新增: return func() { _ = windows.UnlockFileEx(windows.Handle(file.Fd()), 0, 1, 0, &overlapped) }, nil | |
| 15 | 新增:} | |
| 4 | 16 | |
| 5 | 17 | func cacheOwnerAlive(pid int) bool { |
| 6 | 18 | handle, err := windows.OpenProcess(windows.SYNCHRONIZE, false, uint32(pid)) |
| @@ -148,6 +148,13 @@ func decodeInputImage(encoded string) ([]byte, error) { | ||
| 148 | 148 | if err != nil || len(data) == 0 || len(data) > maxImageBytes { |
| 149 | 149 | return nil, errors.New("图片数据无效") |
| 150 | 150 | } |
| 151 | 新增: return validateInputImage(data) | |
| 152 | 新增:} | |
| 153 | 新增: | |
| 154 | 新增:func validateInputImage(data []byte) ([]byte, error) { | |
| 155 | 新增: if len(data) == 0 || len(data) > maxImageBytes { | |
| 156 | 新增: return nil, errors.New("图片数据无效") | |
| 157 | 新增: } | |
| 151 | 158 | config, err := png.DecodeConfig(bytes.NewReader(data)) |
| 152 | 159 | if err != nil || config.Width <= 0 || config.Height <= 0 || int64(config.Width)*int64(config.Height) > 40_000_000 { |
| 153 | 160 | return nil, errors.New("需要有效的 PNG 图片,且不能超过 4000 万像素") |
| @@ -404,6 +404,7 @@ func (s *Server) handler() http.Handler { | ||
| 404 | 404 | mux.HandleFunc("POST /api/tailscale/{action}", s.tailnet) |
| 405 | 405 | mux.HandleFunc("GET /api/browser/tabs", s.browser) |
| 406 | 406 | mux.HandleFunc("GET /api/browser/events", s.browserEvents) |
| 407 | 新增: mux.HandleFunc("POST /api/browser/clipboard", s.browserClipboard) | |
| 407 | 408 | mux.HandleFunc("GET /api/browser/preferences", s.browserPreferences) |
| 408 | 409 | mux.HandleFunc("POST /api/browser/preferences", s.browserPreferences) |
| 409 | 410 | mux.HandleFunc("POST /api/browser/extensions/upload", s.browserExtensionUpload) |
| @@ -2,6 +2,7 @@ package workbench | ||
| 2 | 2 | |
| 3 | 3 | import ( |
| 4 | 4 | "context" |
| 5 | 新增: "encoding/base64" | |
| 5 | 6 | "encoding/json" |
| 6 | 7 | "errors" |
| 7 | 8 | "fmt" |
| @@ -11,6 +12,7 @@ import ( | ||
| 11 | 12 | "net/url" |
| 12 | 13 | "os/exec" |
| 13 | 14 | "time" |
| 15 | 新增: "unicode/utf8" | |
| 14 | 16 | |
| 15 | 17 | "github.com/sylvinus/agent-vm/internal/diagnostics" |
| 16 | 18 | ) |
| @@ -65,6 +67,69 @@ func (s *Server) nativeBrowserAPI(ctx context.Context, method, path string, body | ||
| 65 | 67 | return s.nativeBrowserClient().request(ctx, method, path, input) |
| 66 | 68 | } |
| 67 | 69 | |
| 70 | 新增:type browserClipboardInput struct { | |
| 71 | 新增: ID string `json:"id"` | |
| 72 | 新增: Tab string `json:"tab"` | |
| 73 | 新增: Context string `json:"context"` | |
| 74 | 新增: Document string `json:"document"` | |
| 75 | 新增:} | |
| 76 | 新增: | |
| 77 | 新增:func (s *Server) browserClipboard(w http.ResponseWriter, r *http.Request) { | |
| 78 | 新增: var input browserClipboardInput | |
| 79 | 新增: if !readJSON(w, r, &input) { | |
| 80 | 新增: return | |
| 81 | 新增: } | |
| 82 | 新增: if !browserFileID.MatchString(input.ID) || !validInputToken(input.Tab) || !validInputToken(input.Context) || !validInputToken(input.Document) { | |
| 83 | 新增: http.Error(w, "复制内容或网页上下文无效", http.StatusBadRequest) | |
| 84 | 新增: return | |
| 85 | 新增: } | |
| 86 | 新增: s.mu.Lock() | |
| 87 | 新增: ready := s.ready | |
| 88 | 新增: s.mu.Unlock() | |
| 89 | 新增: if !ready { | |
| 90 | 新增: http.Error(w, "环境尚未就绪", http.StatusConflict) | |
| 91 | 新增: return | |
| 92 | 新增: } | |
| 93 | 新增: if s.macosGuest == nil { | |
| 94 | 新增: http.Error(w, "当前客机使用系统共享剪贴板", http.StatusNotImplemented) | |
| 95 | 新增: return | |
| 96 | 新增: } | |
| 97 | 新增: ctx, cancel := context.WithTimeout(r.Context(), 30*time.Second) | |
| 98 | 新增: defer cancel() | |
| 99 | 新增: reader, size, mime, err := s.nativeBrowserClient().downloadClipboard(ctx, input) | |
| 100 | 新增: if err != nil { | |
| 101 | 新增: http.Error(w, err.Error(), http.StatusBadGateway) | |
| 102 | 新增: return | |
| 103 | 新增: } | |
| 104 | 新增: defer reader.Close() | |
| 105 | 新增: limit := int64(maxImageBytes) | |
| 106 | 新增: if mime == "text/plain" { | |
| 107 | 新增: limit = 2 << 20 | |
| 108 | 新增: } | |
| 109 | 新增: if (mime != "image/png" && mime != "text/plain") || size > limit { | |
| 110 | 新增: http.Error(w, "客机复制内容的类型或大小无效", http.StatusBadGateway) | |
| 111 | 新增: return | |
| 112 | 新增: } | |
| 113 | 新增: data, err := io.ReadAll(io.LimitReader(reader, limit+1)) | |
| 114 | 新增: if err != nil || int64(len(data)) != size { | |
| 115 | 新增: http.Error(w, "复制内容未完整收到,请重新复制", http.StatusBadGateway) | |
| 116 | 新增: return | |
| 117 | 新增: } | |
| 118 | 新增: if mime == "text/plain" { | |
| 119 | 新增: if !utf8.Valid(data) { | |
| 120 | 新增: http.Error(w, "复制文字的编码无效", http.StatusBadGateway) | |
| 121 | 新增: return | |
| 122 | 新增: } | |
| 123 | 新增: jsonReply(w, map[string]string{"text": string(data)}) | |
| 124 | 新增: return | |
| 125 | 新增: } | |
| 126 | 新增: if _, err := validateInputImage(data); err != nil { | |
| 127 | 新增: http.Error(w, err.Error(), http.StatusBadGateway) | |
| 128 | 新增: return | |
| 129 | 新增: } | |
| 130 | 新增: jsonReply(w, map[string]string{"png": base64.StdEncoding.EncodeToString(data)}) | |
| 131 | 新增:} | |
| 132 | 新增: | |
| 68 | 133 | func (s *Server) subscribeNativeBrowser(parent context.Context) io.ReadCloser { |
| 69 | 134 | ctx, cancel := context.WithCancel(parent) |
| 70 | 135 | reader, writer := io.Pipe() |
| @@ -260,6 +260,10 @@ func (c *windowsBrowserClient) request(ctx context.Context, method, path string, | ||
| 260 | 260 | "project_grant_unavailable": "此项目授权已断开,请重新连接后再操作", |
| 261 | 261 | "extensions_unavailable": "客机浏览器扩展管理未就绪,请核对浏览器版本和安装组件", |
| 262 | 262 | "clipboard_copy_not_observed": "尚未确认客机剪贴板发生复制,请重新选择内容后复制", |
| 263 | 新增: "copy_unavailable": "未收到新的复制内容,请重新选择后复制", | |
| 264 | 新增: "copy_changed": "复制来源已切换,请重新复制", | |
| 265 | 新增: "copy_limit": "复制内容超过大小限制", | |
| 266 | 新增: "copy_image_invalid": "复制的 PNG 图片无效", | |
| 263 | 267 | "action_unavailable": "客机浏览器尚未支持这项操作", |
| 264 | 268 | "target_changed": "接收操作的网页已经切换,请回到原网页重试", |
| 265 | 269 | "browser_target_changed": "客机浏览器窗口已变化,请在新画面中重试", |
| @@ -389,6 +393,24 @@ func (c *windowsBrowserClient) download(ctx context.Context, id string) (io.Read | ||
| 389 | 393 | return openNativeFileDownload(ctx, id, c.request) |
| 390 | 394 | } |
| 391 | 395 | |
| 396 | 新增:func (c *windowsBrowserClient) downloadClipboard(ctx context.Context, input browserClipboardInput) (io.ReadCloser, int64, string, error) { | |
| 397 | 新增: var metadata struct { | |
| 398 | 新增: Mime string `json:"mime"` | |
| 399 | 新增: } | |
| 400 | 新增: request := func(ctx context.Context, method, path string, value any) ([]byte, error) { | |
| 401 | 新增: if path == "/file-open" { | |
| 402 | 新增: value = map[string]any{"direction": "download", "id": input.ID, "tab": input.Tab, "context": input.Context, "document": input.Document} | |
| 403 | 新增: } | |
| 404 | 新增: data, err := c.request(ctx, method, path, value) | |
| 405 | 新增: if err == nil && path == "/file-open" { | |
| 406 | 新增: _ = json.Unmarshal(data, &metadata) | |
| 407 | 新增: } | |
| 408 | 新增: return data, err | |
| 409 | 新增: } | |
| 410 | 新增: reader, size, _, err := openNativeFileDownload(ctx, input.ID, request) | |
| 411 | 新增: return reader, size, metadata.Mime, err | |
| 412 | 新增:} | |
| 413 | 新增: | |
| 392 | 414 | func openNativeFileDownload(ctx context.Context, id string, request func(context.Context, string, string, any) ([]byte, error)) (io.ReadCloser, int64, string, error) { |
| 393 | 415 | if !browserFileID.MatchString(id) { |
| 394 | 416 | return nil, 0, "", browserInputError{errors.New("下载标识无效")} |
| @@ -90,6 +90,41 @@ func TestNativeBrowserTextHTTPPreservesTwoMiBAndRequiresPreparation(t *testing.T | ||
| 90 | 90 | } |
| 91 | 91 | } |
| 92 | 92 | |
| 93 | 新增:func TestNativeBrowserClipboardHTTPReadsBoundPNGAndLargeText(t *testing.T) { | |
| 94 | 新增: s := testServer(t) | |
| 95 | 新增: s.macosGuest, s.ready = &guestos.MacOSProfile{}, true | |
| 96 | 新增: s.windowsBrowserControl = fixtureWindowsBrowserClient(t) | |
| 97 | 新增: for _, id := range []string{"11111111-1111-4111-8111-111111111111", "22222222-2222-4222-8222-222222222222"} { | |
| 98 | 新增: input := browserClipboardInput{ID: id, Tab: strings.Repeat("a", 32), Context: strings.Repeat("b", 32), Document: strings.Repeat("c", 32)} | |
| 99 | 新增: body, _ := json.Marshal(input) | |
| 100 | 新增: response := windowsBrowserRequestForTest(s, http.MethodPost, "/api/browser/clipboard", body) | |
| 101 | 新增: var result map[string]string | |
| 102 | 新增: if response.Code != http.StatusOK || json.Unmarshal(response.Body.Bytes(), &result) != nil { | |
| 103 | 新增: t.Fatalf("clipboard reference failed: %d %s", response.Code, response.Body.String()) | |
| 104 | 新增: } | |
| 105 | 新增: if strings.HasPrefix(id, "11111111") { | |
| 106 | 新增: if result["png"] != testPNG(t) { | |
| 107 | 新增: t.Fatal("PNG reference changed its bytes") | |
| 108 | 新增: } | |
| 109 | 新增: } else if result["text"] != strings.Repeat("中", (2<<20)/3)+"ab" { | |
| 110 | 新增: t.Fatal("large UTF-8 clipboard text changed") | |
| 111 | 新增: } | |
| 112 | 新增: input.Context = strings.Repeat("d", 32) | |
| 113 | 新增: body, _ = json.Marshal(input) | |
| 114 | 新增: if response := windowsBrowserRequestForTest(s, http.MethodPost, "/api/browser/clipboard", body); response.Code == http.StatusOK { | |
| 115 | 新增: t.Fatal("superseded copy context was accepted") | |
| 116 | 新增: } | |
| 117 | 新增: input.Context = "" | |
| 118 | 新增: body, _ = json.Marshal(input) | |
| 119 | 新增: if response := windowsBrowserRequestForTest(s, http.MethodPost, "/api/browser/clipboard", body); response.Code != http.StatusBadRequest { | |
| 120 | 新增: t.Fatal("unbound copy reference was accepted") | |
| 121 | 新增: } | |
| 122 | 新增: } | |
| 123 | 新增: if s.browserControl != nil { | |
| 124 | 新增: t.Fatal("private clipboard used Linux transport") | |
| 125 | 新增: } | |
| 126 | 新增:} | |
| 127 | 新增: | |
| 93 | 128 | func TestWindowsBrowserExtensionHTTPUsesNativeChunkedPreview(t *testing.T) { |
| 94 | 129 | s := testServer(t) |
| 95 | 130 | s.windowsGuest, s.ready = &guestos.WindowsProfile{}, true |

