xref: /aosp_15_r20/external/cronet/net/proxy_resolution/pac_file_data.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2012 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 "net/proxy_resolution/pac_file_data.h"
6 
7 #include "base/check_op.h"
8 #include "base/strings/utf_string_conversions.h"
9 
10 namespace net {
11 
12 // static
FromUTF8(const std::string & utf8)13 scoped_refptr<PacFileData> PacFileData::FromUTF8(const std::string& utf8) {
14   return base::WrapRefCounted(
15       new PacFileData(TYPE_SCRIPT_CONTENTS, GURL(), base::UTF8ToUTF16(utf8)));
16 }
17 
18 // static
FromUTF16(const std::u16string & utf16)19 scoped_refptr<PacFileData> PacFileData::FromUTF16(const std::u16string& utf16) {
20   return base::WrapRefCounted(
21       new PacFileData(TYPE_SCRIPT_CONTENTS, GURL(), utf16));
22 }
23 
24 // static
FromURL(const GURL & url)25 scoped_refptr<PacFileData> PacFileData::FromURL(const GURL& url) {
26   return base::WrapRefCounted(
27       new PacFileData(TYPE_SCRIPT_URL, url, std::u16string()));
28 }
29 
30 // static
ForAutoDetect()31 scoped_refptr<PacFileData> PacFileData::ForAutoDetect() {
32   return base::WrapRefCounted(
33       new PacFileData(TYPE_AUTO_DETECT, GURL(), std::u16string()));
34 }
35 
utf16() const36 const std::u16string& PacFileData::utf16() const {
37   DCHECK_EQ(TYPE_SCRIPT_CONTENTS, type_);
38   return utf16_;
39 }
40 
url() const41 const GURL& PacFileData::url() const {
42   DCHECK_EQ(TYPE_SCRIPT_URL, type_);
43   return url_;
44 }
45 
Equals(const PacFileData * other) const46 bool PacFileData::Equals(const PacFileData* other) const {
47   if (type() != other->type())
48     return false;
49 
50   switch (type()) {
51     case TYPE_SCRIPT_CONTENTS:
52       return utf16() == other->utf16();
53     case TYPE_SCRIPT_URL:
54       return url() == other->url();
55     case TYPE_AUTO_DETECT:
56       return true;
57   }
58 
59   return false;  // Shouldn't be reached.
60 }
61 
PacFileData(Type type,const GURL & url,const std::u16string & utf16)62 PacFileData::PacFileData(Type type,
63                          const GURL& url,
64                          const std::u16string& utf16)
65     : type_(type), url_(url), utf16_(utf16) {}
66 
67 PacFileData::~PacFileData() = default;
68 
69 }  // namespace net
70