ServiceWorker: Consolidate version manipulation functions in SWProviderContext
[chromium-blink-merge.git] / mojo / common / common_type_converters.cc
blob114b409d7488232a4c4cb39c1859aca7cd0c0823
1 // Copyright 2013 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.
5 #include "mojo/common/common_type_converters.h"
7 #include <string>
9 #include "base/strings/utf_string_conversions.h"
10 #include "url/gurl.h"
12 namespace mojo {
14 // static
15 String TypeConverter<String, base::StringPiece>::Convert(
16 const base::StringPiece& input) {
17 if (input.empty()) {
18 char c = 0;
19 return String(&c, 0);
21 return String(input.data(), input.size());
23 // static
24 base::StringPiece TypeConverter<base::StringPiece, String>::Convert(
25 const String& input) {
26 return input.get();
29 // static
30 String TypeConverter<String, base::string16>::Convert(
31 const base::string16& input) {
32 return TypeConverter<String, base::StringPiece>::Convert(
33 base::UTF16ToUTF8(input));
35 // static
36 base::string16 TypeConverter<base::string16, String>::Convert(
37 const String& input) {
38 return base::UTF8ToUTF16(input.To<base::StringPiece>());
41 String TypeConverter<String, GURL>::Convert(const GURL& input) {
42 return String(input.spec());
45 GURL TypeConverter<GURL, String>::Convert(const String& input) {
46 return GURL(input.get());
49 std::string TypeConverter<std::string, Array<uint8_t> >::Convert(
50 const Array<uint8_t>& input) {
51 if (input.is_null())
52 return std::string();
54 return std::string(reinterpret_cast<const char*>(&input.front()),
55 input.size());
58 Array<uint8_t> TypeConverter<Array<uint8_t>, std::string>::Convert(
59 const std::string& input) {
60 Array<uint8_t> result(input.size());
61 memcpy(&result.front(), input.c_str(), input.size());
62 return result.Pass();
65 } // namespace mojo