Home | History | Annotate | Download | only in login
      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 #ifndef CHROME_BROWSER_CHROMEOS_LOGIN_USER_IMAGE_DOWNLOADER_H_
      6 #define CHROME_BROWSER_CHROMEOS_LOGIN_USER_IMAGE_DOWNLOADER_H_
      7 #pragma once
      8 
      9 #include <string>
     10 
     11 #include "base/basictypes.h"
     12 #include "base/memory/scoped_ptr.h"
     13 #include "chrome/browser/chromeos/login/image_decoder.h"
     14 #include "chrome/common/net/url_fetcher.h"
     15 
     16 class ListValue;
     17 
     18 namespace chromeos {
     19 
     20 // Gets user image URL from user's Google Profile, downloads the image,
     21 // executes image decode and calls UserManager to store image in a file with
     22 // path to it stored in local state dictionary.
     23 class UserImageDownloader : public URLFetcher::Delegate,
     24                             public ImageDecoder::Delegate {
     25  public:
     26   // |auth_token| is a authentication token received in ClientLogin
     27   // response, used for requests sent to Contacts API.
     28   // Starts downloading the picture. Object is deleted as reference counted
     29   // object.
     30   UserImageDownloader(const std::string& username,
     31                       const std::string& auth_token);
     32 
     33  private:
     34   // It's a reference counted object, so destructor is private.
     35   ~UserImageDownloader();
     36 
     37   // Overriden from URLFetcher::Delegate:
     38   virtual void OnURLFetchComplete(const URLFetcher* source,
     39                                   const GURL& url,
     40                                   const net::URLRequestStatus& status,
     41                                   int response_code,
     42                                   const ResponseCookies& cookies,
     43                                   const std::string& data);
     44 
     45   // Overriden from ImageDecoder::Delegate:
     46   virtual void OnImageDecoded(const SkBitmap& decoded_image);
     47 
     48   // Parses received JSON data looking for user image url.
     49   // If succeeded, returns true and stores the url in |image_url| parameter.
     50   // Otherwise, returns false.
     51   bool GetImageURL(const std::string& json_data, GURL* image_url) const;
     52 
     53   // Searches for image url in a list of contacts matching contact address
     54   // with user email. Returns true and image url if succeeds, false
     55   // otherwise.
     56   bool GetImageURLFromEntries(ListValue* entry_list, GURL* image_url) const;
     57 
     58   // Checks if email list contains user email. Returns true if match is
     59   // found.
     60   bool IsUserEntry(ListValue* email_list) const;
     61 
     62   // Searches for image url in list of links for the found contact.
     63   // Returns true and image url if succeeds, false otherwise.
     64   bool GetImageURLFromLinks(ListValue* link_list, GURL* image_url) const;
     65 
     66   // Encodes user image in PNG format and saves the result to the file
     67   // specified. Should work on IO thread.
     68   void SaveImageAsPNG(const std::string& filename, const SkBitmap& image);
     69 
     70   // Fetcher for user's profile page.
     71   scoped_ptr<URLFetcher> profile_fetcher_;
     72 
     73   // Username saved to use as a key for user picture in preferences.
     74   std::string username_;
     75 
     76   // Authentication token to use for image download.
     77   std::string auth_token_;
     78 
     79   DISALLOW_COPY_AND_ASSIGN(UserImageDownloader);
     80 };
     81 
     82 }  // namespace chromeos
     83 
     84 #endif  // CHROME_BROWSER_CHROMEOS_LOGIN_USER_IMAGE_DOWNLOADER_H_
     85 
     86