NavigationDestination.cpp (3695B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim:set ts=2 sw=2 sts=2 et cindent: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "mozilla/dom/NavigationDestination.h" 8 9 #include "mozilla/ErrorResult.h" 10 #include "mozilla/dom/NavigationDestinationBinding.h" 11 #include "mozilla/dom/NavigationHistoryEntry.h" 12 #include "nsError.h" 13 #include "nsIURI.h" 14 #include "nsReadableUtils.h" 15 16 namespace mozilla::dom { 17 18 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(NavigationDestination, mGlobal, mEntry, 19 mState) 20 NS_IMPL_CYCLE_COLLECTING_ADDREF(NavigationDestination) 21 NS_IMPL_CYCLE_COLLECTING_RELEASE(NavigationDestination) 22 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(NavigationDestination) 23 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY 24 NS_INTERFACE_MAP_ENTRY(nsISupports) 25 NS_INTERFACE_MAP_END 26 27 NavigationDestination::NavigationDestination( 28 nsIGlobalObject* aGlobal, nsIURI* aURI, NavigationHistoryEntry* aEntry, 29 nsIStructuredCloneContainer* aState, bool aIsSameDocument) 30 : mGlobal(aGlobal), 31 mURL(aURI), 32 mEntry(aEntry), 33 mState(aState), 34 mIsSameDocument(aIsSameDocument) {} 35 36 // https://html.spec.whatwg.org/#dom-navigationdestination-url 37 void NavigationDestination::GetUrl(nsString& aURL) const { 38 nsAutoCString uri; 39 nsresult rv = mURL->GetSpec(uri); 40 if (NS_WARN_IF(NS_FAILED(rv))) { 41 aURL.Truncate(); 42 return; 43 } 44 45 CopyUTF8toUTF16(uri, aURL); 46 } 47 48 // https://html.spec.whatwg.org/#dom-navigationdestination-key 49 void NavigationDestination::GetKey(nsString& aKey) const { 50 if (!mEntry) { 51 aKey.Truncate(); 52 return; 53 } 54 55 mEntry->GetKey(aKey); 56 } 57 58 // https://html.spec.whatwg.org/#dom-navigationdestination-id 59 void NavigationDestination::GetId(nsString& aId) const { 60 if (!mEntry) { 61 aId.Truncate(); 62 return; 63 } 64 65 mEntry->GetId(aId); 66 } 67 68 // https://html.spec.whatwg.org/#dom-navigationdestination-index 69 int64_t NavigationDestination::Index() const { 70 if (!mEntry) { 71 return -1; 72 } 73 74 return mEntry->Index(); 75 } 76 77 // https://html.spec.whatwg.org/#dom-navigationdestination-samedocument 78 bool NavigationDestination::SameDocument() const { return mIsSameDocument; } 79 80 // https://html.spec.whatwg.org/#dom-navigationdestination-getstate 81 void NavigationDestination::GetState(JSContext* aCx, 82 JS::MutableHandle<JS::Value> aRetVal, 83 ErrorResult& aRv) const { 84 if (!mState) { 85 return; 86 } 87 nsresult rv = mState->DeserializeToJsval(aCx, aRetVal); 88 if (NS_FAILED(rv)) { 89 // nsStructuredCloneContainer::DeserializeToJsval suppresses exceptions, so 90 // the best we can do is just re-throw the NS_ERROR_DOM_DATA_CLONE_ERR. When 91 // nsStructuredCloneContainer::DeserializeToJsval throws better exceptions 92 // this should too. 93 // See also: NavigationHistoryEntry::GetState 94 aRv.Throw(rv); 95 return; 96 } 97 } 98 99 void NavigationDestination::SetState(nsIStructuredCloneContainer* aState) { 100 mState = aState; 101 } 102 103 JSObject* NavigationDestination::WrapObject(JSContext* aCx, 104 JS::Handle<JSObject*> aGivenProto) { 105 return NavigationDestination_Binding::Wrap(aCx, this, aGivenProto); 106 } 107 108 nsIGlobalObject* NavigationDestination::GetParentObject() { return mGlobal; } 109 110 NavigationHistoryEntry* NavigationDestination::GetEntry() const { 111 return mEntry; 112 } 113 114 nsIURI* NavigationDestination::GetURL() const { return mURL; } 115 116 void NavigationDestination::SetURL(nsIURI* aURI) { mURL = aURI; } 117 118 } // namespace mozilla::dom