File: smbfs_host.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 (158 lines) | stat: -rw-r--r-- 5,348 bytes parent folder | download | duplicates (6)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/ash/components/smbfs/smbfs_host.h"

#include <utility>

#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/logging.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "chromeos/ash/components/disks/disk_mount_manager.h"
#include "mojo/public/cpp/bindings/receiver.h"

namespace smbfs {
namespace {

class SmbFsDelegateImpl : public mojom::SmbFsDelegate {
 public:
  SmbFsDelegateImpl(
      mojo::PendingReceiver<mojom::SmbFsDelegate> pending_receiver,
      base::OnceClosure disconnect_callback,
      SmbFsHost::Delegate* delegate)
      : receiver_(this, std::move(pending_receiver)), delegate_(delegate) {
    receiver_.set_disconnect_handler(std::move(disconnect_callback));
  }

  SmbFsDelegateImpl(const SmbFsDelegateImpl&) = delete;
  SmbFsDelegateImpl& operator=(const SmbFsDelegateImpl&) = delete;

  ~SmbFsDelegateImpl() override = default;

  // mojom::SmbFsDelegate overrides.
  void RequestCredentials(RequestCredentialsCallback callback) override {
    delegate_->RequestCredentials(
        base::BindOnce(&SmbFsDelegateImpl::OnRequestCredentialsDone,
                       weak_factory_.GetWeakPtr(), std::move(callback)));
  }

 private:
  void OnRequestCredentialsDone(RequestCredentialsCallback callback,
                                bool cancel,
                                const std::string& username,
                                const std::string& workgroup,
                                const std::string& password) {
    if (cancel) {
      std::move(callback).Run(nullptr);
      return;
    }

    mojom::CredentialsPtr creds =
        mojom::Credentials::New(username, workgroup, nullptr);
    if (password.size() > mojom::Password::kMaxLength) {
      LOG(WARNING) << "smbfs password too long";
    } else if (!password.empty()) {
      // Create pipe and write password.
      base::ScopedFD pipe_read_end;
      base::ScopedFD pipe_write_end;
      CHECK(base::CreatePipe(&pipe_read_end, &pipe_write_end,
                             true /* non_blocking */));
      CHECK(base::WriteFileDescriptor(pipe_write_end.get(), password));

      creds->password = mojom::Password::New(
          mojo::WrapPlatformHandle(
              mojo::PlatformHandle(std::move(pipe_read_end))),
          static_cast<int32_t>(password.size()));
    }
    std::move(callback).Run(std::move(creds));
  }

  mojo::Receiver<mojom::SmbFsDelegate> receiver_;
  const raw_ptr<SmbFsHost::Delegate> delegate_;

  base::WeakPtrFactory<SmbFsDelegateImpl> weak_factory_{this};
};

}  // namespace

SmbFsHost::Delegate::~Delegate() = default;

SmbFsHost::SmbFsHost(
    std::unique_ptr<ash::disks::MountPoint> mount_point,
    Delegate* delegate,
    mojo::Remote<mojom::SmbFs> smbfs_remote,
    mojo::PendingReceiver<mojom::SmbFsDelegate> delegate_receiver)
    : mount_point_(std::move(mount_point)),
      delegate_(delegate),
      smbfs_(std::move(smbfs_remote)),
      delegate_impl_(std::make_unique<SmbFsDelegateImpl>(
          std::move(delegate_receiver),
          base::BindOnce(&SmbFsHost::OnDisconnect, base::Unretained(this)),
          delegate)) {
  DCHECK(mount_point_);
  DCHECK(delegate);

  smbfs_.set_disconnect_handler(
      base::BindOnce(&SmbFsHost::OnDisconnect, base::Unretained(this)));
}

SmbFsHost::~SmbFsHost() = default;

void SmbFsHost::Unmount(SmbFsHost::UnmountCallback callback) {
  mount_point_->Unmount(base::BindOnce(
      &SmbFsHost::OnUnmountDone, base::Unretained(this), std::move(callback)));
}

void SmbFsHost::OnUnmountDone(SmbFsHost::UnmountCallback callback,
                              ash::MountError result) {
  LOG_IF(ERROR, result != ash::MountError::kSuccess)
      << "Could not unmount smbfs share: " << result;
  std::move(callback).Run(result);
}

void SmbFsHost::RemoveSavedCredentials(
    SmbFsHost::RemoveSavedCredentialsCallback callback) {
  smbfs_->RemoveSavedCredentials(
      base::BindOnce(&SmbFsHost::OnRemoveSavedCredentialsDone,
                     base::Unretained(this), std::move(callback)));
}

void SmbFsHost::OnRemoveSavedCredentialsDone(
    SmbFsHost::RemoveSavedCredentialsCallback callback,
    bool success) {
  LOG_IF(ERROR, !success) << "Unable to remove saved password for smbfs";
  std::move(callback).Run(success);
}

void SmbFsHost::OnDisconnect() {
  // Ensure only one disconnection event occurs.
  smbfs_.reset();
  delegate_impl_.reset();

  // This may delete us.
  delegate_->OnDisconnected();
}

void SmbFsHost::DeleteRecursively(const base::FilePath& path,
                                  DeleteRecursivelyCallback callback) {
  smbfs_->DeleteRecursively(
      path, base::BindOnce(&SmbFsHost::OnDeleteRecursivelyDone,
                           base::Unretained(this), std::move(callback)));
}

void SmbFsHost::OnDeleteRecursivelyDone(
    DeleteRecursivelyCallback callback,
    smbfs::mojom::DeleteRecursivelyError error) {
  base::File::Error file_error =
      error == smbfs::mojom::DeleteRecursivelyError::kOk
          ? base::File::FILE_OK
          : base::File::FILE_ERROR_FAILED;

  std::move(callback).Run(file_error);
}

}  // namespace smbfs