Home | History | Annotate | Download | only in bindings
      1 // Copyright 2016 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 
      5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_STRING_TRAITS_STRING_PIECE_H_
      6 #define MOJO_PUBLIC_CPP_BINDINGS_STRING_TRAITS_STRING_PIECE_H_
      7 
      8 #include "base/strings/string_piece.h"
      9 #include "mojo/public/cpp/bindings/string_traits.h"
     10 
     11 namespace mojo {
     12 
     13 template <>
     14 struct StringTraits<base::StringPiece> {
     15   static bool IsNull(const base::StringPiece& input) {
     16     // base::StringPiece is always converted to non-null mojom string. We could
     17     // have let StringPiece containing a null data pointer map to null mojom
     18     // string, but StringPiece::empty() returns true in this case. It seems
     19     // confusing to mix the concept of empty and null strings, especially
     20     // because they mean different things in mojom.
     21     return false;
     22   }
     23 
     24   static void SetToNull(base::StringPiece* output) {
     25     // Convert null to an "empty" base::StringPiece.
     26     output->set(nullptr, 0);
     27   }
     28 
     29   static size_t GetSize(const base::StringPiece& input) { return input.size(); }
     30 
     31   static const char* GetData(const base::StringPiece& input) {
     32     return input.data();
     33   }
     34 
     35   static bool Read(StringDataView input, base::StringPiece* output) {
     36     output->set(input.storage(), input.size());
     37     return true;
     38   }
     39 };
     40 
     41 }  // namespace mojo
     42 
     43 #endif  // MOJO_PUBLIC_CPP_BINDINGS_STRING_TRAITS_STRING_PIECE_H_
     44