| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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/chromeos/image_source.h" | 5 #include "chrome/browser/ui/webui/chromeos/image_source.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
| 9 #include "base/location.h" | 9 #include "base/location.h" |
| 10 #include "base/memory/ref_counted_memory.h" | 10 #include "base/memory/ref_counted_memory.h" |
| 11 #include "base/sequenced_task_runner.h" | 11 #include "base/sequenced_task_runner.h" |
| 12 #include "base/single_thread_task_runner.h" |
| 13 #include "base/thread_task_runner_handle.h" |
| 12 #include "chrome/browser/chromeos/login/users/avatar/user_image_loader.h" | 14 #include "chrome/browser/chromeos/login/users/avatar/user_image_loader.h" |
| 13 #include "chrome/common/url_constants.h" | 15 #include "chrome/common/url_constants.h" |
| 14 #include "components/user_manager/user_image/user_image.h" | 16 #include "components/user_manager/user_image/user_image.h" |
| 15 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 16 #include "net/base/mime_util.h" | 18 #include "net/base/mime_util.h" |
| 17 | 19 |
| 18 using content::BrowserThread; | 20 using content::BrowserThread; |
| 19 | 21 |
| 20 namespace chromeos { | 22 namespace chromeos { |
| 21 namespace { | 23 namespace { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 34 got_data_callback.Run(new base::RefCountedBytes(user_image.raw_image())); | 36 got_data_callback.Run(new base::RefCountedBytes(user_image.raw_image())); |
| 35 else | 37 else |
| 36 got_data_callback.Run(NULL); | 38 got_data_callback.Run(NULL); |
| 37 } | 39 } |
| 38 | 40 |
| 39 // Looks for the image at |path| under the shared assets directory. | 41 // Looks for the image at |path| under the shared assets directory. |
| 40 void StartOnBlockingPool( | 42 void StartOnBlockingPool( |
| 41 const std::string& path, | 43 const std::string& path, |
| 42 scoped_refptr<UserImageLoader> image_loader, | 44 scoped_refptr<UserImageLoader> image_loader, |
| 43 const content::URLDataSource::GotDataCallback& got_data_callback, | 45 const content::URLDataSource::GotDataCallback& got_data_callback, |
| 44 scoped_refptr<base::MessageLoopProxy> message_loop_proxy) { | 46 scoped_refptr<base::SingleThreadTaskRunner> thread_task_runner) { |
| 45 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | 47 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
| 46 | 48 |
| 47 const base::FilePath asset_dir(FILE_PATH_LITERAL(chrome::kChromeOSAssetPath)); | 49 const base::FilePath asset_dir(FILE_PATH_LITERAL(chrome::kChromeOSAssetPath)); |
| 48 const base::FilePath image_path = asset_dir.AppendASCII(path); | 50 const base::FilePath image_path = asset_dir.AppendASCII(path); |
| 49 if (base::PathExists(image_path)) { | 51 if (base::PathExists(image_path)) { |
| 50 image_loader->Start(image_path.value(), 0, | 52 image_loader->Start(image_path.value(), 0, |
| 51 base::Bind(&ImageLoaded, got_data_callback)); | 53 base::Bind(&ImageLoaded, got_data_callback)); |
| 52 } else { | 54 } else { |
| 53 message_loop_proxy->PostTask(FROM_HERE, | 55 thread_task_runner->PostTask(FROM_HERE, |
| 54 base::Bind(got_data_callback, nullptr)); | 56 base::Bind(got_data_callback, nullptr)); |
| 55 } | 57 } |
| 56 } | 58 } |
| 57 | 59 |
| 58 } // namespace | 60 } // namespace |
| 59 | 61 |
| 60 ImageSource::ImageSource() : weak_factory_(this) { | 62 ImageSource::ImageSource() : weak_factory_(this) { |
| 61 base::SequencedWorkerPool* blocking_pool = | 63 base::SequencedWorkerPool* blocking_pool = |
| 62 BrowserThread::GetBlockingPool(); | 64 BrowserThread::GetBlockingPool(); |
| 63 task_runner_ = blocking_pool->GetSequencedTaskRunnerWithShutdownBehavior( | 65 task_runner_ = blocking_pool->GetSequencedTaskRunnerWithShutdownBehavior( |
| (...skipping 22 matching lines...) Expand all Loading... |
| 86 image_loader_ = new UserImageLoader(ImageDecoder::DEFAULT_CODEC, | 88 image_loader_ = new UserImageLoader(ImageDecoder::DEFAULT_CODEC, |
| 87 task_runner_); | 89 task_runner_); |
| 88 } | 90 } |
| 89 | 91 |
| 90 content::BrowserThread::GetBlockingPool()->PostTask( | 92 content::BrowserThread::GetBlockingPool()->PostTask( |
| 91 FROM_HERE, | 93 FROM_HERE, |
| 92 base::Bind(&StartOnBlockingPool, | 94 base::Bind(&StartOnBlockingPool, |
| 93 path, | 95 path, |
| 94 image_loader_, | 96 image_loader_, |
| 95 got_data_callback, | 97 got_data_callback, |
| 96 base::MessageLoopProxy::current())); | 98 base::ThreadTaskRunnerHandle::Get())); |
| 97 } | 99 } |
| 98 | 100 |
| 99 std::string ImageSource::GetMimeType(const std::string& path) const { | 101 std::string ImageSource::GetMimeType(const std::string& path) const { |
| 100 std::string mime_type; | 102 std::string mime_type; |
| 101 std::string ext = base::FilePath(path).Extension(); | 103 std::string ext = base::FilePath(path).Extension(); |
| 102 if (!ext.empty()) | 104 if (!ext.empty()) |
| 103 net::GetWellKnownMimeTypeFromExtension(ext.substr(1), &mime_type); | 105 net::GetWellKnownMimeTypeFromExtension(ext.substr(1), &mime_type); |
| 104 return mime_type; | 106 return mime_type; |
| 105 } | 107 } |
| 106 | 108 |
| 107 bool ImageSource::IsWhitelisted(const std::string& path) const { | 109 bool ImageSource::IsWhitelisted(const std::string& path) const { |
| 108 const char** end = kWhitelistedFiles + arraysize(kWhitelistedFiles); | 110 const char** end = kWhitelistedFiles + arraysize(kWhitelistedFiles); |
| 109 return std::find(kWhitelistedFiles, end, path) != end; | 111 return std::find(kWhitelistedFiles, end, path) != end; |
| 110 } | 112 } |
| 111 | 113 |
| 112 } // namespace chromeos | 114 } // namespace chromeos |
| OLD | NEW |