forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathurl_handler_service_provider.cc
87 lines (71 loc) · 2.87 KB
/
url_handler_service_provider.cc
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
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/dbus/url_handler_service_provider.h"
#include <utility>
#include "ash/public/cpp/new_window_delegate.h"
#include "base/bind.h"
#include "dbus/bus.h"
#include "dbus/message.h"
#include "extensions/common/constants.h"
#include "third_party/cros_system_api/dbus/service_constants.h"
#include "url/gurl.h"
#include "url/url_constants.h"
namespace ash {
namespace {
constexpr const char* kUrlSchemes[] = {url::kDataScheme, url::kFileScheme,
url::kFtpScheme, url::kHttpScheme,
url::kHttpsScheme, url::kMailToScheme};
// Called from ExportedObject when OpenUrl() is exported as a D-Bus method or
// failed to be exported.
void OnExported(const std::string& interface_name,
const std::string& method_name,
bool success) {
if (!success) {
LOG(ERROR) << "Failed to export " << interface_name << "." << method_name;
}
}
} // namespace
UrlHandlerServiceProvider::UrlHandlerServiceProvider()
: allowed_url_schemes_(std::cbegin(kUrlSchemes), std::cend(kUrlSchemes)) {}
UrlHandlerServiceProvider::~UrlHandlerServiceProvider() = default;
void UrlHandlerServiceProvider::Start(
scoped_refptr<dbus::ExportedObject> exported_object) {
exported_object->ExportMethod(
chromeos::kUrlHandlerServiceInterface,
chromeos::kUrlHandlerServiceOpenUrlMethod,
base::BindRepeating(&UrlHandlerServiceProvider::OpenUrl,
weak_ptr_factory_.GetWeakPtr()),
base::BindOnce(&OnExported));
}
bool UrlHandlerServiceProvider::UrlAllowed(const GURL& gurl) const {
return gurl.is_valid() &&
allowed_url_schemes_.find(gurl.scheme()) != allowed_url_schemes_.end();
}
void UrlHandlerServiceProvider::OpenUrl(
dbus::MethodCall* method_call,
dbus::ExportedObject::ResponseSender response_sender) {
dbus::MessageReader reader(method_call);
std::string url;
if (!reader.PopString(&url)) {
LOG(ERROR) << "Method call lacks URL: " << method_call->ToString();
std::move(response_sender)
.Run(dbus::ErrorResponse::FromMethodCall(
method_call, DBUS_ERROR_INVALID_ARGS, "No URL string arg"));
return;
}
VLOG(1) << "Got request to open url";
const GURL gurl(url);
if (!UrlAllowed(gurl)) {
VLOG(1) << "Url is not allowed";
std::move(response_sender)
.Run(dbus::ErrorResponse::FromMethodCall(method_call, DBUS_ERROR_FAILED,
"Invalid URL"));
return;
}
VLOG(1) << "Opening url now";
NewWindowDelegate::GetInstance()->NewTabWithUrl(
gurl, false /* from_user_interaction */);
std::move(response_sender).Run(dbus::Response::FromMethodCall(method_call));
}
} // namespace ash