1 // Copyright (c) 2011 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 #include "chrome/browser/chromeos/login/image_downloader.h"
6
7 #include <vector>
8
9 #include "base/logging.h"
10 #include "base/message_loop.h"
11 #include "base/string_util.h"
12 #include "base/stringprintf.h"
13 #include "content/browser/browser_thread.h"
14 #include "chrome/browser/profiles/profile_manager.h"
15 #include "chrome/common/net/url_fetcher.h"
16
17 namespace chromeos {
18
19 namespace {
20
21 // Template for optional authorization header.
22 const char kAuthorizationHeader[] = "Authorization: GoogleLogin auth=%s";
23
24 } // namespace
25
ImageDownloader(ImageDecoder::Delegate * delegate,const GURL & image_url,const std::string & auth_token)26 ImageDownloader::ImageDownloader(ImageDecoder::Delegate* delegate,
27 const GURL& image_url,
28 const std::string& auth_token)
29 : delegate_(delegate) {
30 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
31 image_fetcher_.reset(new URLFetcher(GURL(image_url), URLFetcher::GET, this));
32 image_fetcher_->set_request_context(
33 ProfileManager::GetDefaultProfile()->GetRequestContext());
34 if (!auth_token.empty()) {
35 image_fetcher_->set_extra_request_headers(
36 base::StringPrintf(kAuthorizationHeader, auth_token.c_str()));
37 }
38 image_fetcher_->Start();
39 }
40
OnURLFetchComplete(const URLFetcher * source,const GURL & url,const net::URLRequestStatus & status,int response_code,const ResponseCookies & cookies,const std::string & data)41 void ImageDownloader::OnURLFetchComplete(const URLFetcher* source,
42 const GURL& url,
43 const net::URLRequestStatus& status,
44 int response_code,
45 const ResponseCookies& cookies,
46 const std::string& data) {
47 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
48 if (response_code != 200) {
49 LOG(ERROR) << "Response code is " << response_code;
50 LOG(ERROR) << "Url is " << url.spec();
51 LOG(ERROR) << "Data is " << data;
52 MessageLoop::current()->DeleteSoon(FROM_HERE, this);
53 return;
54 }
55
56 VLOG(1) << "Decoding the image...";
57 scoped_refptr<ImageDecoder> image_decoder = new ImageDecoder(delegate_, data);
58 image_decoder->Start();
59 MessageLoop::current()->DeleteSoon(FROM_HERE, this);
60 }
61
62 } // namespace chromeos
63