summaryrefslogtreecommitdiffstats
path: root/components/enhanced_bookmarks/bookmark_server_service.h
blob: 9428f85f5b1a52ba9e9e770bdb04931685eb5cc6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_ENHANCED_BOOKMARKS_BOOKMARK_SERVER_SERVICE_H_
#define COMPONENTS_ENHANCED_BOOKMARKS_BOOKMARK_SERVER_SERVICE_H_

#include <string>
#include <vector>

#include "components/enhanced_bookmarks/enhanced_bookmark_model_observer.h"
#include "google_apis/gaia/google_service_auth_error.h"
#include "google_apis/gaia/oauth2_token_service.h"
#include "net/url_request/url_fetcher.h"
#include "net/url_request/url_fetcher_delegate.h"
#include "net/url_request/url_request_context_getter.h"

class ProfileOAuth2TokenService;
class SigninManagerBase;

namespace bookmarks {
class BookmarkNode;
}

namespace enhanced_bookmarks {

class BookmarkServerService;
class EnhancedBookmarkModel;

class BookmarkServerServiceObserver {
 public:
  virtual void OnChange(BookmarkServerService* service) = 0;

 protected:
  virtual ~BookmarkServerServiceObserver() {}
};

// This abstract class manages the connection to the bookmark servers and
// stores the maps necessary to translate the response from stars.id to
// BookmarkNodes. Subclasses just have to provide the right query and the
// parsing of the response.
class BookmarkServerService : protected net::URLFetcherDelegate,
                              private OAuth2TokenService::Consumer,
                              public EnhancedBookmarkModelObserver {
 public:
  BookmarkServerService(
      scoped_refptr<net::URLRequestContextGetter> request_context_getter,
      ProfileOAuth2TokenService* token_service,
      SigninManagerBase* signin_manager,
      EnhancedBookmarkModel* enhanced_bookmark_model);
  ~BookmarkServerService() override;

  virtual void AddObserver(BookmarkServerServiceObserver* observer);
  void RemoveObserver(BookmarkServerServiceObserver* observer);

 protected:
  // Retrieves a bookmark by using its remote id. Returns null if nothing
  // matches.
  virtual const bookmarks::BookmarkNode* BookmarkForRemoteId(
      const std::string& remote_id) const;
  const std::string RemoteIDForBookmark(
      const bookmarks::BookmarkNode* bookmark) const;

  // Cancels the ongoing request, if any.
  void Cancel();

  // Notifies the observers that something changed.
  void Notify();

  // Triggers a fetch.
  void TriggerTokenRequest(bool cancel_previous);

  // Build the query to send to the server. Returns a newly created url_fetcher.
  virtual scoped_ptr<net::URLFetcher> CreateFetcher() = 0;

  // Processes the response to the query. Returns true on successful parsing,
  // false on failure. The implementation can assume that |should_notify| is set
  // to true by default, if changed to false there will be no OnChange
  // notification send.
  virtual bool ProcessResponse(const std::string& response,
                               bool* should_notify) = 0;

  // If the token can't be retrieved or the query fails this method is called.
  virtual void CleanAfterFailure() = 0;

  // EnhancedBookmarkModelObserver:
  void EnhancedBookmarkModelShuttingDown() override;

  SigninManagerBase* GetSigninManager();

  // Cached pointer to the bookmarks model.
  EnhancedBookmarkModel* model_;  // weak

 protected:
  // The observers.
  base::ObserverList<BookmarkServerServiceObserver> observers_;

 private:
  FRIEND_TEST_ALL_PREFIXES(BookmarkServerServiceTest, Cluster);
  FRIEND_TEST_ALL_PREFIXES(BookmarkServerServiceTest, SignOut);
  FRIEND_TEST_ALL_PREFIXES(BookmarkServerServiceTest,
                           ClearClusterMapOnRemoveAllBookmarks);

  // net::URLFetcherDelegate methods. Called when the query is finished.
  void OnURLFetchComplete(const net::URLFetcher* source) override;

  // OAuth2TokenService::Consumer methods.
  void OnGetTokenSuccess(const OAuth2TokenService::Request* request,
                         const std::string& access_token,
                         const base::Time& expiration_time) override;
  void OnGetTokenFailure(const OAuth2TokenService::Request* request,
                         const GoogleServiceAuthError& error) override;

  // The Auth service is used to get a token for auth with the server.
  ProfileOAuth2TokenService* token_service_;  // Weak
  // The request to the token service.
  scoped_ptr<OAuth2TokenService::Request> token_request_;
  // To get the currently signed in user.
  SigninManagerBase* signin_manager_;  // Weak
  // To have access to the right context getter for the profile.
  scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
  // The fetcher used to query the server.
  scoped_ptr<net::URLFetcher> url_fetcher_;

  DISALLOW_COPY_AND_ASSIGN(BookmarkServerService);
};
}  // namespace enhanced_bookmarks

#endif  // COMPONENTS_ENHANCED_BOOKMARKS_BOOKMARK_SERVER_SERVICE_H_