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

Side by Side Diff: third_party/WebKit/Source/platform/weborigin/KURL.cpp

Issue 2647633006: Make KURL::protocolIs take StringView (+ DCHECK cleanups) (Closed)
Patch Set: comment only changes Created 3 years, 11 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 | « third_party/WebKit/Source/platform/weborigin/KURL.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2004, 2007, 2008, 2011, 2012 Apple Inc. All rights reserved. 2 * Copyright (C) 2004, 2007, 2008, 2011, 2012 Apple Inc. All rights reserved.
3 * Copyright (C) 2012 Research In Motion Limited. All rights reserved. 3 * Copyright (C) 2012 Research In Motion Limited. All rights reserved.
4 * Copyright (C) 2008, 2009, 2011 Google Inc. All rights reserved. 4 * Copyright (C) 2008, 2009, 2011 Google Inc. All rights reserved.
5 * 5 *
6 * Redistribution and use in source and binary forms, with or without 6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions 7 * modification, are permitted provided that the following conditions
8 * are met: 8 * are met:
9 * 1. Redistributions of source code must retain the above copyright 9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer. 10 * notice, this list of conditions and the following disclaimer.
(...skipping 29 matching lines...) Expand all
40 #include <algorithm> 40 #include <algorithm>
41 #ifndef NDEBUG 41 #ifndef NDEBUG
42 #include <stdio.h> 42 #include <stdio.h>
43 #endif 43 #endif
44 44
45 namespace blink { 45 namespace blink {
46 46
47 static const int maximumValidPortNumber = 0xFFFE; 47 static const int maximumValidPortNumber = 0xFFFE;
48 static const int invalidPortNumber = 0xFFFF; 48 static const int invalidPortNumber = 0xFFFF;
49 49
50 static void assertProtocolIsGood(const char* protocol) {
51 #if DCHECK_IS_ON() 50 #if DCHECK_IS_ON()
52 DCHECK_NE(protocol, ""); 51 static void assertProtocolIsGood(const StringView protocol) {
53 const char* p = protocol; 52 DCHECK(protocol != "");
54 while (*p) { 53 for (size_t i = 0; i < protocol.length(); ++i) {
55 ASSERT(*p > ' ' && *p < 0x7F && !(*p >= 'A' && *p <= 'Z')); 54 LChar c = protocol.characters8()[i];
56 ++p; 55 DCHECK(c > ' ' && c < 0x7F && !(c >= 'A' && c <= 'Z'));
esprehn 2017/01/20 17:50:04 This could probably be simpler I think? You want i
57 } 56 }
57 }
58 #endif 58 #endif
59 }
60 59
61 // Note: You must ensure that |spec| is a valid canonicalized URL before calling 60 // Note: You must ensure that |spec| is a valid canonicalized URL before calling
62 // this function. 61 // this function.
63 static const char* asURLChar8Subtle(const String& spec) { 62 static const char* asURLChar8Subtle(const String& spec) {
64 ASSERT(spec.is8Bit()); 63 DCHECK(spec.is8Bit());
65 // characters8 really return characters in Latin-1, but because we 64 // characters8 really return characters in Latin-1, but because we
66 // canonicalize URL strings, we know that everything before the fragment 65 // canonicalize URL strings, we know that everything before the fragment
67 // identifier will actually be ASCII, which means this cast is safe as long as 66 // identifier will actually be ASCII, which means this cast is safe as long as
68 // you don't look at the fragment component. 67 // you don't look at the fragment component.
69 return reinterpret_cast<const char*>(spec.characters8()); 68 return reinterpret_cast<const char*>(spec.characters8());
70 } 69 }
71 70
72 // Returns the characters for the given string, or a pointer to a static empty 71 // Returns the characters for the given string, or a pointer to a static empty
73 // string if the input string is null. This will always ensure we have a non- 72 // string if the input string is null. This will always ensure we have a non-
74 // null character pointer since ReplaceComponents has special meaning for null. 73 // null character pointer since ReplaceComponents has special meaning for null.
(...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after
311 310
312 bool KURL::hasPath() const { 311 bool KURL::hasPath() const {
313 // Note that http://www.google.com/" has a path, the path is "/". This can 312 // Note that http://www.google.com/" has a path, the path is "/". This can
314 // return false only for invalid or nonstandard URLs. 313 // return false only for invalid or nonstandard URLs.
315 return m_parsed.path.len >= 0; 314 return m_parsed.path.len >= 0;
316 } 315 }
317 316
318 String KURL::lastPathComponent() const { 317 String KURL::lastPathComponent() const {
319 if (!m_isValid) 318 if (!m_isValid)
320 return stringViewForInvalidComponent().toString(); 319 return stringViewForInvalidComponent().toString();
321 ASSERT(!m_string.isNull()); 320 DCHECK(!m_string.isNull());
322 321
323 // When the output ends in a slash, WebCore has different expectations than 322 // When the output ends in a slash, WebCore has different expectations than
324 // the GoogleURL library. For "/foo/bar/" the library will return the empty 323 // the GoogleURL library. For "/foo/bar/" the library will return the empty
325 // string, but WebCore wants "bar". 324 // string, but WebCore wants "bar".
326 url::Component path = m_parsed.path; 325 url::Component path = m_parsed.path;
327 if (path.len > 0 && m_string[path.end() - 1] == '/') 326 if (path.len > 0 && m_string[path.end() - 1] == '/')
328 path.len--; 327 path.len--;
329 328
330 url::Component file; 329 url::Component file;
331 if (m_string.is8Bit()) 330 if (m_string.is8Bit())
(...skipping 19 matching lines...) Expand all
351 350
352 // Returns 0 when there is no port. 351 // Returns 0 when there is no port.
353 // 352 //
354 // We treat URL's with out-of-range port numbers as invalid URLs, and they will 353 // We treat URL's with out-of-range port numbers as invalid URLs, and they will
355 // be rejected by the canonicalizer. KURL.cpp will allow them in parsing, but 354 // be rejected by the canonicalizer. KURL.cpp will allow them in parsing, but
356 // return invalidPortNumber from this port() function, so we mirror that 355 // return invalidPortNumber from this port() function, so we mirror that
357 // behavior here. 356 // behavior here.
358 unsigned short KURL::port() const { 357 unsigned short KURL::port() const {
359 if (!m_isValid || m_parsed.port.len <= 0) 358 if (!m_isValid || m_parsed.port.len <= 0)
360 return 0; 359 return 0;
361 ASSERT(!m_string.isNull()); 360 DCHECK(!m_string.isNull());
362 int port = m_string.is8Bit() 361 int port = m_string.is8Bit()
363 ? url::ParsePort(asURLChar8Subtle(m_string), m_parsed.port) 362 ? url::ParsePort(asURLChar8Subtle(m_string), m_parsed.port)
364 : url::ParsePort(m_string.characters16(), m_parsed.port); 363 : url::ParsePort(m_string.characters16(), m_parsed.port);
365 ASSERT(port != url::PORT_UNSPECIFIED); // Checked port.len <= 0 before. 364 DCHECK_NE(port, url::PORT_UNSPECIFIED); // Checked port.len <= 0 before.
366 365
367 if (port == url::PORT_INVALID || 366 if (port == url::PORT_INVALID ||
368 port > maximumValidPortNumber) // Mimic KURL::port() 367 port > maximumValidPortNumber) // Mimic KURL::port()
369 port = invalidPortNumber; 368 port = invalidPortNumber;
370 369
371 return static_cast<unsigned short>(port); 370 return static_cast<unsigned short>(port);
372 } 371 }
373 372
374 // TODO(csharrison): Migrate pass() and user() to return a StringView. Most 373 // TODO(csharrison): Migrate pass() and user() to return a StringView. Most
375 // consumers just need to know if the string is empty. 374 // consumers just need to know if the string is empty.
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
519 setPort(parsedPort.toUInt()); 518 setPort(parsedPort.toUInt());
520 } 519 }
521 520
522 void KURL::setPort(unsigned short port) { 521 void KURL::setPort(unsigned short port) {
523 if (isDefaultPortForProtocol(port, protocol())) { 522 if (isDefaultPortForProtocol(port, protocol())) {
524 removePort(); 523 removePort();
525 return; 524 return;
526 } 525 }
527 526
528 String portString = String::number(port); 527 String portString = String::number(port);
529 ASSERT(portString.is8Bit()); 528 DCHECK(portString.is8Bit());
530 529
531 url::Replacements<char> replacements; 530 url::Replacements<char> replacements;
532 replacements.SetPort(reinterpret_cast<const char*>(portString.characters8()), 531 replacements.SetPort(reinterpret_cast<const char*>(portString.characters8()),
533 url::Component(0, portString.length())); 532 url::Component(0, portString.length()));
534 replaceComponents(replacements); 533 replaceComponents(replacements);
535 } 534 }
536 535
537 void KURL::setUser(const String& user) { 536 void KURL::setUser(const String& user) {
538 // This function is commonly called to clear the username, which we 537 // This function is commonly called to clear the username, which we
539 // normally don't have, so we optimize this case. 538 // normally don't have, so we optimize this case.
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
707 return m_parsed.CountCharactersBefore(url::Parsed::PATH, false); 706 return m_parsed.CountCharactersBefore(url::Parsed::PATH, false);
708 url::Component filename; 707 url::Component filename;
709 if (m_string.is8Bit()) 708 if (m_string.is8Bit())
710 url::ExtractFileName(asURLChar8Subtle(m_string), m_parsed.path, &filename); 709 url::ExtractFileName(asURLChar8Subtle(m_string), m_parsed.path, &filename);
711 else 710 else
712 url::ExtractFileName(m_string.characters16(), m_parsed.path, &filename); 711 url::ExtractFileName(m_string.characters16(), m_parsed.path, &filename);
713 return filename.begin; 712 return filename.begin;
714 } 713 }
715 714
716 bool protocolIs(const String& url, const char* protocol) { 715 bool protocolIs(const String& url, const char* protocol) {
716 #if DCHECK_IS_ON()
717 assertProtocolIsGood(protocol); 717 assertProtocolIsGood(protocol);
718 #endif
718 if (url.isNull()) 719 if (url.isNull())
719 return false; 720 return false;
720 if (url.is8Bit()) 721 if (url.is8Bit())
721 return url::FindAndCompareScheme(asURLChar8Subtle(url), url.length(), 722 return url::FindAndCompareScheme(asURLChar8Subtle(url), url.length(),
722 protocol, 0); 723 protocol, 0);
723 return url::FindAndCompareScheme(url.characters16(), url.length(), protocol, 724 return url::FindAndCompareScheme(url.characters16(), url.length(), protocol,
724 0); 725 0);
725 } 726 }
726 727
727 void KURL::init(const KURL& base, 728 void KURL::init(const KURL& base,
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
809 } else if (protocol == WTF::httpAtom) { 810 } else if (protocol == WTF::httpAtom) {
810 m_protocol = WTF::httpAtom; 811 m_protocol = WTF::httpAtom;
811 } else { 812 } else {
812 m_protocol = protocol.toAtomicString(); 813 m_protocol = protocol.toAtomicString();
813 m_protocolIsInHTTPFamily = 814 m_protocolIsInHTTPFamily =
814 m_protocol == "http-so" || m_protocol == "https-so"; 815 m_protocol == "http-so" || m_protocol == "https-so";
815 } 816 }
816 DCHECK_EQ(m_protocol, m_protocol.lower()); 817 DCHECK_EQ(m_protocol, m_protocol.lower());
817 } 818 }
818 819
819 bool KURL::protocolIs(const char* protocol) const { 820 bool KURL::protocolIs(const StringView protocol) const {
821 #if DCHECK_IS_ON()
820 assertProtocolIsGood(protocol); 822 assertProtocolIsGood(protocol);
823 #endif
821 824
822 // JavaScript URLs are "valid" and should be executed even if KURL decides 825 // JavaScript URLs are "valid" and should be executed even if KURL decides
823 // they are invalid. The free function protocolIsJavaScript() should be used 826 // they are invalid. The free function protocolIsJavaScript() should be used
824 // instead. 827 // instead.
825 // FIXME: Chromium code needs to be fixed for this assert to be enabled. 828 // FIXME: Chromium code needs to be fixed for this assert to be enabled.
826 // ASSERT(strcmp(protocol, "javascript")); 829 // ASSERT(strcmp(protocol, "javascript"));
827 return m_protocol == protocol; 830 return m_protocol == protocol;
828 } 831 }
829 832
830 StringView KURL::stringViewForInvalidComponent() const { 833 StringView KURL::stringViewForInvalidComponent() const {
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
865 m_string = AtomicString::fromUTF8(output.data(), output.length()); 868 m_string = AtomicString::fromUTF8(output.data(), output.length());
866 initProtocolMetadata(); 869 initProtocolMetadata();
867 } 870 }
868 871
869 bool KURL::isSafeToSendToAnotherThread() const { 872 bool KURL::isSafeToSendToAnotherThread() const {
870 return m_string.isSafeToSendToAnotherThread() && 873 return m_string.isSafeToSendToAnotherThread() &&
871 (!m_innerURL || m_innerURL->isSafeToSendToAnotherThread()); 874 (!m_innerURL || m_innerURL->isSafeToSendToAnotherThread());
872 } 875 }
873 876
874 } // namespace blink 877 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/platform/weborigin/KURL.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698