Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 #ifndef COMPONENTS_ENHANCED_BOOKMARKS_BOOKMARK_SERVER_SERVICE_H_ | |
|
Yaron
2014/09/05 04:37:52
nit: insert ws before
noyau (Ping after 24h)
2014/09/08 09:46:49
Done.
| |
| 5 #define COMPONENTS_ENHANCED_BOOKMARKS_BOOKMARK_SERVER_SERVICE_H_ | |
| 6 | |
| 7 #include <string> | |
| 8 #include <vector> | |
| 9 | |
| 10 #include "components/bookmarks/browser/bookmark_model_observer.h" | |
| 11 #include "google_apis/gaia/google_service_auth_error.h" | |
| 12 #include "google_apis/gaia/oauth2_token_service.h" | |
| 13 #include "net/url_request/url_fetcher.h" | |
| 14 #include "net/url_request/url_fetcher_delegate.h" | |
| 15 #include "net/url_request/url_request_context_getter.h" | |
| 16 | |
| 17 class BookmarkModel; | |
| 18 class ProfileOAuth2TokenService; | |
| 19 class SigninManagerBase; | |
| 20 | |
| 21 namespace enhanced_bookmarks { | |
| 22 | |
| 23 class BookmarkServerService; | |
| 24 | |
| 25 class BookmarkServerServiceObserver { | |
| 26 public: | |
| 27 virtual void OnChange(BookmarkServerService* service) = 0; | |
| 28 | |
| 29 protected: | |
| 30 virtual ~BookmarkServerServiceObserver() {} | |
| 31 }; | |
| 32 | |
| 33 // This abstract class manages the connection to the bookmark servers and | |
| 34 // stores the maps necessary to translate the response from stars_id to | |
|
Yaron
2014/09/05 04:37:52
stars.id
noyau (Ping after 24h)
2014/09/08 09:46:49
Done.
| |
| 35 // BookmarkNodes. Subclasses just have to provide the right query and the | |
| 36 // parsing of the response. | |
| 37 class BookmarkServerService : protected net::URLFetcherDelegate, | |
| 38 protected BookmarkModelObserver, | |
| 39 private OAuth2TokenService::Consumer { | |
| 40 public: | |
| 41 BookmarkServerService( | |
| 42 scoped_refptr<net::URLRequestContextGetter> request_context_getter, | |
| 43 ProfileOAuth2TokenService* token_service, | |
| 44 SigninManagerBase* signin_manager, | |
| 45 BookmarkModel* bookmark_model); | |
| 46 virtual ~BookmarkServerService(); | |
| 47 | |
| 48 void AddObserver(BookmarkServerServiceObserver* observer); | |
| 49 void RemoveObserver(BookmarkServerServiceObserver* observer); | |
| 50 | |
| 51 protected: | |
| 52 // Retrieves a bookmark by using its remote id. Returns null if nothing | |
| 53 // matches. | |
| 54 virtual const BookmarkNode* BookmarkForRemoteId( | |
| 55 const std::string& remote_id) const; | |
| 56 const std::string RemoteIDForBookmark(const BookmarkNode* bookmark) const; | |
| 57 | |
| 58 // Notifies the observers that something changed. | |
| 59 void Notify(); | |
| 60 | |
| 61 // Triggers a fetch. | |
| 62 void TriggerTokenRequest(bool cancel_previous); | |
| 63 | |
| 64 // Build the query to send to the server. Returns a newly created url_fetcher. | |
| 65 virtual net::URLFetcher* CreateFetcherWithToken( | |
| 66 const std::string& access_token) = 0; | |
| 67 | |
| 68 // Processes the response to the query. Returns true on successful parsing, | |
| 69 // false on failure. The implementation can assume that |should_notify| is set | |
| 70 // to true by default, if changed to false there will be no OnChange | |
| 71 // notification send. | |
| 72 virtual bool ProcessResponse(const std::string& response, | |
| 73 bool* should_notify) = 0; | |
| 74 | |
| 75 // If the token can't be retrieved or the query fails this method is called. | |
| 76 virtual void CleanAfterFailure() = 0; | |
| 77 | |
| 78 // BookmarkModelObserver methods. | |
| 79 virtual void BookmarkModelLoaded(BookmarkModel* model, | |
| 80 bool ids_reassigned) OVERRIDE; | |
| 81 virtual void BookmarkNodeMoved(BookmarkModel* model, | |
| 82 const BookmarkNode* old_parent, | |
| 83 int old_index, | |
| 84 const BookmarkNode* new_parent, | |
| 85 int new_index) OVERRIDE {}; | |
| 86 virtual void BookmarkNodeAdded(BookmarkModel* model, | |
| 87 const BookmarkNode* parent, | |
| 88 int index) OVERRIDE; | |
| 89 virtual void BookmarkNodeRemoved(BookmarkModel* model, | |
| 90 const BookmarkNode* parent, | |
| 91 int old_index, | |
| 92 const BookmarkNode* node, | |
| 93 const std::set<GURL>& removed_urls) OVERRIDE; | |
| 94 virtual void BookmarkNodeChanged(BookmarkModel* model, | |
| 95 const BookmarkNode* node) OVERRIDE {}; | |
| 96 virtual void OnWillChangeBookmarkMetaInfo(BookmarkModel* model, | |
| 97 const BookmarkNode* node) OVERRIDE; | |
| 98 | |
| 99 virtual void BookmarkMetaInfoChanged(BookmarkModel* model, | |
| 100 const BookmarkNode* node) OVERRIDE; | |
| 101 | |
| 102 virtual void BookmarkNodeFaviconChanged(BookmarkModel* model, | |
| 103 const BookmarkNode* node) OVERRIDE {}; | |
| 104 virtual void BookmarkNodeChildrenReordered(BookmarkModel* model, | |
| 105 const BookmarkNode* node) | |
| 106 OVERRIDE {}; | |
| 107 virtual void BookmarkAllUserNodesRemoved( | |
| 108 BookmarkModel* model, | |
| 109 const std::set<GURL>& removed_urls) OVERRIDE; | |
| 110 | |
| 111 SigninManagerBase* GetSigninManager(); | |
| 112 | |
| 113 // Cached pointer to the bookmarks model. | |
| 114 BookmarkModel* bookmark_model_; // weak | |
| 115 | |
| 116 private: | |
| 117 FRIEND_TEST_ALL_PREFIXES(BookmarkServerServiceTest, Cluster); | |
| 118 FRIEND_TEST_ALL_PREFIXES(BookmarkServerServiceTest, | |
| 119 ClearClusterMapOnRemoveAllBookmarks); | |
| 120 | |
| 121 // Once the model is ready this method fills in the starsid_to_bookmark_ map. | |
| 122 void BuildIdMap(); | |
| 123 | |
| 124 // net::URLFetcherDelegate methods. Called when the query is finished. | |
| 125 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | |
| 126 | |
| 127 // OAuth2TokenService::Consumer methods. | |
| 128 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request, | |
| 129 const std::string& access_token, | |
| 130 const base::Time& expiration_time) OVERRIDE; | |
| 131 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request, | |
| 132 const GoogleServiceAuthError& error) OVERRIDE; | |
| 133 | |
| 134 // The observers. | |
| 135 ObserverList<BookmarkServerServiceObserver> observers_; | |
| 136 // The Auth service is used to get a token for auth with the server. | |
| 137 ProfileOAuth2TokenService* token_service_; // Weak | |
| 138 // The request to the token service. | |
| 139 scoped_ptr<OAuth2TokenService::Request> token_request_; | |
| 140 // To get the currently signed in user. | |
| 141 SigninManagerBase* signin_manager_; // Weak | |
| 142 // To have access to the right context getter for the profile. | |
| 143 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; | |
| 144 // The fetcher used to query the server. | |
| 145 scoped_ptr<net::URLFetcher> url_fetcher_; | |
| 146 // A map from stars.id to bookmark nodes. With no null entries. | |
| 147 std::map<std::string, const BookmarkNode*> starsid_to_bookmark_; | |
| 148 // Set to true during the creation of a new bookmark in order to send only the | |
| 149 // proper notification. | |
| 150 bool inhibit_change_notifications_; | |
| 151 | |
| 152 DISALLOW_COPY_AND_ASSIGN(BookmarkServerService); | |
| 153 }; | |
| 154 } // namespace enhanced_bookmarks | |
| 155 | |
| 156 #endif // COMPONENTS_ENHANCED_BOOKMARKS_BOOKMARK_SERVER_SERVICE_H_ | |
| OLD | NEW |