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_source2.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/user_manager.h" | |
11 #include "chrome/common/url_constants.h" | |
12 #include "googleurl/src/url_parse.h" | |
13 #include "grit/theme_resources.h" | |
14 #include "ui/base/resource/resource_bundle.h" | |
15 #include "ui/gfx/codec/png_codec.h" | |
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 | |
61 namespace chromeos { | |
62 namespace options2 { | |
63 | |
64 std::vector<unsigned char> UserImageSource::GetUserImage( | |
65 const std::string& email, bool is_image_animated) const { | |
66 const chromeos::User* user = chromeos::UserManager::Get()->FindUser(email); | |
67 if (user) { | |
68 if (user->has_animated_image() && is_image_animated) | |
69 return user->animated_image(); | |
70 else if (user->has_raw_image()) | |
71 return user->raw_image(); | |
72 } | |
73 std::vector<unsigned char> user_image; | |
74 gfx::PNGCodec::EncodeBGRASkBitmap( | |
75 *ResourceBundle::GetSharedInstance().GetImageSkiaNamed( | |
76 IDR_LOGIN_DEFAULT_USER), | |
77 false, | |
78 &user_image); | |
79 return user_image; | |
80 } | |
81 | |
82 UserImageSource::UserImageSource() | |
83 : DataSource(chrome::kChromeUIUserImageHost, MessageLoop::current()) { | |
84 } | |
85 | |
86 UserImageSource::~UserImageSource() {} | |
87 | |
88 void UserImageSource::StartDataRequest(const std::string& path, | |
89 bool is_incognito, | |
90 int request_id) { | |
91 std::string email; | |
92 bool is_image_animated = false; | |
93 ParseRequest(path, &email, &is_image_animated); | |
94 | |
95 std::vector<unsigned char> image = GetUserImage(email, is_image_animated); | |
96 SendResponse(request_id, new base::RefCountedBytes(image)); | |
97 } | |
98 | |
99 std::string UserImageSource::GetMimeType(const std::string& path) const { | |
100 // We need to explicitly return a mime type, otherwise if the user tries to | |
101 // drag the image they get no extension. | |
102 std::string email; | |
103 bool is_image_animated = false; | |
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"; | |
112 } | |
113 | |
114 } // namespace options2 | |
115 } // namespace chromeos | |
OLD | NEW |