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/options/chromeos/user_image_source.h" | 5 #include "chrome/browser/ui/webui/options/chromeos/user_image_source.h" |
6 | 6 |
7 #include "base/memory/ref_counted_memory.h" | 7 #include "base/memory/ref_counted_memory.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "base/strings/string_split.h" | 9 #include "base/strings/string_split.h" |
10 #include "chrome/browser/chromeos/login/users/user_manager.h" | 10 #include "chrome/browser/chromeos/login/users/user_manager.h" |
11 #include "chrome/common/url_constants.h" | 11 #include "chrome/common/url_constants.h" |
12 #include "components/user_manager/user_image/default_user_images.h" | 12 #include "components/user_manager/user_image/default_user_images.h" |
13 #include "grit/theme_resources.h" | 13 #include "grit/theme_resources.h" |
14 #include "grit/ui_chromeos_resources.h" | 14 #include "grit/ui_chromeos_resources.h" |
15 #include "net/base/escape.h" | 15 #include "net/base/escape.h" |
16 #include "ui/base/resource/resource_bundle.h" | 16 #include "ui/base/resource/resource_bundle.h" |
17 #include "ui/gfx/codec/png_codec.h" | 17 #include "ui/gfx/codec/png_codec.h" |
18 #include "url/url_parse.h" | 18 #include "url/url_parse.h" |
19 | 19 |
20 namespace { | 20 namespace { |
21 | 21 |
22 // Animated key is used in user image URL requests to specify that | |
23 // animated version of user image is required. Without that key | |
24 // non-animated version of user image should be returned. | |
25 const char kKeyAnimated[] = "animated"; | |
26 | |
27 // Parses the user image URL, which looks like | 22 // Parses the user image URL, which looks like |
28 // "chrome://userimage/user@host?key1=value1&...&key_n=value_n", | 23 // "chrome://userimage/user@host?key1=value1&...&key_n=value_n", |
29 // to user email and optional parameters. | 24 // to user email. |
30 void ParseRequest(const GURL& url, | 25 void ParseRequest(const GURL& url, |
31 std::string* email, | 26 std::string* email) { |
32 bool* is_image_animated) { | |
33 DCHECK(url.is_valid()); | 27 DCHECK(url.is_valid()); |
34 *email = net::UnescapeURLComponent(url.path().substr(1), | 28 *email = net::UnescapeURLComponent(url.path().substr(1), |
35 (net::UnescapeRule::URL_SPECIAL_CHARS | | 29 (net::UnescapeRule::URL_SPECIAL_CHARS | |
36 net::UnescapeRule::SPACES)); | 30 net::UnescapeRule::SPACES)); |
37 std::string url_spec = url.possibly_invalid_spec(); | |
38 url::Component query = url.parsed_for_possibly_invalid_spec().query; | |
39 url::Component key, value; | |
40 *is_image_animated = false; | |
41 while (ExtractQueryKeyValue(url_spec.c_str(), &query, &key, &value)) { | |
42 if (url_spec.substr(key.begin, key.len) == kKeyAnimated) { | |
43 *is_image_animated = true; | |
44 break; | |
45 } | |
46 } | |
47 } | 31 } |
48 | 32 |
49 } // namespace | 33 } // namespace |
50 | 34 |
51 namespace chromeos { | 35 namespace chromeos { |
52 namespace options { | 36 namespace options { |
53 | 37 |
54 base::RefCountedMemory* UserImageSource::GetUserImage( | 38 base::RefCountedMemory* UserImageSource::GetUserImage( |
55 const std::string& email, | 39 const std::string& email, |
56 bool is_image_animated, | |
57 ui::ScaleFactor scale_factor) const { | 40 ui::ScaleFactor scale_factor) const { |
58 const user_manager::User* user = | 41 const user_manager::User* user = |
59 chromeos::UserManager::Get()->FindUser(email); | 42 chromeos::UserManager::Get()->FindUser(email); |
60 if (user) { | 43 if (user) { |
61 if (user->has_animated_image() && is_image_animated) { | 44 if (user->has_raw_image()) { |
62 return new base::RefCountedBytes(user->animated_image()); | |
63 } else if (user->has_raw_image()) { | |
64 return new base::RefCountedBytes(user->raw_image()); | 45 return new base::RefCountedBytes(user->raw_image()); |
65 } else if (user->image_is_stub()) { | 46 } else if (user->image_is_stub()) { |
66 return ResourceBundle::GetSharedInstance(). | 47 return ResourceBundle::GetSharedInstance(). |
67 LoadDataResourceBytesForScale(IDR_PROFILE_PICTURE_LOADING, | 48 LoadDataResourceBytesForScale(IDR_PROFILE_PICTURE_LOADING, |
68 scale_factor); | 49 scale_factor); |
69 } else if (user->HasDefaultImage()) { | 50 } else if (user->HasDefaultImage()) { |
70 return ResourceBundle::GetSharedInstance(). | 51 return ResourceBundle::GetSharedInstance(). |
71 LoadDataResourceBytesForScale( | 52 LoadDataResourceBytesForScale( |
72 user_manager::kDefaultImageResourceIDs[user->image_index()], | 53 user_manager::kDefaultImageResourceIDs[user->image_index()], |
73 scale_factor); | 54 scale_factor); |
(...skipping 13 matching lines...) Expand all Loading... |
87 std::string UserImageSource::GetSource() const { | 68 std::string UserImageSource::GetSource() const { |
88 return chrome::kChromeUIUserImageHost; | 69 return chrome::kChromeUIUserImageHost; |
89 } | 70 } |
90 | 71 |
91 void UserImageSource::StartDataRequest( | 72 void UserImageSource::StartDataRequest( |
92 const std::string& path, | 73 const std::string& path, |
93 int render_process_id, | 74 int render_process_id, |
94 int render_frame_id, | 75 int render_frame_id, |
95 const content::URLDataSource::GotDataCallback& callback) { | 76 const content::URLDataSource::GotDataCallback& callback) { |
96 std::string email; | 77 std::string email; |
97 bool is_image_animated = false; | |
98 GURL url(chrome::kChromeUIUserImageURL + path); | 78 GURL url(chrome::kChromeUIUserImageURL + path); |
99 ParseRequest(url, &email, &is_image_animated); | 79 ParseRequest(url, &email); |
100 callback.Run(GetUserImage(email, is_image_animated, ui::SCALE_FACTOR_100P)); | 80 callback.Run(GetUserImage(email, ui::SCALE_FACTOR_100P)); |
101 } | 81 } |
102 | 82 |
103 std::string UserImageSource::GetMimeType(const std::string& path) const { | 83 std::string UserImageSource::GetMimeType(const std::string& path) const { |
104 // We need to explicitly return a mime type, otherwise if the user tries to | 84 // We need to explicitly return a mime type, otherwise if the user tries to |
105 // drag the image they get no extension. | 85 // drag the image they get no extension. |
106 std::string email; | |
107 bool is_image_animated = false; | |
108 | |
109 GURL url(chrome::kChromeUIUserImageURL + path); | |
110 ParseRequest(url, &email, &is_image_animated); | |
111 | |
112 if (is_image_animated) { | |
113 const user_manager::User* user = | |
114 chromeos::UserManager::Get()->FindUser(email); | |
115 if (user && user->has_animated_image()) | |
116 return "image/gif"; | |
117 } | |
118 return "image/png"; | 86 return "image/png"; |
119 } | 87 } |
120 | 88 |
121 } // namespace options | 89 } // namespace options |
122 } // namespace chromeos | 90 } // namespace chromeos |
OLD | NEW |