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 <utility> | |
8 #include <vector> | |
9 | |
10 #include "base/logging.h" | |
7 #include "base/memory/ref_counted_memory.h" | 11 #include "base/memory/ref_counted_memory.h" |
8 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
13 #include "base/string_split.h" | |
9 #include "chrome/browser/chromeos/login/user_manager.h" | 14 #include "chrome/browser/chromeos/login/user_manager.h" |
10 #include "chrome/common/url_constants.h" | 15 #include "chrome/common/url_constants.h" |
11 #include "grit/theme_resources.h" | 16 #include "grit/theme_resources.h" |
12 #include "ui/base/resource/resource_bundle.h" | 17 #include "ui/base/resource/resource_bundle.h" |
13 #include "ui/gfx/codec/png_codec.h" | 18 #include "ui/gfx/codec/png_codec.h" |
14 | 19 |
20 using std::pair; | |
21 using std::string; | |
22 using std::vector; | |
23 | |
24 namespace { | |
25 | |
26 const char* const kPathKeyAnimated = "animated"; | |
27 const char* const kPathKeyAnimatedTrueValue = "true"; | |
28 | |
29 void ParseRequest(const string& path, string* email, bool* is_image_animated) { | |
Ivan Korotkov
2012/05/29 10:50:23
Please use GURL and something like GetValueForKeyI
ygorshenin1
2012/05/30 12:17:34
Done.
| |
30 // Strip the query param value - we only use it as a hack to ensure our | |
31 // image gets reloaded instead of being pulled from the browser cache | |
32 size_t split_pos = path.find_first_of('?'); | |
33 | |
34 if (email) | |
35 *email = path.substr(0, split_pos); | |
36 | |
37 if (is_image_animated) { | |
38 string params = path.substr(split_pos + 1, path.size() - split_pos - 1); | |
39 vector<pair<string, string> > key_value_pairs; | |
40 base::SplitStringIntoKeyValuePairs(params, '=', '&', &key_value_pairs); | |
41 *is_image_animated = false; | |
42 for (size_t i = 0; i < key_value_pairs.size(); ++i) { | |
43 if (key_value_pairs[i].first == kPathKeyAnimated && | |
44 key_value_pairs[i].second == kPathKeyAnimatedTrueValue) { | |
45 *is_image_animated = true; | |
46 } | |
47 } | |
48 } | |
49 } | |
50 | |
51 } // namespace | |
52 | |
15 namespace chromeos { | 53 namespace chromeos { |
16 namespace options2 { | 54 namespace options2 { |
17 | 55 |
18 std::vector<unsigned char> UserImageSource::GetUserImage( | 56 vector<unsigned char> UserImageSource::GetUserImage( |
19 const std::string& email) const { | 57 const string& email, bool is_image_animated) const { |
20 std::vector<unsigned char> user_image; | 58 vector<unsigned char> user_image; |
21 const chromeos::User* user = chromeos::UserManager::Get()->FindUser(email); | 59 const chromeos::User* user = chromeos::UserManager::Get()->FindUser(email); |
22 if (user) { | 60 if (user) { |
23 gfx::PNGCodec::EncodeBGRASkBitmap(user->image(), false, &user_image); | 61 if (user->has_gif_image() && is_image_animated) |
62 user->gif_image(&user_image); | |
63 else | |
64 gfx::PNGCodec::EncodeBGRASkBitmap(user->image(), false, &user_image); | |
24 return user_image; | 65 return user_image; |
25 } | 66 } |
26 gfx::PNGCodec::EncodeBGRASkBitmap( | 67 gfx::PNGCodec::EncodeBGRASkBitmap( |
27 *ResourceBundle::GetSharedInstance().GetBitmapNamed( | 68 *ResourceBundle::GetSharedInstance().GetBitmapNamed( |
28 IDR_LOGIN_DEFAULT_USER), | 69 IDR_LOGIN_DEFAULT_USER), |
29 false, | 70 false, |
30 &user_image); | 71 &user_image); |
31 return user_image; | 72 return user_image; |
32 } | 73 } |
33 | 74 |
34 UserImageSource::UserImageSource() | 75 UserImageSource::UserImageSource() |
35 : DataSource(chrome::kChromeUIUserImageHost, MessageLoop::current()) { | 76 : DataSource(chrome::kChromeUIUserImageHost, MessageLoop::current()) { |
36 } | 77 } |
37 | 78 |
38 UserImageSource::~UserImageSource() {} | 79 UserImageSource::~UserImageSource() {} |
39 | 80 |
40 void UserImageSource::StartDataRequest(const std::string& path, | 81 void UserImageSource::StartDataRequest(const string& path, |
41 bool is_incognito, | 82 bool is_incognito, |
42 int request_id) { | 83 int request_id) { |
43 // Strip the query param value - we only use it as a hack to ensure our | 84 string email; |
44 // image gets reloaded instead of being pulled from the browser cache | 85 bool is_image_animated; |
45 std::string email = path.substr(0, path.find_first_of("?")); | 86 ParseRequest(path, &email, &is_image_animated); |
46 SendResponse(request_id, new base::RefCountedBytes(GetUserImage(email))); | 87 |
88 vector<unsigned char> image = GetUserImage(email, is_image_animated); | |
89 SendResponse(request_id, new base::RefCountedBytes(image)); | |
47 } | 90 } |
48 | 91 |
49 std::string UserImageSource::GetMimeType(const std::string&) const { | 92 string UserImageSource::GetMimeType(const string& path) const { |
50 // We need to explicitly return a mime type, otherwise if the user tries to | 93 // We need to explicitly return a mime type, otherwise if the user tries to |
51 // drag the image they get no extension. | 94 // drag the image they get no extension. |
95 string email; | |
96 bool is_image_animated; | |
97 ParseRequest(path, &email, &is_image_animated); | |
98 | |
99 const chromeos::User* user = chromeos::UserManager::Get()->FindUser(email); | |
100 if (user && user->has_gif_image() && is_image_animated) | |
101 return "image/gif"; | |
52 return "image/png"; | 102 return "image/png"; |
53 } | 103 } |
54 | 104 |
55 } // namespace options2 | 105 } // namespace options2 |
56 } // namespace chromeos | 106 } // namespace chromeos |
OLD | NEW |