Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / chrome / browser / ui / webui / chromeos / image_source.cc
blobd45c0c9c5865a7d26eee475f5cb9f50a266316cc
1 // Copyright 2014 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.
5 #include "chrome/browser/ui/webui/chromeos/image_source.h"
7 #include "base/bind.h"
8 #include "base/files/file_util.h"
9 #include "base/location.h"
10 #include "base/memory/ref_counted_memory.h"
11 #include "base/sequenced_task_runner.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/thread_task_runner_handle.h"
14 #include "chrome/browser/chromeos/login/users/avatar/user_image_loader.h"
15 #include "chrome/common/url_constants.h"
16 #include "components/user_manager/user_image/user_image.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "net/base/mime_util.h"
20 using content::BrowserThread;
22 namespace chromeos {
23 namespace {
25 const char* kWhitelistedFiles[] = {
26 "fcc/label.png"
29 // Callback for user_manager::UserImageLoader.
30 void ImageLoaded(
31 const content::URLDataSource::GotDataCallback& got_data_callback,
32 const user_manager::UserImage& user_image) {
33 DCHECK_CURRENTLY_ON(BrowserThread::UI);
35 if (user_image.has_raw_image())
36 got_data_callback.Run(new base::RefCountedBytes(user_image.raw_image()));
37 else
38 got_data_callback.Run(NULL);
41 // Looks for the image at |path| under the shared assets directory.
42 void StartOnBlockingPool(
43 const std::string& path,
44 scoped_refptr<UserImageLoader> image_loader,
45 const content::URLDataSource::GotDataCallback& got_data_callback,
46 scoped_refptr<base::SingleThreadTaskRunner> thread_task_runner) {
47 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
49 const base::FilePath asset_dir(FILE_PATH_LITERAL(chrome::kChromeOSAssetPath));
50 const base::FilePath image_path = asset_dir.AppendASCII(path);
51 if (base::PathExists(image_path)) {
52 image_loader->Start(image_path.value(), 0,
53 base::Bind(&ImageLoaded, got_data_callback));
54 } else {
55 thread_task_runner->PostTask(FROM_HERE,
56 base::Bind(got_data_callback, nullptr));
60 } // namespace
62 ImageSource::ImageSource() : weak_factory_(this) {
63 base::SequencedWorkerPool* blocking_pool =
64 BrowserThread::GetBlockingPool();
65 task_runner_ = blocking_pool->GetSequencedTaskRunnerWithShutdownBehavior(
66 blocking_pool->GetSequenceToken(),
67 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN);
70 ImageSource::~ImageSource() {
73 std::string ImageSource::GetSource() const {
74 return chrome::kChromeOSAssetHost;
77 void ImageSource::StartDataRequest(
78 const std::string& path,
79 int render_process_id,
80 int render_frame_id,
81 const content::URLDataSource::GotDataCallback& got_data_callback) {
82 if (!IsWhitelisted(path)) {
83 got_data_callback.Run(NULL);
84 return;
87 if (!image_loader_) {
88 image_loader_ = new UserImageLoader(ImageDecoder::DEFAULT_CODEC,
89 task_runner_);
92 content::BrowserThread::GetBlockingPool()->PostTask(
93 FROM_HERE,
94 base::Bind(&StartOnBlockingPool,
95 path,
96 image_loader_,
97 got_data_callback,
98 base::ThreadTaskRunnerHandle::Get()));
101 std::string ImageSource::GetMimeType(const std::string& path) const {
102 std::string mime_type;
103 std::string ext = base::FilePath(path).Extension();
104 if (!ext.empty())
105 net::GetWellKnownMimeTypeFromExtension(ext.substr(1), &mime_type);
106 return mime_type;
109 bool ImageSource::IsWhitelisted(const std::string& path) const {
110 const char** end = kWhitelistedFiles + arraysize(kWhitelistedFiles);
111 return std::find(kWhitelistedFiles, end, path) != end;
114 } // namespace chromeos