Only use web app style header for hosted apps.
[chromium-blink-merge.git] / components / enhanced_bookmarks / bookmark_server_service.cc
blob73cd8f8c93f4e3607020e62789bd84f6564f1bb7
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 namespace enhanced_bookmarks {
18 BookmarkServerService::BookmarkServerService(
19 scoped_refptr<net::URLRequestContextGetter> request_context_getter,
20 ProfileOAuth2TokenService* token_service,
21 SigninManagerBase* signin_manager,
22 EnhancedBookmarkModel* enhanced_bookmark_model)
23 : OAuth2TokenService::Consumer("bookmark_server_service"),
24 model_(enhanced_bookmark_model),
25 token_service_(token_service),
26 signin_manager_(signin_manager),
27 request_context_getter_(request_context_getter) {
28 DCHECK(request_context_getter.get());
29 DCHECK(token_service);
30 DCHECK(signin_manager);
31 DCHECK(enhanced_bookmark_model);
32 model_->AddObserver(this);
35 BookmarkServerService::~BookmarkServerService() {
36 model_->RemoveObserver(this);
39 void BookmarkServerService::AddObserver(
40 BookmarkServerServiceObserver* observer) {
41 observers_.AddObserver(observer);
44 void BookmarkServerService::RemoveObserver(
45 BookmarkServerServiceObserver* observer) {
46 observers_.RemoveObserver(observer);
49 const BookmarkNode* BookmarkServerService::BookmarkForRemoteId(
50 const std::string& remote_id) const {
51 return model_->BookmarkForRemoteId(remote_id);
54 const std::string BookmarkServerService::RemoteIDForBookmark(
55 const BookmarkNode* bookmark) const {
56 return model_->GetRemoteId(bookmark);
59 void BookmarkServerService::Cancel() {
60 url_fetcher_.reset();
61 token_request_.reset();
64 void BookmarkServerService::Notify() {
65 FOR_EACH_OBSERVER(BookmarkServerServiceObserver, observers_, OnChange(this));
68 void BookmarkServerService::TriggerTokenRequest(bool cancel_previous) {
69 if (cancel_previous)
70 url_fetcher_.reset();
72 if (token_request_ || url_fetcher_)
73 return; // Fetcher is already running.
75 if (!signin_manager_->IsAuthenticated()) {
76 // User is not signed in.
77 CleanAfterFailure();
78 Notify();
79 return;
81 // Find a token.
82 OAuth2TokenService::ScopeSet scopes;
83 scopes.insert(GaiaConstants::kChromeSyncOAuth2Scope);
84 token_request_ = token_service_->StartRequest(
85 signin_manager_->GetAuthenticatedAccountId(), scopes, this);
89 // OAuth2AccessTokenConsumer methods.
91 void BookmarkServerService::OnGetTokenSuccess(
92 const OAuth2TokenService::Request* request,
93 const std::string& access_token,
94 const base::Time& expiration_time) {
95 url_fetcher_ = CreateFetcher();
97 // Free the token request.
98 token_request_.reset();
100 if (!url_fetcher_) {
101 CleanAfterFailure();
102 Notify();
103 return;
105 url_fetcher_->SetRequestContext(request_context_getter_.get());
107 // Add the token.
108 std::string headers;
109 headers = "Authorization: Bearer ";
110 headers += access_token;
111 headers += "\r\n";
112 url_fetcher_->SetExtraRequestHeaders(headers);
114 // Do not pollute the cookie store with cruft, or mix the users cookie in this
115 // request.
116 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
117 net::LOAD_DO_NOT_SAVE_COOKIES);
119 url_fetcher_->Start();
122 void BookmarkServerService::OnGetTokenFailure(
123 const OAuth2TokenService::Request* request,
124 const GoogleServiceAuthError& error) {
125 // Free the request.
126 token_request_.reset();
127 CleanAfterFailure();
128 Notify();
132 // net::URLFetcherDelegate methods.
134 void BookmarkServerService::OnURLFetchComplete(const net::URLFetcher* source) {
135 scoped_ptr<net::URLFetcher> url_fetcher(url_fetcher_.Pass());
136 std::string response;
137 bool should_notify = true;
139 if (url_fetcher->GetResponseCode() != 200 ||
140 !url_fetcher->GetResponseAsString(&response) ||
141 !ProcessResponse(response, &should_notify)) {
142 CleanAfterFailure();
144 if (should_notify)
145 Notify();
148 void BookmarkServerService::EnhancedBookmarkModelShuttingDown() {
149 NOTREACHED();
152 SigninManagerBase* BookmarkServerService::GetSigninManager() {
153 DCHECK(signin_manager_);
154 return signin_manager_;
157 } // namespace enhanced_bookmarks