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