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" |
9 #include "chrome/browser/chromeos/login/user_manager.h" | 10 #include "chrome/browser/chromeos/login/user_manager.h" |
10 #include "chrome/common/url_constants.h" | 11 #include "chrome/common/url_constants.h" |
| 12 #include "googleurl/src/url_parse.h" |
11 #include "grit/theme_resources.h" | 13 #include "grit/theme_resources.h" |
12 #include "ui/base/resource/resource_bundle.h" | 14 #include "ui/base/resource/resource_bundle.h" |
13 #include "ui/gfx/codec/png_codec.h" | 15 #include "ui/gfx/codec/png_codec.h" |
14 | 16 |
| 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. Doesn't change |
| 30 // arguments if email can't be parsed (for instance, in guest mode). |
| 31 void ParseRequest(const std::string& path, |
| 32 std::string* email, |
| 33 bool* is_image_animated) { |
| 34 url_parse::Parsed parsed; |
| 35 url_parse::ParseStandardURL(path.c_str(), path.size(), &parsed); |
| 36 if (!parsed.username.is_valid() || !parsed.host.is_valid()) |
| 37 return; |
| 38 |
| 39 DCHECK(email != NULL); |
| 40 *email = path.substr(parsed.username.begin, parsed.username.len); |
| 41 email->append("@"); |
| 42 email->append(path.substr(parsed.host.begin, parsed.host.len)); |
| 43 |
| 44 if (!parsed.query.is_valid()) |
| 45 return; |
| 46 |
| 47 url_parse::Component query = parsed.query; |
| 48 url_parse::Component key, value; |
| 49 DCHECK(is_image_animated != NULL); |
| 50 *is_image_animated = false; |
| 51 while (ExtractQueryKeyValue(path.c_str(), &query, &key, &value)) { |
| 52 if (path.substr(key.begin, key.len) == kKeyAnimated) { |
| 53 *is_image_animated = true; |
| 54 break; |
| 55 } |
| 56 } |
| 57 } |
| 58 |
| 59 } // namespace |
| 60 |
15 namespace chromeos { | 61 namespace chromeos { |
16 namespace options2 { | 62 namespace options2 { |
17 | 63 |
18 std::vector<unsigned char> UserImageSource::GetUserImage( | 64 std::vector<unsigned char> UserImageSource::GetUserImage( |
19 const std::string& email) const { | 65 const std::string& email, bool is_image_animated) const { |
20 std::vector<unsigned char> user_image; | 66 std::vector<unsigned char> user_image; |
21 const chromeos::User* user = chromeos::UserManager::Get()->FindUser(email); | 67 const chromeos::User* user = chromeos::UserManager::Get()->FindUser(email); |
22 if (user) { | 68 if (user) { |
23 gfx::PNGCodec::EncodeBGRASkBitmap(user->image(), false, &user_image); | 69 if (user->has_animated_image() && is_image_animated) |
| 70 user->GetAnimatedImage(&user_image); |
| 71 else |
| 72 gfx::PNGCodec::EncodeBGRASkBitmap(user->image(), false, &user_image); |
24 return user_image; | 73 return user_image; |
25 } | 74 } |
26 gfx::PNGCodec::EncodeBGRASkBitmap( | 75 gfx::PNGCodec::EncodeBGRASkBitmap( |
27 *ResourceBundle::GetSharedInstance().GetImageSkiaNamed( | 76 *ResourceBundle::GetSharedInstance().GetImageSkiaNamed( |
28 IDR_LOGIN_DEFAULT_USER), | 77 IDR_LOGIN_DEFAULT_USER), |
29 false, | 78 false, |
30 &user_image); | 79 &user_image); |
31 return user_image; | 80 return user_image; |
32 } | 81 } |
33 | 82 |
34 UserImageSource::UserImageSource() | 83 UserImageSource::UserImageSource() |
35 : DataSource(chrome::kChromeUIUserImageHost, MessageLoop::current()) { | 84 : DataSource(chrome::kChromeUIUserImageHost, MessageLoop::current()) { |
36 } | 85 } |
37 | 86 |
38 UserImageSource::~UserImageSource() {} | 87 UserImageSource::~UserImageSource() {} |
39 | 88 |
40 void UserImageSource::StartDataRequest(const std::string& path, | 89 void UserImageSource::StartDataRequest(const std::string& path, |
41 bool is_incognito, | 90 bool is_incognito, |
42 int request_id) { | 91 int request_id) { |
43 // Strip the query param value - we only use it as a hack to ensure our | 92 std::string email; |
44 // image gets reloaded instead of being pulled from the browser cache | 93 bool is_image_animated = false; |
45 std::string email = path.substr(0, path.find_first_of("?")); | 94 ParseRequest(path, &email, &is_image_animated); |
46 SendResponse(request_id, new base::RefCountedBytes(GetUserImage(email))); | 95 |
| 96 std::vector<unsigned char> image = GetUserImage(email, is_image_animated); |
| 97 SendResponse(request_id, new base::RefCountedBytes(image)); |
47 } | 98 } |
48 | 99 |
49 std::string UserImageSource::GetMimeType(const std::string&) const { | 100 std::string UserImageSource::GetMimeType(const std::string& path) const { |
50 // We need to explicitly return a mime type, otherwise if the user tries to | 101 // We need to explicitly return a mime type, otherwise if the user tries to |
51 // drag the image they get no extension. | 102 // drag the image they get no extension. |
| 103 std::string email; |
| 104 bool is_image_animated = false; |
| 105 ParseRequest(path, &email, &is_image_animated); |
| 106 |
| 107 if (is_image_animated) { |
| 108 const chromeos::User* user = chromeos::UserManager::Get()->FindUser(email); |
| 109 if (user && user->has_animated_image()) |
| 110 return "image/gif"; |
| 111 } |
52 return "image/png"; | 112 return "image/png"; |
53 } | 113 } |
54 | 114 |
55 } // namespace options2 | 115 } // namespace options2 |
56 } // namespace chromeos | 116 } // namespace chromeos |
OLD | NEW |