| 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_SERVICE_H_ | 5 #ifndef COMPONENTS_ENHANCED_BOOKMARKS_BOOKMARK_SERVER_SERVICE_H_ |
| 6 #define COMPONENTS_ENHANCED_BOOKMARKS_BOOKMARK_SERVER_SERVICE_H_ | 6 #define COMPONENTS_ENHANCED_BOOKMARKS_BOOKMARK_SERVER_SERVICE_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "components/bookmarks/browser/bookmark_model_observer.h" | 11 #include "components/enhanced_bookmarks/enhanced_bookmark_model_observer.h" |
| 12 #include "google_apis/gaia/google_service_auth_error.h" | 12 #include "google_apis/gaia/google_service_auth_error.h" |
| 13 #include "google_apis/gaia/oauth2_token_service.h" | 13 #include "google_apis/gaia/oauth2_token_service.h" |
| 14 #include "net/url_request/url_fetcher.h" | 14 #include "net/url_request/url_fetcher.h" |
| 15 #include "net/url_request/url_fetcher_delegate.h" | 15 #include "net/url_request/url_fetcher_delegate.h" |
| 16 #include "net/url_request/url_request_context_getter.h" | 16 #include "net/url_request/url_request_context_getter.h" |
| 17 | 17 |
| 18 class BookmarkModel; | |
| 19 class ProfileOAuth2TokenService; | 18 class ProfileOAuth2TokenService; |
| 20 class SigninManagerBase; | 19 class SigninManagerBase; |
| 20 class BookmarkNode; |
| 21 | 21 |
| 22 namespace enhanced_bookmarks { | 22 namespace enhanced_bookmarks { |
| 23 | 23 |
| 24 class BookmarkServerService; | 24 class BookmarkServerService; |
| 25 class EnhancedBookmarkModel; |
| 25 | 26 |
| 26 class BookmarkServerServiceObserver { | 27 class BookmarkServerServiceObserver { |
| 27 public: | 28 public: |
| 28 virtual void OnChange(BookmarkServerService* service) = 0; | 29 virtual void OnChange(BookmarkServerService* service) = 0; |
| 29 | 30 |
| 30 protected: | 31 protected: |
| 31 virtual ~BookmarkServerServiceObserver() {} | 32 virtual ~BookmarkServerServiceObserver() {} |
| 32 }; | 33 }; |
| 33 | 34 |
| 34 // This abstract class manages the connection to the bookmark servers and | 35 // 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 // 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 // BookmarkNodes. Subclasses just have to provide the right query and the |
| 37 // parsing of the response. | 38 // parsing of the response. |
| 38 class BookmarkServerService : protected net::URLFetcherDelegate, | 39 class BookmarkServerService : protected net::URLFetcherDelegate, |
| 39 protected BookmarkModelObserver, | 40 private OAuth2TokenService::Consumer, |
| 40 private OAuth2TokenService::Consumer { | 41 public EnhancedBookmarkModelObserver { |
| 41 public: | 42 public: |
| 42 BookmarkServerService( | 43 BookmarkServerService( |
| 43 scoped_refptr<net::URLRequestContextGetter> request_context_getter, | 44 scoped_refptr<net::URLRequestContextGetter> request_context_getter, |
| 44 ProfileOAuth2TokenService* token_service, | 45 ProfileOAuth2TokenService* token_service, |
| 45 SigninManagerBase* signin_manager, | 46 SigninManagerBase* signin_manager, |
| 46 BookmarkModel* bookmark_model); | 47 EnhancedBookmarkModel* enhanced_bookmark_model); |
| 47 virtual ~BookmarkServerService(); | 48 virtual ~BookmarkServerService(); |
| 48 | 49 |
| 49 void AddObserver(BookmarkServerServiceObserver* observer); | 50 void AddObserver(BookmarkServerServiceObserver* observer); |
| 50 void RemoveObserver(BookmarkServerServiceObserver* observer); | 51 void RemoveObserver(BookmarkServerServiceObserver* observer); |
| 51 | 52 |
| 52 protected: | 53 protected: |
| 53 // Retrieves a bookmark by using its remote id. Returns null if nothing | 54 // Retrieves a bookmark by using its remote id. Returns null if nothing |
| 54 // matches. | 55 // matches. |
| 55 virtual const BookmarkNode* BookmarkForRemoteId( | 56 virtual const BookmarkNode* BookmarkForRemoteId( |
| 56 const std::string& remote_id) const; | 57 const std::string& remote_id) const; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 68 // Processes the response to the query. Returns true on successful parsing, | 69 // 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 // 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 // to true by default, if changed to false there will be no OnChange |
| 71 // notification send. | 72 // notification send. |
| 72 virtual bool ProcessResponse(const std::string& response, | 73 virtual bool ProcessResponse(const std::string& response, |
| 73 bool* should_notify) = 0; | 74 bool* should_notify) = 0; |
| 74 | 75 |
| 75 // If the token can't be retrieved or the query fails this method is called. | 76 // If the token can't be retrieved or the query fails this method is called. |
| 76 virtual void CleanAfterFailure() = 0; | 77 virtual void CleanAfterFailure() = 0; |
| 77 | 78 |
| 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(); | 79 SigninManagerBase* GetSigninManager(); |
| 112 | 80 |
| 113 // Cached pointer to the bookmarks model. | 81 // Cached pointer to the bookmarks model. |
| 114 BookmarkModel* bookmark_model_; // weak | 82 EnhancedBookmarkModel* model_; // weak |
| 115 | 83 |
| 116 private: | 84 private: |
| 117 FRIEND_TEST_ALL_PREFIXES(BookmarkServerServiceTest, Cluster); | 85 FRIEND_TEST_ALL_PREFIXES(BookmarkServerServiceTest, Cluster); |
| 118 FRIEND_TEST_ALL_PREFIXES(BookmarkServerServiceTest, | 86 FRIEND_TEST_ALL_PREFIXES(BookmarkServerServiceTest, |
| 119 ClearClusterMapOnRemoveAllBookmarks); | 87 ClearClusterMapOnRemoveAllBookmarks); |
| 120 | 88 |
| 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. | 89 // net::URLFetcherDelegate methods. Called when the query is finished. |
| 125 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | 90 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
| 126 | 91 |
| 127 // OAuth2TokenService::Consumer methods. | 92 // OAuth2TokenService::Consumer methods. |
| 128 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request, | 93 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request, |
| 129 const std::string& access_token, | 94 const std::string& access_token, |
| 130 const base::Time& expiration_time) OVERRIDE; | 95 const base::Time& expiration_time) OVERRIDE; |
| 131 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request, | 96 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request, |
| 132 const GoogleServiceAuthError& error) OVERRIDE; | 97 const GoogleServiceAuthError& error) OVERRIDE; |
| 133 | 98 |
| 134 // The observers. | 99 // The observers. |
| 135 ObserverList<BookmarkServerServiceObserver> observers_; | 100 ObserverList<BookmarkServerServiceObserver> observers_; |
| 136 // The Auth service is used to get a token for auth with the server. | 101 // The Auth service is used to get a token for auth with the server. |
| 137 ProfileOAuth2TokenService* token_service_; // Weak | 102 ProfileOAuth2TokenService* token_service_; // Weak |
| 138 // The request to the token service. | 103 // The request to the token service. |
| 139 scoped_ptr<OAuth2TokenService::Request> token_request_; | 104 scoped_ptr<OAuth2TokenService::Request> token_request_; |
| 140 // To get the currently signed in user. | 105 // To get the currently signed in user. |
| 141 SigninManagerBase* signin_manager_; // Weak | 106 SigninManagerBase* signin_manager_; // Weak |
| 142 // To have access to the right context getter for the profile. | 107 // To have access to the right context getter for the profile. |
| 143 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; | 108 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
| 144 // The fetcher used to query the server. | 109 // The fetcher used to query the server. |
| 145 scoped_ptr<net::URLFetcher> url_fetcher_; | 110 scoped_ptr<net::URLFetcher> url_fetcher_; |
| 146 // A map from stars.id to bookmark nodes. With no null entries. | 111 // A map from stars.id to bookmark nodes. With no null entries. |
| 147 std::map<std::string, const BookmarkNode*> starsid_to_bookmark_; | 112 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 | 113 |
| 152 DISALLOW_COPY_AND_ASSIGN(BookmarkServerService); | 114 DISALLOW_COPY_AND_ASSIGN(BookmarkServerService); |
| 153 }; | 115 }; |
| 154 } // namespace enhanced_bookmarks | 116 } // namespace enhanced_bookmarks |
| 155 | 117 |
| 156 #endif // COMPONENTS_ENHANCED_BOOKMARKS_BOOKMARK_SERVER_SERVICE_H_ | 118 #endif // COMPONENTS_ENHANCED_BOOKMARKS_BOOKMARK_SERVER_SERVICE_H_ |
| OLD | NEW |