File: system_storage_api.cc

package info (click to toggle)
chromium 140.0.7339.80-1~deb13u1
  • links: PTS, VCS
  • area: main
  • in suites: trixie-proposed-updates
  • size: 6,192,892 kB
  • sloc: cpp: 35,092,386; ansic: 7,161,671; javascript: 4,199,703; python: 1,441,797; asm: 949,904; xml: 747,409; pascal: 187,748; perl: 88,691; sh: 88,248; objc: 79,953; sql: 52,714; cs: 44,599; fortran: 24,137; makefile: 22,114; tcl: 15,277; php: 13,980; yacc: 9,000; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (137 lines) | stat: -rw-r--r-- 4,950 bytes parent folder | download | duplicates (8)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "extensions/browser/api/system_storage/system_storage_api.h"

#include "base/functional/bind.h"
#include "base/task/thread_pool.h"
#include "content/public/browser/browser_thread.h"
#include "extensions/browser/api/system_storage/storage_info_provider.h"

using storage_monitor::StorageMonitor;

namespace extensions {

using api::system_storage::StorageUnitInfo;
namespace EjectDevice = api::system_storage::EjectDevice;
namespace GetAvailableCapacity = api::system_storage::GetAvailableCapacity;

ExtensionFunction::ResponseAction SystemStorageGetInfoFunction::Run() {
  StorageInfoProvider::Get()->StartQueryInfo(base::BindOnce(
      &SystemStorageGetInfoFunction::OnGetStorageInfoCompleted, this));
  return RespondLater();
}

void SystemStorageGetInfoFunction::OnGetStorageInfoCompleted(bool success) {
  if (success) {
    Respond(ArgumentList(api::system_storage::GetInfo::Results::Create(
        StorageInfoProvider::Get()->storage_unit_info_list())));
  } else {
    Respond(Error("Error occurred when querying storage information."));
  }
}

ExtensionFunction::ResponseAction SystemStorageEjectDeviceFunction::Run() {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

  std::optional<EjectDevice::Params> params =
      EjectDevice::Params::Create(args());
  EXTENSION_FUNCTION_VALIDATE(params);

  StorageMonitor::GetInstance()->EnsureInitialized(
      base::BindOnce(&SystemStorageEjectDeviceFunction::OnStorageMonitorInit,
                     this, params->id));
  // EnsureInitialized() above can result in synchronous Respond().
  return did_respond() ? AlreadyResponded() : RespondLater();
}

void SystemStorageEjectDeviceFunction::OnStorageMonitorInit(
    const std::string& transient_device_id) {
  DCHECK(StorageMonitor::GetInstance()->IsInitialized());
  StorageMonitor* monitor = StorageMonitor::GetInstance();
  std::string device_id_str =
      StorageMonitor::GetInstance()->GetDeviceIdForTransientId(
          transient_device_id);

  if (device_id_str.empty()) {
    HandleResponse(StorageMonitor::EJECT_NO_SUCH_DEVICE);
    return;
  }

  monitor->EjectDevice(
      device_id_str,
      base::BindOnce(&SystemStorageEjectDeviceFunction::HandleResponse, this));
}

void SystemStorageEjectDeviceFunction::HandleResponse(
    StorageMonitor::EjectStatus status) {
  api::system_storage::EjectDeviceResultCode result =
      api::system_storage::EjectDeviceResultCode::kFailure;
  switch (status) {
    case StorageMonitor::EJECT_OK:
      result = api::system_storage::EjectDeviceResultCode::kSuccess;
      break;
    case StorageMonitor::EJECT_IN_USE:
      result = api::system_storage::EjectDeviceResultCode::kInUse;
      break;
    case StorageMonitor::EJECT_NO_SUCH_DEVICE:
      result = api::system_storage::EjectDeviceResultCode::kNoSuchDevice;
      break;
    case StorageMonitor::EJECT_FAILURE:
      result = api::system_storage::EjectDeviceResultCode::kFailure;
  }

  Respond(WithArguments(api::system_storage::ToString(result)));
}

SystemStorageGetAvailableCapacityFunction::
    SystemStorageGetAvailableCapacityFunction()
    : query_runner_(base::ThreadPool::CreateSequencedTaskRunner(
          {base::TaskPriority::BEST_EFFORT, base::MayBlock(),
           base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN})) {}

SystemStorageGetAvailableCapacityFunction::
    ~SystemStorageGetAvailableCapacityFunction() = default;

ExtensionFunction::ResponseAction
SystemStorageGetAvailableCapacityFunction::Run() {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

  std::optional<GetAvailableCapacity::Params> params =
      GetAvailableCapacity::Params::Create(args());
  EXTENSION_FUNCTION_VALIDATE(params);

  StorageMonitor::GetInstance()->EnsureInitialized(base::BindOnce(
      &SystemStorageGetAvailableCapacityFunction::OnStorageMonitorInit, this,
      params->id));
  return RespondLater();
}

void SystemStorageGetAvailableCapacityFunction::OnStorageMonitorInit(
    const std::string& transient_id) {
  query_runner_->PostTaskAndReplyWithResult(
      FROM_HERE,
      base::BindOnce(
          &StorageInfoProvider::GetStorageFreeSpaceFromTransientIdAsync,
          StorageInfoProvider::Get(), transient_id),
      base::BindOnce(
          &SystemStorageGetAvailableCapacityFunction::OnQueryCompleted, this,
          transient_id));
}

void SystemStorageGetAvailableCapacityFunction::OnQueryCompleted(
    const std::string& transient_id,
    double available_capacity) {
  bool success = available_capacity >= 0;
  if (success) {
    api::system_storage::StorageAvailableCapacityInfo result;
    result.id = transient_id;
    result.available_capacity = available_capacity;
    Respond(WithArguments(result.ToValue()));
  } else {
    Respond(Error("Error occurred when querying available capacity."));
  }
}

}  // namespace extensions