File: sandbox_diagnostics.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (72 lines) | stat: -rw-r--r-- 2,659 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "sandbox/policy/win/sandbox_diagnostics.h"

#include <stddef.h>

#include <string>
#include <utility>
#include <vector>

#include "base/json/json_reader.h"
#include "base/task/sequenced_task_runner.h"
#include "base/values.h"

namespace sandbox {
namespace policy {
namespace {
// Runs on a non-sandbox thread to ensure that response callback is not
// invoked from sandbox process and job tracker thread, and that conversion
// work does not block process or job registration. Converts |policies|
// into base::Value form, then invokes |response| on the same sequence.
static void ConvertToValuesAndRespond(
    std::unique_ptr<PolicyList> policies,
    base::OnceCallback<void(base::Value)> response) {
  base::Value::List policy_values;
  for (auto&& item : *policies) {
    auto snapshot = base::JSONReader::ReadAndReturnValueWithError(
        item->JsonString(), base::JSON_PARSE_RFC);
    CHECK(snapshot.has_value());
    policy_values.Append(std::move(*snapshot));
  }
  std::move(response).Run(base::Value(std::move(policy_values)));
}

// Runs on a non-sandbox thread to ensure that response callback is not
// invoked from sandbox process and job tracker thread.
static void RespondWithEmptyList(
    base::OnceCallback<void(base::Value)> response) {
  base::Value empty(base::Value::Type::LIST);
  std::move(response).Run(std::move(empty));
}

}  // namespace

ServiceManagerDiagnosticsReceiver::ServiceManagerDiagnosticsReceiver(
    scoped_refptr<base::SequencedTaskRunner> origin_task_runner,
    base::OnceCallback<void(base::Value)> response)
    : response_(std::move(response)), origin_task_runner_(origin_task_runner) {}

ServiceManagerDiagnosticsReceiver::~ServiceManagerDiagnosticsReceiver() {}

// This is called by the sandbox's process and job tracking thread and must
// return quickly.
void ServiceManagerDiagnosticsReceiver::ReceiveDiagnostics(
    std::unique_ptr<PolicyList> policies) {
  // Need to run the conversion work on the origin thread.
  origin_task_runner_->PostTask(
      FROM_HERE, base::BindOnce(&ConvertToValuesAndRespond, std::move(policies),
                                std::move(response_)));
}

// This is called by the sandbox's process and job tracking thread and must
// return quickly so we post to the origin thread.
void ServiceManagerDiagnosticsReceiver::OnError(ResultCode error) {
  origin_task_runner_->PostTask(
      FROM_HERE, base::BindOnce(&RespondWithEmptyList, std::move(response_)));
}

}  // namespace policy
}  // namespace sandbox