| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/webui/options2/chromeos/user_image_source2.h" | 5 #include "chrome/browser/ui/webui/options2/chromeos/user_image_source2.h" |
| 6 | 6 |
| 7 #include "base/memory/ref_counted_memory.h" | 7 #include "base/memory/ref_counted_memory.h" |
| 8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "base/string_split.h" | |
| 10 #include "chrome/browser/chromeos/login/user_manager.h" | 9 #include "chrome/browser/chromeos/login/user_manager.h" |
| 11 #include "chrome/common/url_constants.h" | 10 #include "chrome/common/url_constants.h" |
| 12 #include "googleurl/src/url_parse.h" | |
| 13 #include "grit/theme_resources.h" | 11 #include "grit/theme_resources.h" |
| 14 #include "ui/base/resource/resource_bundle.h" | 12 #include "ui/base/resource/resource_bundle.h" |
| 15 #include "ui/gfx/codec/png_codec.h" | 13 #include "ui/gfx/codec/png_codec.h" |
| 16 | 14 |
| 17 namespace { | |
| 18 | |
| 19 // Animated key is used in user image URL requests to specify that | |
| 20 // animated version of user image is required. Without that key | |
| 21 // non-animated version of user image should be returned. | |
| 22 const char kKeyAnimated[] = "animated"; | |
| 23 | |
| 24 // Extracts from user image request user email and type of requested | |
| 25 // image (animated or non-animated). |path| is an user image request | |
| 26 // and should look like "username@host?key1=value1&...&key_n=value_n". | |
| 27 // So, "username@host" is stored into |email|. If a query part of | |
| 28 // |path| contains "animated" key, |is_image_animated| is set to true, | |
| 29 // otherwise |is_image_animated| is set to false. | |
| 30 void ParseRequest(const std::string& path, | |
| 31 std::string* email, | |
| 32 bool* is_image_animated) { | |
| 33 url_parse::Parsed parsed; | |
| 34 url_parse::ParseStandardURL(path.c_str(), path.size(), &parsed); | |
| 35 DCHECK(parsed.username.is_valid()); | |
| 36 DCHECK(parsed.host.is_valid()); | |
| 37 | |
| 38 DCHECK(email != NULL); | |
| 39 *email = path.substr(parsed.username.begin, parsed.username.len); | |
| 40 email->append("@"); | |
| 41 email->append(path.substr(parsed.host.begin, parsed.host.len)); | |
| 42 | |
| 43 if (parsed.query.is_valid()) { | |
| 44 url_parse::Component query = parsed.query; | |
| 45 url_parse::Component key, value; | |
| 46 | |
| 47 DCHECK(is_image_animated != NULL); | |
| 48 *is_image_animated = false; | |
| 49 while (ExtractQueryKeyValue(path.c_str(), &query, &key, &value)) { | |
| 50 if (path.substr(key.begin, key.len) == kKeyAnimated) { | |
| 51 *is_image_animated = true; | |
| 52 break; | |
| 53 } | |
| 54 } | |
| 55 } | |
| 56 } | |
| 57 | |
| 58 } // namespace | |
| 59 | |
| 60 namespace chromeos { | 15 namespace chromeos { |
| 61 namespace options2 { | 16 namespace options2 { |
| 62 | 17 |
| 63 std::vector<unsigned char> UserImageSource::GetUserImage( | 18 std::vector<unsigned char> UserImageSource::GetUserImage( |
| 64 const std::string& email, bool is_image_animated) const { | 19 const std::string& email) const { |
| 65 std::vector<unsigned char> user_image; | 20 std::vector<unsigned char> user_image; |
| 66 const chromeos::User* user = chromeos::UserManager::Get()->FindUser(email); | 21 const chromeos::User* user = chromeos::UserManager::Get()->FindUser(email); |
| 67 if (user) { | 22 if (user) { |
| 68 if (user->has_animated_image() && is_image_animated) | 23 gfx::PNGCodec::EncodeBGRASkBitmap(user->image(), false, &user_image); |
| 69 user->GetAnimatedImage(&user_image); | |
| 70 else | |
| 71 gfx::PNGCodec::EncodeBGRASkBitmap(user->image(), false, &user_image); | |
| 72 return user_image; | 24 return user_image; |
| 73 } | 25 } |
| 74 gfx::PNGCodec::EncodeBGRASkBitmap( | 26 gfx::PNGCodec::EncodeBGRASkBitmap( |
| 75 *ResourceBundle::GetSharedInstance().GetImageSkiaNamed( | 27 *ResourceBundle::GetSharedInstance().GetImageSkiaNamed( |
| 76 IDR_LOGIN_DEFAULT_USER), | 28 IDR_LOGIN_DEFAULT_USER), |
| 77 false, | 29 false, |
| 78 &user_image); | 30 &user_image); |
| 79 return user_image; | 31 return user_image; |
| 80 } | 32 } |
| 81 | 33 |
| 82 UserImageSource::UserImageSource() | 34 UserImageSource::UserImageSource() |
| 83 : DataSource(chrome::kChromeUIUserImageHost, MessageLoop::current()) { | 35 : DataSource(chrome::kChromeUIUserImageHost, MessageLoop::current()) { |
| 84 } | 36 } |
| 85 | 37 |
| 86 UserImageSource::~UserImageSource() {} | 38 UserImageSource::~UserImageSource() {} |
| 87 | 39 |
| 88 void UserImageSource::StartDataRequest(const std::string& path, | 40 void UserImageSource::StartDataRequest(const std::string& path, |
| 89 bool is_incognito, | 41 bool is_incognito, |
| 90 int request_id) { | 42 int request_id) { |
| 91 std::string email; | 43 // Strip the query param value - we only use it as a hack to ensure our |
| 92 bool is_image_animated; | 44 // image gets reloaded instead of being pulled from the browser cache |
| 93 ParseRequest(path, &email, &is_image_animated); | 45 std::string email = path.substr(0, path.find_first_of("?")); |
| 94 | 46 SendResponse(request_id, new base::RefCountedBytes(GetUserImage(email))); |
| 95 std::vector<unsigned char> image = GetUserImage(email, is_image_animated); | |
| 96 SendResponse(request_id, new base::RefCountedBytes(image)); | |
| 97 } | 47 } |
| 98 | 48 |
| 99 std::string UserImageSource::GetMimeType(const std::string& path) const { | 49 std::string UserImageSource::GetMimeType(const std::string&) const { |
| 100 // We need to explicitly return a mime type, otherwise if the user tries to | 50 // We need to explicitly return a mime type, otherwise if the user tries to |
| 101 // drag the image they get no extension. | 51 // drag the image they get no extension. |
| 102 std::string email; | |
| 103 bool is_image_animated; | |
| 104 ParseRequest(path, &email, &is_image_animated); | |
| 105 | |
| 106 if (is_image_animated) { | |
| 107 const chromeos::User* user = chromeos::UserManager::Get()->FindUser(email); | |
| 108 if (user && user->has_animated_image()) | |
| 109 return "image/gif"; | |
| 110 } | |
| 111 return "image/png"; | 52 return "image/png"; |
| 112 } | 53 } |
| 113 | 54 |
| 114 } // namespace options2 | 55 } // namespace options2 |
| 115 } // namespace chromeos | 56 } // namespace chromeos |
| OLD | NEW |