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 |
| 5 #ifndef COMPONENTS_ENHANCED_BOOKMARKS_BOOKMARK_SERVER_SERVICE_H_ |
| 6 #define COMPONENTS_ENHANCED_BOOKMARKS_BOOKMARK_SERVER_SERVICE_H_ |
| 7 |
| 8 #include <string> |
| 9 #include <vector> |
| 10 |
| 11 #include "components/bookmarks/browser/bookmark_model_observer.h" |
| 12 #include "google_apis/gaia/google_service_auth_error.h" |
| 13 #include "google_apis/gaia/oauth2_token_service.h" |
| 14 #include "net/url_request/url_fetcher.h" |
| 15 #include "net/url_request/url_fetcher_delegate.h" |
| 16 #include "net/url_request/url_request_context_getter.h" |
| 17 |
| 18 class BookmarkModel; |
| 19 class ProfileOAuth2TokenService; |
| 20 class SigninManagerBase; |
| 21 |
| 22 namespace enhanced_bookmarks { |
| 23 |
| 24 class BookmarkServerService; |
| 25 |
| 26 class BookmarkServerServiceObserver { |
| 27 public: |
| 28 virtual void OnChange(BookmarkServerService* service) = 0; |
| 29 |
| 30 protected: |
| 31 virtual ~BookmarkServerServiceObserver() {} |
| 32 }; |
| 33 |
| 34 // This abstract class manages the connection to the bookmark servers and |
| 35 // stores the maps necessary to translate the response from stars.id to |
| 36 // BookmarkNodes. Subclasses just have to provide the right query and the |
| 37 // parsing of the response. |
| 38 class BookmarkServerService : protected net::URLFetcherDelegate, |
| 39 protected BookmarkModelObserver, |
| 40 private OAuth2TokenService::Consumer { |
| 41 public: |
| 42 BookmarkServerService( |
| 43 scoped_refptr<net::URLRequestContextGetter> request_context_getter, |
| 44 ProfileOAuth2TokenService* token_service, |
| 45 SigninManagerBase* signin_manager, |
| 46 BookmarkModel* bookmark_model); |
| 47 virtual ~BookmarkServerService(); |
| 48 |
| 49 void AddObserver(BookmarkServerServiceObserver* observer); |
| 50 void RemoveObserver(BookmarkServerServiceObserver* observer); |
| 51 |
| 52 protected: |
| 53 // Retrieves a bookmark by using its remote id. Returns null if nothing |
| 54 // matches. |
| 55 virtual const BookmarkNode* BookmarkForRemoteId( |
| 56 const std::string& remote_id) const; |
| 57 const std::string RemoteIDForBookmark(const BookmarkNode* bookmark) const; |
| 58 |
| 59 // Notifies the observers that something changed. |
| 60 void Notify(); |
| 61 |
| 62 // Triggers a fetch. |
| 63 void TriggerTokenRequest(bool cancel_previous); |
| 64 |
| 65 // Build the query to send to the server. Returns a newly created url_fetcher. |
| 66 virtual net::URLFetcher* CreateFetcher() = 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 |