• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "url/ipc/url_param_traits.h"
6 
7 #include <string>
8 
9 #include "base/pickle.h"
10 #include "url/gurl.h"
11 #include "url/url_constants.h"
12 
13 namespace IPC {
14 
Write(base::Pickle * m,const GURL & p)15 void ParamTraits<GURL>::Write(base::Pickle* m, const GURL& p) {
16   if (p.possibly_invalid_spec().length() > url::kMaxURLChars) {
17     m->WriteString(std::string());
18     return;
19   }
20 
21   // Beware of print-parse inconsistency which would change an invalid
22   // URL into a valid one. Ideally, the message would contain this flag
23   // so that the read side could make the check, but performing it here
24   // avoids changing the on-the-wire representation of such a fundamental
25   // type as GURL. See https://crbug.com/166486 for additional work in
26   // this area.
27   if (!p.is_valid()) {
28     m->WriteString(std::string());
29     return;
30   }
31 
32   m->WriteString(p.possibly_invalid_spec());
33   // TODO(brettw) bug 684583: Add encoding for query params.
34 }
35 
Read(const base::Pickle * m,base::PickleIterator * iter,GURL * p)36 bool ParamTraits<GURL>::Read(const base::Pickle* m,
37                              base::PickleIterator* iter,
38                              GURL* p) {
39   std::string s;
40   if (!iter->ReadString(&s) || s.length() > url::kMaxURLChars) {
41     *p = GURL();
42     return false;
43   }
44   *p = GURL(s);
45   if (!s.empty() && !p->is_valid()) {
46     *p = GURL();
47     return false;
48   }
49   return true;
50 }
51 
Log(const GURL & p,std::string * l)52 void ParamTraits<GURL>::Log(const GURL& p, std::string* l) {
53   l->append(p.spec());
54 }
55 
56 }  // namespace IPC
57