nsGNOMEShellSearchProvider.h (5219B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim:expandtab:shiftwidth=2:tabstop=2: 3 */ 4 /* This Source Code Form is subject to the terms of the Mozilla Public 5 * License, v. 2.0. If a copy of the MPL was not distributed with this 6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 7 8 #ifndef __nsGNOMEShellSearchProvider_h__ 9 #define __nsGNOMEShellSearchProvider_h__ 10 11 #include <gio/gio.h> 12 13 #include "mozilla/RefPtr.h" 14 #include "mozilla/GRefPtr.h" 15 #include "mozilla/Span.h" 16 #include "nsINavHistoryService.h" 17 #include "nsUnixRemoteServer.h" 18 #include "nsString.h" 19 #include "nsCOMPtr.h" 20 #include "mozilla/UniquePtr.h" 21 #include "nsGNOMEShellDBusHelper.h" 22 23 class nsGNOMEShellSearchProvider; 24 25 class GnomeHistoryIcon { 26 public: 27 GnomeHistoryIcon() : mTimeStamp(-1), mWidth(0), mHeight(0) {} 28 29 // From which search is this icon 30 void Set(int aTimeStamp, mozilla::UniquePtr<uint8_t[]> aData, int aWidth, 31 int aHeight) { 32 mTimeStamp = aTimeStamp; 33 mWidth = aWidth; 34 mHeight = aHeight; 35 mData = std::move(aData); 36 } 37 38 bool IsLoaded() { return mData && mWidth > 0 && mHeight > 0; } 39 int GetTimeStamp() { return mTimeStamp; } 40 uint8_t* GetData() { return mData.get(); } 41 int GetWidth() { return mWidth; } 42 int GetHeight() { return mHeight; } 43 44 private: 45 int mTimeStamp; 46 mozilla::UniquePtr<uint8_t[]> mData; 47 int mWidth; 48 int mHeight; 49 }; 50 51 // nsGNOMEShellHistorySearchResult is a container with contains search results 52 // which are files asynchronously by nsGNOMEShellHistoryService. 53 // The search results can be opened by Firefox then. 54 class nsGNOMEShellHistorySearchResult : public nsUnixRemoteServer { 55 public: 56 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(nsGNOMEShellHistorySearchResult) 57 58 nsGNOMEShellHistorySearchResult(nsGNOMEShellSearchProvider* aSearchProvider, 59 GDBusConnection* aConnection, int aTimeStamp) 60 : mSearchProvider(aSearchProvider), 61 mConnection(aConnection), 62 mTimeStamp(aTimeStamp) {} 63 64 void SetReply(RefPtr<GDBusMethodInvocation> aReply) { 65 mReply = std::move(aReply); 66 } 67 void SetSearchTerm(const char* aSearchTerm) { 68 mSearchTerm = nsAutoCString(aSearchTerm); 69 } 70 GDBusConnection* GetDBusConnection() { return mConnection; } 71 void SetTimeStamp(int aTimeStamp) { mTimeStamp = aTimeStamp; } 72 int GetTimeStamp() { return mTimeStamp; } 73 nsAutoCString& GetSearchTerm() { return mSearchTerm; } 74 75 // Receive (asynchronously) history search results from history service. 76 // This is called asynchronously by nsGNOMEShellHistoryService 77 // when we have search results available. 78 void ReceiveSearchResultContainer( 79 nsCOMPtr<nsINavHistoryContainerResultNode> aHistResultContainer); 80 nsCOMPtr<nsINavHistoryContainerResultNode> GetSearchResultContainer() { 81 return mHistResultContainer; 82 } 83 void HandleCommandLine(mozilla::Span<const char> aBuffer, 84 uint32_t aTimestamp) { 85 nsUnixRemoteServer::HandleCommandLine(aBuffer, aTimestamp); 86 } 87 88 void SetHistoryIcon(int aTimeStamp, mozilla::UniquePtr<uint8_t[]> aData, 89 int aWidth, int aHeight, int aIconIndex); 90 GnomeHistoryIcon* GetHistoryIcon(int aIconIndex); 91 92 private: 93 void HandleSearchResultReply(); 94 95 ~nsGNOMEShellHistorySearchResult() = default; 96 97 private: 98 nsGNOMEShellSearchProvider* mSearchProvider; 99 nsCOMPtr<nsINavHistoryContainerResultNode> mHistResultContainer; 100 nsTArray<nsCString> mOpenTabs; 101 nsAutoCString mSearchTerm; 102 RefPtr<GDBusMethodInvocation> mReply; 103 GDBusConnection* mConnection = nullptr; 104 int mTimeStamp; 105 GnomeHistoryIcon mHistoryIcons[MAX_SEARCH_RESULTS_NUM]; 106 }; 107 108 class nsGNOMEShellHistoryService { 109 public: 110 nsresult QueryHistory(RefPtr<nsGNOMEShellHistorySearchResult> aSearchResult); 111 112 private: 113 nsCOMPtr<nsINavHistoryService> mHistoryService; 114 }; 115 116 class nsGNOMEShellSearchProvider { 117 public: 118 nsGNOMEShellSearchProvider() 119 : mConnection(nullptr), mSearchResultTimeStamp(0) {} 120 ~nsGNOMEShellSearchProvider() { Shutdown(); } 121 122 nsresult Startup(); 123 void Shutdown(); 124 125 void UnregisterDBusInterface(GDBusConnection* aConnection); 126 127 bool SetSearchResult(RefPtr<nsGNOMEShellHistorySearchResult> aSearchResult); 128 129 void HandleSearchResultSet(GVariant* aParameters, 130 GDBusMethodInvocation* aInvocation, 131 bool aInitialSearch); 132 void HandleResultMetas(GVariant* aParameters, 133 GDBusMethodInvocation* aInvocation); 134 void ActivateResult(GVariant* aParameters, 135 GDBusMethodInvocation* aInvocation); 136 void LaunchSearch(GVariant* aParameters, GDBusMethodInvocation* aInvocation); 137 138 void OnBusAcquired(GDBusConnection* aConnection); 139 void OnNameAcquired(GDBusConnection* aConnection); 140 void OnNameLost(GDBusConnection* aConnection); 141 142 private: 143 // The connection is owned by DBus library 144 uint mDBusID = 0; 145 uint mRegistrationId = 0; 146 GDBusConnection* mConnection = nullptr; 147 RefPtr<GDBusNodeInfo> mIntrospectionData; 148 149 RefPtr<nsGNOMEShellHistorySearchResult> mSearchResult; 150 int mSearchResultTimeStamp; 151 }; 152 153 nsGNOMEShellHistoryService* GetGNOMEShellHistoryService(); 154 155 #endif // __nsGNOMEShellSearchProvider_h__