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 | |
37 if (parsed.username.is_valid() && parsed.host.is_valid()) { | |
James Hawkins
2012/06/04 15:50:13
Optional nit: Save indentation (and lines of code)
ygorshenin1
2012/06/09 09:34:26
Done.
| |
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 } else { | |
43 return; | |
44 } | |
45 | |
46 if (parsed.query.is_valid()) { | |
James Hawkins
2012/06/04 15:50:13
Optional nit: Same thing here about saving indenta
ygorshenin1
2012/06/09 09:34:26
Done.
| |
47 url_parse::Component query = parsed.query; | |
48 url_parse::Component key, value; | |
49 | |
50 DCHECK(is_image_animated != NULL); | |
51 *is_image_animated = false; | |
52 while (ExtractQueryKeyValue(path.c_str(), &query, &key, &value)) { | |
53 if (path.substr(key.begin, key.len) == kKeyAnimated) { | |
54 *is_image_animated = true; | |
55 break; | |
56 } | |
57 } | |
58 } | |
59 } | |
60 | |
61 } // namespace | |
62 | |
15 namespace chromeos { | 63 namespace chromeos { |
16 namespace options2 { | 64 namespace options2 { |
17 | 65 |
18 std::vector<unsigned char> UserImageSource::GetUserImage( | 66 std::vector<unsigned char> UserImageSource::GetUserImage( |
19 const std::string& email) const { | 67 const std::string& email, bool is_image_animated) const { |
20 std::vector<unsigned char> user_image; | 68 std::vector<unsigned char> user_image; |
21 const chromeos::User* user = chromeos::UserManager::Get()->FindUser(email); | 69 const chromeos::User* user = chromeos::UserManager::Get()->FindUser(email); |
22 if (user) { | 70 if (user) { |
23 gfx::PNGCodec::EncodeBGRASkBitmap(user->image(), false, &user_image); | 71 if (user->has_animated_image() && is_image_animated) |
72 user->GetAnimatedImage(&user_image); | |
73 else | |
74 gfx::PNGCodec::EncodeBGRASkBitmap(user->image(), false, &user_image); | |
24 return user_image; | 75 return user_image; |
25 } | 76 } |
26 gfx::PNGCodec::EncodeBGRASkBitmap( | 77 gfx::PNGCodec::EncodeBGRASkBitmap( |
27 *ResourceBundle::GetSharedInstance().GetImageSkiaNamed( | 78 *ResourceBundle::GetSharedInstance().GetImageSkiaNamed( |
28 IDR_LOGIN_DEFAULT_USER), | 79 IDR_LOGIN_DEFAULT_USER), |
29 false, | 80 false, |
30 &user_image); | 81 &user_image); |
31 return user_image; | 82 return user_image; |
32 } | 83 } |
33 | 84 |
34 UserImageSource::UserImageSource() | 85 UserImageSource::UserImageSource() |
35 : DataSource(chrome::kChromeUIUserImageHost, MessageLoop::current()) { | 86 : DataSource(chrome::kChromeUIUserImageHost, MessageLoop::current()) { |
36 } | 87 } |
37 | 88 |
38 UserImageSource::~UserImageSource() {} | 89 UserImageSource::~UserImageSource() {} |
39 | 90 |
40 void UserImageSource::StartDataRequest(const std::string& path, | 91 void UserImageSource::StartDataRequest(const std::string& path, |
41 bool is_incognito, | 92 bool is_incognito, |
42 int request_id) { | 93 int request_id) { |
43 // Strip the query param value - we only use it as a hack to ensure our | 94 std::string email; |
44 // image gets reloaded instead of being pulled from the browser cache | 95 bool is_image_animated = false; |
45 std::string email = path.substr(0, path.find_first_of("?")); | 96 ParseRequest(path, &email, &is_image_animated); |
46 SendResponse(request_id, new base::RefCountedBytes(GetUserImage(email))); | 97 |
98 std::vector<unsigned char> image = GetUserImage(email, is_image_animated); | |
99 SendResponse(request_id, new base::RefCountedBytes(image)); | |
47 } | 100 } |
48 | 101 |
49 std::string UserImageSource::GetMimeType(const std::string&) const { | 102 std::string UserImageSource::GetMimeType(const std::string& path) const { |
50 // We need to explicitly return a mime type, otherwise if the user tries to | 103 // We need to explicitly return a mime type, otherwise if the user tries to |
51 // drag the image they get no extension. | 104 // drag the image they get no extension. |
105 std::string email; | |
106 bool is_image_animated = false; | |
107 ParseRequest(path, &email, &is_image_animated); | |
108 | |
109 if (is_image_animated) { | |
110 const chromeos::User* user = chromeos::UserManager::Get()->FindUser(email); | |
111 if (user && user->has_animated_image()) | |
112 return "image/gif"; | |
113 } | |
52 return "image/png"; | 114 return "image/png"; |
53 } | 115 } |
54 | 116 |
55 } // namespace options2 | 117 } // namespace options2 |
56 } // namespace chromeos | 118 } // namespace chromeos |
OLD | NEW |