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.
5 #ifndef COMPONENTS_ENHANCED_BOOKMARKS_BOOKMARK_SERVER_SERVICE_H_
6 #define COMPONENTS_ENHANCED_BOOKMARKS_BOOKMARK_SERVER_SERVICE_H_
11 #include "components/enhanced_bookmarks/enhanced_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"
18 class ProfileOAuth2TokenService
;
19 class SigninManagerBase
;
25 namespace enhanced_bookmarks
{
27 class BookmarkServerService
;
28 class EnhancedBookmarkModel
;
30 class BookmarkServerServiceObserver
{
32 virtual void OnChange(BookmarkServerService
* service
) = 0;
35 virtual ~BookmarkServerServiceObserver() {}
38 // This abstract class manages the connection to the bookmark servers and
39 // stores the maps necessary to translate the response from stars.id to
40 // BookmarkNodes. Subclasses just have to provide the right query and the
41 // parsing of the response.
42 class BookmarkServerService
: protected net::URLFetcherDelegate
,
43 private OAuth2TokenService::Consumer
,
44 public EnhancedBookmarkModelObserver
{
46 BookmarkServerService(
47 scoped_refptr
<net::URLRequestContextGetter
> request_context_getter
,
48 ProfileOAuth2TokenService
* token_service
,
49 SigninManagerBase
* signin_manager
,
50 EnhancedBookmarkModel
* enhanced_bookmark_model
);
51 ~BookmarkServerService() override
;
53 virtual void AddObserver(BookmarkServerServiceObserver
* observer
);
54 void RemoveObserver(BookmarkServerServiceObserver
* observer
);
57 // Retrieves a bookmark by using its remote id. Returns null if nothing
59 virtual const bookmarks::BookmarkNode
* BookmarkForRemoteId(
60 const std::string
& remote_id
) const;
61 const std::string
RemoteIDForBookmark(
62 const bookmarks::BookmarkNode
* bookmark
) const;
64 // Cancels the ongoing request, if any.
67 // Notifies the observers that something changed.
71 void TriggerTokenRequest(bool cancel_previous
);
73 // Build the query to send to the server. Returns a newly created url_fetcher.
74 virtual scoped_ptr
<net::URLFetcher
> CreateFetcher() = 0;
76 // Processes the response to the query. Returns true on successful parsing,
77 // false on failure. The implementation can assume that |should_notify| is set
78 // to true by default, if changed to false there will be no OnChange
80 virtual bool ProcessResponse(const std::string
& response
,
81 bool* should_notify
) = 0;
83 // If the token can't be retrieved or the query fails this method is called.
84 virtual void CleanAfterFailure() = 0;
86 // EnhancedBookmarkModelObserver:
87 void EnhancedBookmarkModelShuttingDown() override
;
89 SigninManagerBase
* GetSigninManager();
91 // Cached pointer to the bookmarks model.
92 EnhancedBookmarkModel
* model_
; // weak
96 base::ObserverList
<BookmarkServerServiceObserver
> observers_
;
99 FRIEND_TEST_ALL_PREFIXES(BookmarkServerServiceTest
, Cluster
);
100 FRIEND_TEST_ALL_PREFIXES(BookmarkServerServiceTest
, SignOut
);
101 FRIEND_TEST_ALL_PREFIXES(BookmarkServerServiceTest
,
102 ClearClusterMapOnRemoveAllBookmarks
);
104 // net::URLFetcherDelegate methods. Called when the query is finished.
105 void OnURLFetchComplete(const net::URLFetcher
* source
) override
;
107 // OAuth2TokenService::Consumer methods.
108 void OnGetTokenSuccess(const OAuth2TokenService::Request
* request
,
109 const std::string
& access_token
,
110 const base::Time
& expiration_time
) override
;
111 void OnGetTokenFailure(const OAuth2TokenService::Request
* request
,
112 const GoogleServiceAuthError
& error
) override
;
114 // The Auth service is used to get a token for auth with the server.
115 ProfileOAuth2TokenService
* token_service_
; // Weak
116 // The request to the token service.
117 scoped_ptr
<OAuth2TokenService::Request
> token_request_
;
118 // To get the currently signed in user.
119 SigninManagerBase
* signin_manager_
; // Weak
120 // To have access to the right context getter for the profile.
121 scoped_refptr
<net::URLRequestContextGetter
> request_context_getter_
;
122 // The fetcher used to query the server.
123 scoped_ptr
<net::URLFetcher
> url_fetcher_
;
125 DISALLOW_COPY_AND_ASSIGN(BookmarkServerService
);
127 } // namespace enhanced_bookmarks
129 #endif // COMPONENTS_ENHANCED_BOOKMARKS_BOOKMARK_SERVER_SERVICE_H_