Home | History | Annotate | Download | only in page
      1 /*
      2  * Copyright (C) 2008 Apple Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  *
      8  * 1.  Redistributions of source code must retain the above copyright
      9  *     notice, this list of conditions and the following disclaimer.
     10  * 2.  Redistributions in binary form must reproduce the above copyright
     11  *     notice, this list of conditions and the following disclaimer in the
     12  *     documentation and/or other materials provided with the distribution.
     13  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
     14  *     its contributors may be used to endorse or promote products derived
     15  *     from this software without specific prior written permission.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
     18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
     21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include "config.h"
     30 #include "Location.h"
     31 
     32 #include "Frame.h"
     33 #include "FrameLoader.h"
     34 #include "KURL.h"
     35 #include "PlatformString.h"
     36 
     37 namespace WebCore {
     38 
     39 Location::Location(Frame* frame)
     40     : m_frame(frame)
     41 {
     42 }
     43 
     44 void Location::disconnectFrame()
     45 {
     46     m_frame = 0;
     47 }
     48 
     49 inline const KURL& Location::url() const
     50 {
     51     ASSERT(m_frame);
     52 
     53     const KURL& url = m_frame->loader()->url();
     54     if (!url.isValid())
     55         return blankURL();  // Use "about:blank" while the page is still loading (before we have a frame).
     56 
     57     return url;
     58 }
     59 
     60 String Location::href() const
     61 {
     62     if (!m_frame)
     63         return String();
     64 
     65     const KURL& url = this->url();
     66     return url.hasPath() ? url.prettyURL() : url.prettyURL() + "/";
     67 }
     68 
     69 String Location::protocol() const
     70 {
     71     if (!m_frame)
     72         return String();
     73 
     74     return url().protocol() + ":";
     75 }
     76 
     77 String Location::host() const
     78 {
     79     if (!m_frame)
     80         return String();
     81 
     82     // Note: this is the IE spec. The NS spec swaps the two, it says
     83     // "The hostname property is the concatenation of the host and port properties, separated by a colon."
     84     const KURL& url = this->url();
     85     return url.port() ? url.host() + ":" + String::number((static_cast<int>(url.port()))) : url.host();
     86 }
     87 
     88 String Location::hostname() const
     89 {
     90     if (!m_frame)
     91         return String();
     92 
     93     return url().host();
     94 }
     95 
     96 String Location::port() const
     97 {
     98     if (!m_frame)
     99         return String();
    100 
    101     const KURL& url = this->url();
    102     return url.port() ? String::number(static_cast<int>(url.port())) : "";
    103 }
    104 
    105 String Location::pathname() const
    106 {
    107     if (!m_frame)
    108         return String();
    109 
    110     const KURL& url = this->url();
    111     return url.path().isEmpty() ? "/" : url.path();
    112 }
    113 
    114 String Location::search() const
    115 {
    116     if (!m_frame)
    117         return String();
    118 
    119     const KURL& url = this->url();
    120     return url.query().isEmpty() ? "" : "?" + url.query();
    121 }
    122 
    123 String Location::hash() const
    124 {
    125     if (!m_frame)
    126         return String();
    127 
    128     const String& fragmentIdentifier = this->url().fragmentIdentifier();
    129     return fragmentIdentifier.isEmpty() ? "" : "#" + fragmentIdentifier;
    130 }
    131 
    132 String Location::toString() const
    133 {
    134     if (!m_frame)
    135         return String();
    136 
    137     const KURL& url = this->url();
    138     return url.hasPath() ? url.prettyURL() : url.prettyURL() + "/";
    139 }
    140 
    141 } // namespace WebCore
    142