File: remote_open_url_client.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (156 lines) | stat: -rw-r--r-- 5,012 bytes parent folder | download | duplicates (3)
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
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "remoting/host/remote_open_url/remote_open_url_client.h"

#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/notreached.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "net/base/filename_util.h"
#include "remoting/base/logging.h"
#include "remoting/host/chromoting_host_services_client.h"
#include "remoting/host/mojom/chromoting_host_services.mojom.h"
#include "remoting/host/mojom/remote_url_opener.mojom.h"

#if BUILDFLAG(IS_LINUX)
#include "remoting/host/remote_open_url/remote_open_url_client_delegate_linux.h"
#elif BUILDFLAG(IS_WIN)
#include "remoting/host/remote_open_url/remote_open_url_client_delegate_win.h"
#endif

namespace remoting {

namespace {

constexpr base::TimeDelta kRequestTimeout = base::Seconds(5);

std::unique_ptr<RemoteOpenUrlClient::Delegate> CreateDelegate() {
#if BUILDFLAG(IS_LINUX)
  return std::make_unique<RemoteOpenUrlClientDelegateLinux>();
#elif BUILDFLAG(IS_WIN)
  return std::make_unique<RemoteOpenUrlClientDelegateWin>();
#else
  NOTREACHED();
#endif
}

}  // namespace

RemoteOpenUrlClient::RemoteOpenUrlClient()
    : RemoteOpenUrlClient(CreateDelegate(),
                          std::make_unique<ChromotingHostServicesClient>(),
                          kRequestTimeout) {}

RemoteOpenUrlClient::RemoteOpenUrlClient(
    std::unique_ptr<Delegate> delegate,
    std::unique_ptr<ChromotingHostServicesProvider> api_provider,
    base::TimeDelta request_timeout)
    : delegate_(std::move(delegate)),
      api_provider_(std::move(api_provider)),
      request_timeout_(request_timeout) {}

RemoteOpenUrlClient::~RemoteOpenUrlClient() {
  DCHECK(!done_);
}

void RemoteOpenUrlClient::OpenFallbackBrowser() {
  DCHECK(url_.is_empty());
  delegate_->OpenUrlOnFallbackBrowser(url_);
}

void RemoteOpenUrlClient::Open(const base::CommandLine::StringType& arg,
                               base::OnceClosure done) {
  DCHECK(url_.is_empty());
  DCHECK(!done_);
  DCHECK(!remote_);

  done_ = std::move(done);

#if BUILDFLAG(IS_WIN)
  GURL url(base::WideToUTF8(arg));
#else
  GURL url(arg);
#endif

  if (!url.is_valid()) {
    // It has been observed that in some Linux environments, a file:// URL might
    // be converted back to a file path when it's being passed to
    // remote_open_url, so we try to convert it back to a file:// URL and open
    // it with the fallback browser.
    base::FilePath file_path(arg);
    if (file_path.IsAbsolute()) {
      HOST_LOG << "Argument appears to be an absolute file path. Will convert "
                  "it to a file:// URL.";
      url = net::FilePathToFileURL(file_path);
    }
  }

  if (!url.is_valid()) {
    LOG(ERROR) << "Invalid URL";
    OnOpenUrlResponse(mojom::OpenUrlResult::FAILURE);
    return;
  }

  url_ = url;

  if (!url_.SchemeIsHTTPOrHTTPS() && !url_.SchemeIs("mailto")) {
    HOST_LOG << "Unrecognized scheme. Failing back to the previous default "
             << "browser...";
    OnOpenUrlResponse(mojom::OpenUrlResult::LOCAL_FALLBACK);
    return;
  }

  auto* api = api_provider_->GetSessionServices();
  if (!api) {
    HOST_LOG << "Can't make IPC connection. The host is probably not running.";
    OnOpenUrlResponse(mojom::OpenUrlResult::LOCAL_FALLBACK);
    return;
  }
  api->BindRemoteUrlOpener(remote_.BindNewPipeAndPassReceiver());
  remote_.set_disconnect_handler(base::BindOnce(
      &RemoteOpenUrlClient::OnIpcDisconnected, base::Unretained(this)));
  timeout_timer_.Start(FROM_HERE, request_timeout_, this,
                       &RemoteOpenUrlClient::OnRequestTimeout);
  remote_->OpenUrl(url_, base::BindOnce(&RemoteOpenUrlClient::OnOpenUrlResponse,
                                        base::Unretained(this)));
}

void RemoteOpenUrlClient::OnOpenUrlResponse(mojom::OpenUrlResult result) {
  timeout_timer_.Stop();
  switch (result) {
    case mojom::OpenUrlResult::SUCCESS:
      HOST_LOG << "The URL is successfully opened on the client.";
      break;
    case mojom::OpenUrlResult::FAILURE: {
      delegate_->ShowOpenUrlError(url_);
      break;
    }
    case mojom::OpenUrlResult::LOCAL_FALLBACK:
      delegate_->OpenUrlOnFallbackBrowser(url_);
      break;
    default:
      NOTREACHED();
  }
  std::move(done_).Run();
  remote_.reset();
}

void RemoteOpenUrlClient::OnRequestTimeout() {
  LOG(ERROR) << "Timed out waiting for OpenUrl response.";
  OnOpenUrlResponse(mojom::OpenUrlResult::LOCAL_FALLBACK);
}

void RemoteOpenUrlClient::OnIpcDisconnected() {
  LOG(WARNING) << "IPC disconnected.";
  // This generally happens either because the session is not remoted, or the
  // client hasn't enabled URL forwarding, so we fallback locally.
  OnOpenUrlResponse(mojom::OpenUrlResult::LOCAL_FALLBACK);
}

}  // namespace remoting