summaryrefslogtreecommitdiffstats
path: root/url/mojo/url_gurl_struct_traits.h
blob: 1894baf76e0b36e078ff8fbd813b8157e8fe6b88 (plain)
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
// Copyright 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 "base/strings/string_piece.h"
#include "url/gurl.h"
#include "url/mojo/url.mojom.h"

namespace mojo {

// copied from content/public/common/content_constants.cc: make that file use
// this definition.
const size_t kMaxUrlChars = 2 * 1024 * 1024;

template <>
struct StructTraits<url::mojom::Url, GURL> {
  static base::StringPiece url(const GURL& r) {
    if (r.possibly_invalid_spec().length() > kMaxUrlChars || !r.is_valid()) {
      return base::StringPiece();
    }

    return base::StringPiece(r.possibly_invalid_spec().c_str(),
                             r.possibly_invalid_spec().length());
  }
  static bool Read(url::mojom::Url::Reader r, GURL* out) {
    if (r.url().length() > kMaxUrlChars) {
      *out = GURL();
      return false;
    }
    *out = GURL(r.url());
    if (!r.url().empty() && !out->is_valid()) {
      *out = GURL();
      return false;
    }
    return true;
  }
};

}