url_handler_service_provider.cc 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ash/dbus/url_handler_service_provider.h"
  5. #include <utility>
  6. #include "ash/public/cpp/new_window_delegate.h"
  7. #include "base/bind.h"
  8. #include "base/logging.h"
  9. #include "dbus/bus.h"
  10. #include "dbus/message.h"
  11. #include "extensions/common/constants.h"
  12. #include "third_party/cros_system_api/dbus/service_constants.h"
  13. #include "url/gurl.h"
  14. #include "url/url_constants.h"
  15. namespace ash {
  16. namespace {
  17. constexpr const char* kUrlSchemes[] = {url::kDataScheme, url::kFileScheme,
  18. url::kFtpScheme, url::kHttpScheme,
  19. url::kHttpsScheme, url::kMailToScheme};
  20. // Called from ExportedObject when OpenUrl() is exported as a D-Bus method or
  21. // failed to be exported.
  22. void OnExported(const std::string& interface_name,
  23. const std::string& method_name,
  24. bool success) {
  25. if (!success) {
  26. LOG(ERROR) << "Failed to export " << interface_name << "." << method_name;
  27. }
  28. }
  29. } // namespace
  30. UrlHandlerServiceProvider::UrlHandlerServiceProvider()
  31. : allowed_url_schemes_(std::cbegin(kUrlSchemes), std::cend(kUrlSchemes)) {}
  32. UrlHandlerServiceProvider::~UrlHandlerServiceProvider() = default;
  33. void UrlHandlerServiceProvider::Start(
  34. scoped_refptr<dbus::ExportedObject> exported_object) {
  35. exported_object->ExportMethod(
  36. chromeos::kUrlHandlerServiceInterface,
  37. chromeos::kUrlHandlerServiceOpenUrlMethod,
  38. base::BindRepeating(&UrlHandlerServiceProvider::OpenUrl,
  39. weak_ptr_factory_.GetWeakPtr()),
  40. base::BindOnce(&OnExported));
  41. }
  42. bool UrlHandlerServiceProvider::UrlAllowed(const GURL& gurl) const {
  43. return gurl.is_valid() &&
  44. allowed_url_schemes_.find(gurl.scheme()) != allowed_url_schemes_.end();
  45. }
  46. void UrlHandlerServiceProvider::OpenUrl(
  47. dbus::MethodCall* method_call,
  48. dbus::ExportedObject::ResponseSender response_sender) {
  49. dbus::MessageReader reader(method_call);
  50. std::string url;
  51. if (!reader.PopString(&url)) {
  52. LOG(ERROR) << "Method call lacks URL: " << method_call->ToString();
  53. std::move(response_sender)
  54. .Run(dbus::ErrorResponse::FromMethodCall(
  55. method_call, DBUS_ERROR_INVALID_ARGS, "No URL string arg"));
  56. return;
  57. }
  58. VLOG(1) << "Got request to open url";
  59. const GURL gurl(url);
  60. if (!UrlAllowed(gurl)) {
  61. VLOG(1) << "Url is not allowed";
  62. std::move(response_sender)
  63. .Run(dbus::ErrorResponse::FromMethodCall(method_call, DBUS_ERROR_FAILED,
  64. "Invalid URL"));
  65. return;
  66. }
  67. VLOG(1) << "Opening url now";
  68. NewWindowDelegate::GetPrimary()->OpenUrl(
  69. gurl, NewWindowDelegate::OpenUrlFrom::kUnspecified,
  70. NewWindowDelegate::Disposition::kNewForegroundTab);
  71. std::move(response_sender).Run(dbus::Response::FromMethodCall(method_call));
  72. }
  73. } // namespace ash