| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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/ui/webui/options2/chromeos/user_image_source.h" | |
| 6 | |
| 7 #include "base/memory/ref_counted_memory.h" | |
| 8 #include "base/message_loop.h" | |
| 9 #include "base/string_split.h" | |
| 10 #include "chrome/browser/chromeos/login/default_user_images.h" | |
| 11 #include "chrome/browser/chromeos/login/user_manager.h" | |
| 12 #include "chrome/browser/ui/webui/web_ui_util.h" | |
| 13 #include "chrome/common/url_constants.h" | |
| 14 #include "googleurl/src/url_parse.h" | |
| 15 #include "grit/theme_resources.h" | |
| 16 #include "ui/base/resource/resource_bundle.h" | |
| 17 #include "ui/gfx/codec/png_codec.h" | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 // Animated key is used in user image URL requests to specify that | |
| 22 // animated version of user image is required. Without that key | |
| 23 // non-animated version of user image should be returned. | |
| 24 const char kKeyAnimated[] = "animated"; | |
| 25 | |
| 26 // Parses the user image URL, which looks like | |
| 27 // "chrome://userimage/user@host?key1=value1&...&key_n=value_n@<scale>x", | |
| 28 // to user email, optional parameters and scale factor. | |
| 29 void ParseRequest(const GURL& url, | |
| 30 std::string* email, | |
| 31 bool* is_image_animated, | |
| 32 ui::ScaleFactor* scale_factor) { | |
| 33 DCHECK(url.is_valid()); | |
| 34 web_ui_util::ParsePathAndScale(url, email, scale_factor); | |
| 35 std::string url_spec = url.possibly_invalid_spec(); | |
| 36 url_parse::Component query = url.parsed_for_possibly_invalid_spec().query; | |
| 37 url_parse::Component key, value; | |
| 38 *is_image_animated = false; | |
| 39 while (ExtractQueryKeyValue(url_spec.c_str(), &query, &key, &value)) { | |
| 40 if (url_spec.substr(key.begin, key.len) == kKeyAnimated) { | |
| 41 *is_image_animated = true; | |
| 42 break; | |
| 43 } | |
| 44 } | |
| 45 } | |
| 46 | |
| 47 } // namespace | |
| 48 | |
| 49 namespace chromeos { | |
| 50 namespace options { | |
| 51 | |
| 52 base::RefCountedMemory* UserImageSource::GetUserImage( | |
| 53 const std::string& email, | |
| 54 bool is_image_animated, | |
| 55 ui::ScaleFactor scale_factor) const { | |
| 56 const chromeos::User* user = chromeos::UserManager::Get()->FindUser(email); | |
| 57 if (user) { | |
| 58 if (user->has_animated_image() && is_image_animated) { | |
| 59 return new base::RefCountedBytes(user->animated_image()); | |
| 60 } else if (user->has_raw_image()) { | |
| 61 return new base::RefCountedBytes(user->raw_image()); | |
| 62 } else if (user->image_is_stub()) { | |
| 63 return ResourceBundle::GetSharedInstance(). | |
| 64 LoadDataResourceBytes(IDR_PROFILE_PICTURE_LOADING, scale_factor); | |
| 65 } else if (user->HasDefaultImage()) { | |
| 66 return ResourceBundle::GetSharedInstance(). | |
| 67 LoadDataResourceBytes(kDefaultImageResourceIDs[user->image_index()], | |
| 68 scale_factor); | |
| 69 } else { | |
| 70 NOTREACHED() << "User with custom image missing raw data"; | |
| 71 } | |
| 72 } | |
| 73 return ResourceBundle::GetSharedInstance(). | |
| 74 LoadDataResourceBytes(IDR_LOGIN_DEFAULT_USER, scale_factor); | |
| 75 } | |
| 76 | |
| 77 UserImageSource::UserImageSource() | |
| 78 : DataSource(chrome::kChromeUIUserImageHost, MessageLoop::current()) { | |
| 79 } | |
| 80 | |
| 81 UserImageSource::~UserImageSource() {} | |
| 82 | |
| 83 void UserImageSource::StartDataRequest(const std::string& path, | |
| 84 bool is_incognito, | |
| 85 int request_id) { | |
| 86 std::string email; | |
| 87 bool is_image_animated = false; | |
| 88 ui::ScaleFactor scale_factor; | |
| 89 GURL url(chrome::kChromeUIUserImageURL + path); | |
| 90 ParseRequest(url, &email, &is_image_animated, &scale_factor); | |
| 91 SendResponse(request_id, | |
| 92 GetUserImage(email, is_image_animated, scale_factor)); | |
| 93 } | |
| 94 | |
| 95 std::string UserImageSource::GetMimeType(const std::string& path) const { | |
| 96 // We need to explicitly return a mime type, otherwise if the user tries to | |
| 97 // drag the image they get no extension. | |
| 98 std::string email; | |
| 99 bool is_image_animated = false; | |
| 100 ui::ScaleFactor scale_factor; | |
| 101 | |
| 102 GURL url(chrome::kChromeUIUserImageURL + path); | |
| 103 ParseRequest(url, &email, &is_image_animated, &scale_factor); | |
| 104 | |
| 105 if (is_image_animated) { | |
| 106 const chromeos::User* user = chromeos::UserManager::Get()->FindUser(email); | |
| 107 if (user && user->has_animated_image()) | |
| 108 return "image/gif"; | |
| 109 } | |
| 110 return "image/png"; | |
| 111 } | |
| 112 | |
| 113 } // namespace options | |
| 114 } // namespace chromeos | |
| OLD | NEW |