Chromium Code Reviews| Index: ui/base/dragdrop/drag_and_drop_url_utils.cc |
| diff --git a/ui/base/dragdrop/drag_and_drop_url_utils.cc b/ui/base/dragdrop/drag_and_drop_url_utils.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b56bb02ff5528855f324a2a850d0e708fedd1c12 |
| --- /dev/null |
| +++ b/ui/base/dragdrop/drag_and_drop_url_utils.cc |
| @@ -0,0 +1,52 @@ |
| +// Copyright (c) 2016 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 "ui/base/dragdrop/drag_and_drop_url_utils.h" |
| + |
| +#include "base/macros.h" |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "components/url_formatter/url_fixer.h" |
| +#include "net/base/filename_util.h" |
| +#include "ui/base/dragdrop/os_exchange_data.h" |
| +#include "url/gurl.h" |
| + |
| +namespace ui { |
| + |
| +namespace { |
| + |
| +base::Optional<GURL> TryToExtractPlainTextURL(const ui::OSExchangeData& data) { |
| + base::string16 text; |
| + if (!data.GetString(&text)) |
| + return {}; |
| + auto res = url_formatter::FixupURL(base::UTF16ToUTF8(text), ""); |
|
sky
2016/09/15 21:32:50
""->std::string in all of these.
|
| + if (!res.is_valid()) |
| + return {}; |
| + return res; |
| +} |
| + |
| +} // namespace |
| + |
| +bool CanBeInterpretedAsURL(const ui::OSExchangeData& data, |
| + ui::OSExchangeData::FilenameToURLPolicy policy) { |
| + if (data.HasURL(policy)) |
| + return true; |
| + return static_cast<bool>(TryToExtractPlainTextURL(data)); |
| +} |
| + |
| +base::Optional<std::pair<GURL, base::string16>> TryToInterpretAsURL( |
| + const ui::OSExchangeData& data, |
| + ui::OSExchangeData::FilenameToURLPolicy policy) { |
| + std::pair<GURL, base::string16> res; |
| + if (data.GetURLAndTitle(policy, &res.first, &res.second)) |
| + return res; |
| + |
| + auto opt_url = TryToExtractPlainTextURL(data); |
| + if (!opt_url) |
| + return {}; |
| + res.first = std::move(*opt_url); |
| + res.second = net::GetSuggestedFilename(res.first, "", "", "", "", {}); |
| + return res; |
| +} |
| + |
| +} // namespace ui |