Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(45)

Side by Side Diff: net/base/net_util_icu.cc

Issue 1133943002: Create net::FormatOriginForDisplay helper function. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Crucial punctuation in comment. Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/base/net_util.h ('k') | net/base/net_util_icu_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/base/net_util.h" 5 #include "net/base/net_util.h"
6 6
7 #include <map> 7 #include <map>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/i18n/time_formatting.h" 10 #include "base/i18n/time_formatting.h"
(...skipping 779 matching lines...) Expand 10 before | Expand all | Expand 10 after
790 790
791 // If we need to strip out http do it after the fact. 791 // If we need to strip out http do it after the fact.
792 if (omit_http && StartsWith(url_string, base::ASCIIToUTF16(kHTTP), true)) { 792 if (omit_http && StartsWith(url_string, base::ASCIIToUTF16(kHTTP), true)) {
793 const size_t kHTTPSize = arraysize(kHTTP) - 1; 793 const size_t kHTTPSize = arraysize(kHTTP) - 1;
794 url_string = url_string.substr(kHTTPSize); 794 url_string = url_string.substr(kHTTPSize);
795 // Because offsets in the |adjustments| are already calculated with respect 795 // Because offsets in the |adjustments| are already calculated with respect
796 // to the string with the http:// prefix in it, those offsets remain correct 796 // to the string with the http:// prefix in it, those offsets remain correct
797 // after stripping the prefix. The only thing necessary is to add an 797 // after stripping the prefix. The only thing necessary is to add an
798 // adjustment to reflect the stripped prefix. 798 // adjustment to reflect the stripped prefix.
799 adjustments->insert(adjustments->begin(), 799 adjustments->insert(adjustments->begin(),
800 base::OffsetAdjuster::Adjustment(0, kHTTPSize, 0)); 800 base::OffsetAdjuster::Adjustment(0, kHTTPSize, 0));
801 801
802 if (prefix_end) 802 if (prefix_end)
803 *prefix_end -= kHTTPSize; 803 *prefix_end -= kHTTPSize;
804 804
805 // Adjust new_parsed. 805 // Adjust new_parsed.
806 DCHECK(new_parsed->scheme.is_valid()); 806 DCHECK(new_parsed->scheme.is_valid());
807 int delta = -(new_parsed->scheme.len + 3); // +3 for ://. 807 int delta = -(new_parsed->scheme.len + 3); // +3 for ://.
808 new_parsed->scheme.reset(); 808 new_parsed->scheme.reset();
809 AdjustAllComponentsButScheme(delta, new_parsed); 809 AdjustAllComponentsButScheme(delta, new_parsed);
810 } 810 }
(...skipping 11 matching lines...) Expand all
822 Offsets offsets; 822 Offsets offsets;
823 if (offset_for_adjustment) 823 if (offset_for_adjustment)
824 offsets.push_back(*offset_for_adjustment); 824 offsets.push_back(*offset_for_adjustment);
825 base::string16 result = FormatUrlWithOffsets(url, languages, format_types, 825 base::string16 result = FormatUrlWithOffsets(url, languages, format_types,
826 unescape_rules, new_parsed, prefix_end, &offsets); 826 unescape_rules, new_parsed, prefix_end, &offsets);
827 if (offset_for_adjustment) 827 if (offset_for_adjustment)
828 *offset_for_adjustment = offsets[0]; 828 *offset_for_adjustment = offsets[0];
829 return result; 829 return result;
830 } 830 }
831 831
832 base::string16 FormatOriginForDisplay(const GURL& url,
833 const std::string& languages,
834 bool omit_scheme) {
835 if (!url.IsStandard())
Ryan Sleevi 2015/05/16 01:26:04 BUG: You should be checking how invalid GURLs prov
palmer 2015/05/16 01:37:18 Can you formulate a unit test that triggers a fail
Ryan Sleevi 2015/05/16 01:43:01 Sure, easy - FormatOriginForDisplay(GURL(), ...)
836 return FormatUrl(url, languages);
837
838 if (url.SchemeIsFile()) {
839 // TODO(palmer): Determine whether to encode this policy in GURL::GetOrigin.
840 return (omit_scheme ? base::ASCIIToUTF16("")
Ryan Sleevi 2015/05/16 01:26:04 Performance: You should just be using base::string
palmer 2015/05/16 01:37:18 OK.
841 : base::ASCIIToUTF16("file://")) +
842 base::UTF8ToUTF16(url.path());
843 }
844
845 if (url.SchemeIsFileSystem()) {
846 // TODO(palmer): Determine whether to encode this policy in GURL::GetOrigin.
847 const GURL inner_url(url.spec().substr(strlen("filesystem:")));
848 return base::ASCIIToUTF16("filesystem:") +
849 FormatOriginForDisplay(inner_url, languages, omit_scheme);
Ryan Sleevi 2015/05/16 01:26:04 BUG: This is not the right way to create sub-GURLs
palmer 2015/05/16 01:37:18 Look at previous patchsets, in which we tried that
Ryan Sleevi 2015/05/16 01:43:01 I did before making that comment, I didn't see a s
850 }
851
852 const GURL origin = url.GetOrigin();
853 const std::string& scheme = origin.scheme();
854 const std::string& host = origin.host();
855 if (scheme.empty() || host.empty())
856 return FormatUrl(url, languages);
857
858 base::string16 result;
859
860 if (!omit_scheme)
861 result = base::UTF8ToUTF16(scheme) + base::ASCIIToUTF16("://");
862
863 result += base::UTF8ToUTF16(host);
864
865 const int port = origin.IntPort();
866 const int default_port = url::DefaultPortForScheme(origin.scheme().c_str(),
867 origin.scheme().length());
868 if (origin.port().length() > 0 && port != 0 && port != default_port)
869 result += base::ASCIIToUTF16(":") + base::UTF8ToUTF16(origin.port());
870
871 return result;
872 }
873
832 } // namespace net 874 } // namespace net
OLDNEW
« no previous file with comments | « net/base/net_util.h ('k') | net/base/net_util_icu_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698