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