OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #ifndef COMPONENTS_ENHANCED_BOOKMARKS_BOOKMARK_SERVER_SEARCH_SERVICE_H_ | 5 #ifndef COMPONENTS_ENHANCED_BOOKMARKS_BOOKMARK_SERVER_SEARCH_SERVICE_H_ |
6 #define COMPONENTS_ENHANCED_BOOKMARKS_BOOKMARK_SERVER_SEARCH_SERVICE_H_ | 6 #define COMPONENTS_ENHANCED_BOOKMARKS_BOOKMARK_SERVER_SEARCH_SERVICE_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
| 11 #include "base/containers/mru_cache.h" |
11 #include "components/enhanced_bookmarks/bookmark_server_service.h" | 12 #include "components/enhanced_bookmarks/bookmark_server_service.h" |
12 #include "net/url_request/url_fetcher.h" | 13 #include "net/url_request/url_fetcher.h" |
13 | 14 |
14 namespace enhanced_bookmarks { | 15 namespace enhanced_bookmarks { |
15 | 16 |
16 class EnhancedBookmarkModel; | 17 class EnhancedBookmarkModel; |
17 | 18 |
18 // Sends requests to the bookmark server to search for bookmarks relevant to a | 19 // Sends requests to the bookmark server to search for bookmarks relevant to a |
19 // given query. Will handle one outgoing request at a time. | 20 // given query. Will handle one outgoing request at a time. |
20 class BookmarkServerSearchService : public BookmarkServerService { | 21 class BookmarkServerSearchService : public BookmarkServerService { |
21 public: | 22 public: |
22 BookmarkServerSearchService( | 23 BookmarkServerSearchService( |
23 scoped_refptr<net::URLRequestContextGetter> request_context_getter, | 24 scoped_refptr<net::URLRequestContextGetter> request_context_getter, |
24 ProfileOAuth2TokenService* token_service, | 25 ProfileOAuth2TokenService* token_service, |
25 SigninManagerBase* signin_manager, | 26 SigninManagerBase* signin_manager, |
26 EnhancedBookmarkModel* bookmark_model); | 27 EnhancedBookmarkModel* bookmark_model); |
27 ~BookmarkServerSearchService() override; | 28 ~BookmarkServerSearchService() override; |
28 | 29 |
29 // Triggers a search. The query must not be empty. A call to this method | 30 // Triggers a search. The query must not be empty. A call to this method |
30 // cancels any previous searches. OnChange() is garanteed to be called once | 31 // cancels any previous searches. If there have been multiple queries in |
31 // per query. | 32 // between, onChange will only be called for the last query. |
| 33 // Note this method will be synchronous if query hits the cache. |
32 void Search(const std::string& query); | 34 void Search(const std::string& query); |
33 | 35 |
34 // Returns the search results. The results are only available after the | 36 // Returns search results for a query. Results for a query are only available |
35 // OnChange() observer methods has been sent. This method will return an empty | 37 // after Search() is called and after then OnChange() observer methods has |
36 // result otherwise. query should be a string passed to Search() previously. | 38 // been sent.This method might return an empty vector, meaning there are no |
37 std::vector<const BookmarkNode*> ResultForQuery(const std::string& query); | 39 // bookmarks matching the given query. Returning null means we are still |
| 40 // loading and no results have come to the client. Previously cancelled |
| 41 // queries will not trigger onChange(), and this method will also return null |
| 42 // for queries that have never been passed to Search() before. |
| 43 scoped_ptr<std::vector<const BookmarkNode*>> ResultForQuery( |
| 44 const std::string& query); |
38 | 45 |
39 protected: | 46 protected: |
40 scoped_ptr<net::URLFetcher> CreateFetcher() override; | 47 scoped_ptr<net::URLFetcher> CreateFetcher() override; |
41 | 48 |
42 bool ProcessResponse(const std::string& response, | 49 bool ProcessResponse(const std::string& response, |
43 bool* should_notify) override; | 50 bool* should_notify) override; |
44 | 51 |
45 void CleanAfterFailure() override; | 52 void CleanAfterFailure() override; |
46 | 53 |
47 // EnhancedBookmarkModelObserver methods. | 54 // EnhancedBookmarkModelObserver methods. |
48 void EnhancedBookmarkModelLoaded() override{}; | 55 void EnhancedBookmarkModelLoaded() override{}; |
49 void EnhancedBookmarkAdded(const BookmarkNode* node) override; | 56 void EnhancedBookmarkAdded(const BookmarkNode* node) override; |
50 void EnhancedBookmarkRemoved(const BookmarkNode* node) override{}; | 57 void EnhancedBookmarkRemoved(const BookmarkNode* node) override{}; |
51 void EnhancedBookmarkAllUserNodesRemoved() override; | 58 void EnhancedBookmarkAllUserNodesRemoved() override; |
52 void EnhancedBookmarkRemoteIdChanged(const BookmarkNode* node, | 59 void EnhancedBookmarkRemoteIdChanged(const BookmarkNode* node, |
53 const std::string& old_remote_id, | 60 const std::string& old_remote_id, |
54 const std::string& remote_id) override; | 61 const std::string& remote_id) override; |
55 | 62 |
56 private: | 63 private: |
57 // The search data, a map from query to a vector of stars.id. | 64 // Cache for previous search result, a map from a query string to vector of |
58 std::map<std::string, std::vector<std::string> > searches_; | 65 // star_ids. |
| 66 base::MRUCache<std::string, std::vector<std::string>> cache_; |
| 67 // The query currently on the fly, and is cleared as soon as the result is |
| 68 // available. |
59 std::string current_query_; | 69 std::string current_query_; |
60 DISALLOW_COPY_AND_ASSIGN(BookmarkServerSearchService); | 70 DISALLOW_COPY_AND_ASSIGN(BookmarkServerSearchService); |
61 }; | 71 }; |
62 } // namespace enhanced_bookmarks | 72 } // namespace enhanced_bookmarks |
63 | 73 |
64 #endif // COMPONENTS_ENHANCED_BOOKMARKS_BOOKMARK_SERVER_SEARCH_SERVICE_H_ | 74 #endif // COMPONENTS_ENHANCED_BOOKMARKS_BOOKMARK_SERVER_SEARCH_SERVICE_H_ |
OLD | NEW |