summaryrefslogtreecommitdiffstats
path: root/components/enhanced_bookmarks/bookmark_server_service.cc
blob: 23c85b1441f921c674d2777f1796e434580b28ea (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// 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.

#include "components/enhanced_bookmarks/bookmark_server_service.h"

#include "base/auto_reset.h"
#include "components/enhanced_bookmarks/enhanced_bookmark_model.h"
#include "components/signin/core/browser/profile_oauth2_token_service.h"
#include "components/signin/core/browser/signin_manager_base.h"
#include "google_apis/gaia/gaia_constants.h"
#include "net/base/load_flags.h"
#include "net/url_request/url_request_context_getter.h"
#include "ui/base/models/tree_node_iterator.h"

using bookmarks::BookmarkNode;

namespace enhanced_bookmarks {

BookmarkServerService::BookmarkServerService(
    scoped_refptr<net::URLRequestContextGetter> request_context_getter,
    ProfileOAuth2TokenService* token_service,
    SigninManagerBase* signin_manager,
    EnhancedBookmarkModel* enhanced_bookmark_model)
    : OAuth2TokenService::Consumer("bookmark_server_service"),
      model_(enhanced_bookmark_model),
      token_service_(token_service),
      signin_manager_(signin_manager),
      request_context_getter_(request_context_getter) {
  DCHECK(request_context_getter.get());
  DCHECK(token_service);
  DCHECK(signin_manager);
  DCHECK(enhanced_bookmark_model);
  model_->AddObserver(this);
}

BookmarkServerService::~BookmarkServerService() {
  model_->RemoveObserver(this);
}

void BookmarkServerService::AddObserver(
    BookmarkServerServiceObserver* observer) {
  observers_.AddObserver(observer);
}

void BookmarkServerService::RemoveObserver(
    BookmarkServerServiceObserver* observer) {
  observers_.RemoveObserver(observer);
}

const BookmarkNode* BookmarkServerService::BookmarkForRemoteId(
    const std::string& remote_id) const {
  return model_->BookmarkForRemoteId(remote_id);
}

const std::string BookmarkServerService::RemoteIDForBookmark(
    const BookmarkNode* bookmark) const {
  return model_->GetRemoteId(bookmark);
}

void BookmarkServerService::Cancel() {
  url_fetcher_.reset();
  token_request_.reset();
}

void BookmarkServerService::Notify() {
  FOR_EACH_OBSERVER(BookmarkServerServiceObserver, observers_, OnChange(this));
}

void BookmarkServerService::TriggerTokenRequest(bool cancel_previous) {
  if (cancel_previous)
    url_fetcher_.reset();

  if (token_request_ || url_fetcher_)
    return;  // Fetcher is already running.

  if (!signin_manager_->IsAuthenticated()) {
    // User is not signed in.
    CleanAfterFailure();
    Notify();
    return;
  }
  // Find a token.
  OAuth2TokenService::ScopeSet scopes;
  scopes.insert(GaiaConstants::kChromeSyncOAuth2Scope);
  token_request_ = token_service_->StartRequest(
      signin_manager_->GetAuthenticatedAccountId(), scopes, this);
}

//
// OAuth2AccessTokenConsumer methods.
//
void BookmarkServerService::OnGetTokenSuccess(
    const OAuth2TokenService::Request* request,
    const std::string& access_token,
    const base::Time& expiration_time) {
  url_fetcher_ = CreateFetcher();

  // Free the token request.
  token_request_.reset();

  if (!url_fetcher_) {
    CleanAfterFailure();
    Notify();
    return;
  }
  url_fetcher_->SetRequestContext(request_context_getter_.get());

  // Add the token.
  std::string headers;
  headers = "Authorization: Bearer ";
  headers += access_token;
  headers += "\r\n";
  url_fetcher_->SetExtraRequestHeaders(headers);

  // Do not pollute the cookie store with cruft, or mix the users cookie in this
  // request.
  url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
                             net::LOAD_DO_NOT_SAVE_COOKIES);

  url_fetcher_->Start();
}

void BookmarkServerService::OnGetTokenFailure(
    const OAuth2TokenService::Request* request,
    const GoogleServiceAuthError& error) {
  // Free the request.
  token_request_.reset();
  CleanAfterFailure();
  Notify();
}

//
// net::URLFetcherDelegate methods.
//
void BookmarkServerService::OnURLFetchComplete(const net::URLFetcher* source) {
  scoped_ptr<net::URLFetcher> url_fetcher(url_fetcher_.Pass());
  std::string response;
  bool should_notify = true;

  if (url_fetcher->GetResponseCode() != 200 ||
      !url_fetcher->GetResponseAsString(&response) ||
      !ProcessResponse(response, &should_notify)) {
    CleanAfterFailure();
  }
  if (should_notify)
    Notify();
}

void BookmarkServerService::EnhancedBookmarkModelShuttingDown() {
  NOTREACHED();
}

SigninManagerBase* BookmarkServerService::GetSigninManager() {
  DCHECK(signin_manager_);
  return signin_manager_;
}

}  // namespace enhanced_bookmarks