[AndroidWebViewShell] Replace rebaseline script with a new version using test_runner.py
[chromium-blink-merge.git] / components / enhanced_bookmarks / bookmark_server_service.cc
blob23c85b1441f921c674d2777f1796e434580b28ea
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 #include "components/enhanced_bookmarks/bookmark_server_service.h"
7 #include "base/auto_reset.h"
8 #include "components/enhanced_bookmarks/enhanced_bookmark_model.h"
9 #include "components/signin/core/browser/profile_oauth2_token_service.h"
10 #include "components/signin/core/browser/signin_manager_base.h"
11 #include "google_apis/gaia/gaia_constants.h"
12 #include "net/base/load_flags.h"
13 #include "net/url_request/url_request_context_getter.h"
14 #include "ui/base/models/tree_node_iterator.h"
16 using bookmarks::BookmarkNode;
18 namespace enhanced_bookmarks {
20 BookmarkServerService::BookmarkServerService(
21 scoped_refptr<net::URLRequestContextGetter> request_context_getter,
22 ProfileOAuth2TokenService* token_service,
23 SigninManagerBase* signin_manager,
24 EnhancedBookmarkModel* enhanced_bookmark_model)
25 : OAuth2TokenService::Consumer("bookmark_server_service"),
26 model_(enhanced_bookmark_model),
27 token_service_(token_service),
28 signin_manager_(signin_manager),
29 request_context_getter_(request_context_getter) {
30 DCHECK(request_context_getter.get());
31 DCHECK(token_service);
32 DCHECK(signin_manager);
33 DCHECK(enhanced_bookmark_model);
34 model_->AddObserver(this);
37 BookmarkServerService::~BookmarkServerService() {
38 model_->RemoveObserver(this);
41 void BookmarkServerService::AddObserver(
42 BookmarkServerServiceObserver* observer) {
43 observers_.AddObserver(observer);
46 void BookmarkServerService::RemoveObserver(
47 BookmarkServerServiceObserver* observer) {
48 observers_.RemoveObserver(observer);
51 const BookmarkNode* BookmarkServerService::BookmarkForRemoteId(
52 const std::string& remote_id) const {
53 return model_->BookmarkForRemoteId(remote_id);
56 const std::string BookmarkServerService::RemoteIDForBookmark(
57 const BookmarkNode* bookmark) const {
58 return model_->GetRemoteId(bookmark);
61 void BookmarkServerService::Cancel() {
62 url_fetcher_.reset();
63 token_request_.reset();
66 void BookmarkServerService::Notify() {
67 FOR_EACH_OBSERVER(BookmarkServerServiceObserver, observers_, OnChange(this));
70 void BookmarkServerService::TriggerTokenRequest(bool cancel_previous) {
71 if (cancel_previous)
72 url_fetcher_.reset();
74 if (token_request_ || url_fetcher_)
75 return; // Fetcher is already running.
77 if (!signin_manager_->IsAuthenticated()) {
78 // User is not signed in.
79 CleanAfterFailure();
80 Notify();
81 return;
83 // Find a token.
84 OAuth2TokenService::ScopeSet scopes;
85 scopes.insert(GaiaConstants::kChromeSyncOAuth2Scope);
86 token_request_ = token_service_->StartRequest(
87 signin_manager_->GetAuthenticatedAccountId(), scopes, this);
91 // OAuth2AccessTokenConsumer methods.
93 void BookmarkServerService::OnGetTokenSuccess(
94 const OAuth2TokenService::Request* request,
95 const std::string& access_token,
96 const base::Time& expiration_time) {
97 url_fetcher_ = CreateFetcher();
99 // Free the token request.
100 token_request_.reset();
102 if (!url_fetcher_) {
103 CleanAfterFailure();
104 Notify();
105 return;
107 url_fetcher_->SetRequestContext(request_context_getter_.get());
109 // Add the token.
110 std::string headers;
111 headers = "Authorization: Bearer ";
112 headers += access_token;
113 headers += "\r\n";
114 url_fetcher_->SetExtraRequestHeaders(headers);
116 // Do not pollute the cookie store with cruft, or mix the users cookie in this
117 // request.
118 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
119 net::LOAD_DO_NOT_SAVE_COOKIES);
121 url_fetcher_->Start();
124 void BookmarkServerService::OnGetTokenFailure(
125 const OAuth2TokenService::Request* request,
126 const GoogleServiceAuthError& error) {
127 // Free the request.
128 token_request_.reset();
129 CleanAfterFailure();
130 Notify();
134 // net::URLFetcherDelegate methods.
136 void BookmarkServerService::OnURLFetchComplete(const net::URLFetcher* source) {
137 scoped_ptr<net::URLFetcher> url_fetcher(url_fetcher_.Pass());
138 std::string response;
139 bool should_notify = true;
141 if (url_fetcher->GetResponseCode() != 200 ||
142 !url_fetcher->GetResponseAsString(&response) ||
143 !ProcessResponse(response, &should_notify)) {
144 CleanAfterFailure();
146 if (should_notify)
147 Notify();
150 void BookmarkServerService::EnhancedBookmarkModelShuttingDown() {
151 NOTREACHED();
154 SigninManagerBase* BookmarkServerService::GetSigninManager() {
155 DCHECK(signin_manager_);
156 return signin_manager_;
159 } // namespace enhanced_bookmarks