Chromium Code Reviews| 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/path_service.h" | |
| 11 #include "base/sequenced_task_runner.h" | 12 #include "base/sequenced_task_runner.h" |
| 12 #include "chrome/browser/chromeos/login/users/avatar/user_image_loader.h" | 13 #include "chrome/browser/chromeos/login/users/avatar/user_image_loader.h" |
| 13 #include "chrome/common/url_constants.h" | 14 #include "chrome/common/url_constants.h" |
| 15 #include "chromeos/chromeos_paths.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 { |
| 22 | 24 |
| 23 const char* kWhitelistedFiles[] = { | 25 const char* kWhitelistedFiles[] = { |
| 24 "fcc/label.png" | 26 "fcc/label.png" |
| 25 }; | 27 }; |
| 26 | 28 |
| 29 // Checks if the path exists and starts loading the image. | |
| 30 void StartOnBlockingPool( | |
| 31 const std::string& path, | |
| 32 scoped_refptr<UserImageLoader> image_loader, | |
| 33 const UserImageLoader::LoadedCallback& image_loaded_callback, | |
| 34 const content::URLDataSource::GotDataCallback& url_data_callback) { | |
| 35 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | |
| 36 | |
| 37 base::FilePath shared_assets_dir; | |
| 38 if (PathService::Get(DIR_SHARED_ASSETS, &shared_assets_dir)) { | |
| 39 base::FilePath file_path = shared_assets_dir.AppendASCII(path); | |
| 40 if (base::PathExists(file_path)) { | |
| 41 image_loader->Start(file_path.value(), 0, image_loaded_callback); | |
| 42 return; | |
| 43 } | |
| 44 } | |
| 45 | |
| 46 url_data_callback.Run(NULL); | |
|
stevenjb
2014/12/05 19:31:53
This invokes |url_data_callback| (which is just |c
michaelpg
2014/12/05 22:42:11
Done.
| |
| 47 return; | |
| 48 } | |
| 49 | |
| 27 } // namespace | 50 } // namespace |
| 28 | 51 |
| 29 ImageSource::ImageSource() : weak_factory_(this) { | 52 ImageSource::ImageSource() : weak_factory_(this) { |
| 30 base::SequencedWorkerPool* blocking_pool = | 53 base::SequencedWorkerPool* blocking_pool = |
| 31 BrowserThread::GetBlockingPool(); | 54 BrowserThread::GetBlockingPool(); |
| 32 task_runner_ = blocking_pool->GetSequencedTaskRunnerWithShutdownBehavior( | 55 task_runner_ = blocking_pool->GetSequencedTaskRunnerWithShutdownBehavior( |
| 33 blocking_pool->GetSequenceToken(), | 56 blocking_pool->GetSequenceToken(), |
| 34 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN); | 57 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN); |
| 35 } | 58 } |
| 36 | 59 |
| 37 ImageSource::~ImageSource() { | 60 ImageSource::~ImageSource() { |
| 38 } | 61 } |
| 39 | 62 |
| 40 std::string ImageSource::GetSource() const { | 63 std::string ImageSource::GetSource() const { |
| 41 return chrome::kChromeOSAssetHost; | 64 return chrome::kChromeOSAssetHost; |
| 42 } | 65 } |
| 43 | 66 |
| 44 void ImageSource::StartDataRequest( | 67 void ImageSource::StartDataRequest( |
| 45 const std::string& path, | 68 const std::string& path, |
| 46 int render_process_id, | 69 int render_process_id, |
| 47 int render_frame_id, | 70 int render_frame_id, |
| 48 const content::URLDataSource::GotDataCallback& callback) { | 71 const content::URLDataSource::GotDataCallback& callback) { |
| 49 if (!IsWhitelisted(path)) { | 72 if (!IsWhitelisted(path)) { |
| 50 callback.Run(NULL); | 73 callback.Run(NULL); |
| 51 return; | 74 return; |
| 52 } | 75 } |
| 53 BrowserThread::PostTask( | 76 |
|
michaelpg
2014/12/05 09:44:43
So this was also using the deprecated FILE thread,
| |
| 54 BrowserThread::FILE, FROM_HERE, | 77 if (!image_loader_) { |
| 55 base::Bind(&ImageSource::StartOnFileThread, | 78 image_loader_ = new UserImageLoader(ImageDecoder::DEFAULT_CODEC, |
| 79 task_runner_); | |
|
stevenjb
2014/12/05 19:31:53
Constructing this here means that |callback| will
michaelpg
2014/12/05 22:42:11
Done.
| |
| 80 } | |
| 81 | |
| 82 UserImageLoader::LoadedCallback image_loaded_callback = | |
| 83 base::Bind(&ImageSource::ImageLoaded, | |
| 56 weak_factory_.GetWeakPtr(), | 84 weak_factory_.GetWeakPtr(), |
| 57 path, | 85 callback); |
| 58 callback)); | 86 task_runner_->PostTask(FROM_HERE, |
| 87 base::Bind(&StartOnBlockingPool, | |
| 88 path, | |
| 89 image_loader_, | |
| 90 image_loaded_callback, | |
| 91 callback)); | |
| 59 } | 92 } |
| 60 | 93 |
| 61 std::string ImageSource::GetMimeType(const std::string& path) const { | 94 std::string ImageSource::GetMimeType(const std::string& path) const { |
| 62 std::string mime_type; | 95 std::string mime_type; |
| 63 std::string ext = base::FilePath(path).Extension(); | 96 std::string ext = base::FilePath(path).Extension(); |
| 64 if (!ext.empty()) | 97 if (!ext.empty()) |
| 65 net::GetWellKnownMimeTypeFromExtension(ext.substr(1), &mime_type); | 98 net::GetWellKnownMimeTypeFromExtension(ext.substr(1), &mime_type); |
| 66 return mime_type; | 99 return mime_type; |
| 67 } | 100 } |
| 68 | 101 |
| 69 void ImageSource::StartOnFileThread( | |
| 70 const std::string& path, | |
| 71 const content::URLDataSource::GotDataCallback& callback) { | |
| 72 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 73 | |
| 74 base::FilePath file_path(chrome::kChromeOSAssetPath + path); | |
| 75 if (!base::PathExists(file_path)) { | |
| 76 callback.Run(NULL); | |
| 77 return; | |
| 78 } | |
| 79 | |
| 80 image_loader_ = new UserImageLoader(ImageDecoder::DEFAULT_CODEC, | |
| 81 task_runner_); | |
| 82 image_loader_->Start(file_path.value(), | |
| 83 0, | |
| 84 base::Bind(&ImageSource::ImageLoaded, | |
| 85 weak_factory_.GetWeakPtr(), | |
| 86 callback)); | |
| 87 } | |
| 88 | 102 |
| 89 void ImageSource::ImageLoaded( | 103 void ImageSource::ImageLoaded( |
| 90 const content::URLDataSource::GotDataCallback& callback, | 104 const content::URLDataSource::GotDataCallback& callback, |
| 91 const user_manager::UserImage& user_image) const { | 105 const user_manager::UserImage& user_image) const { |
| 92 if (user_image.has_raw_image()) | 106 if (user_image.has_raw_image()) |
| 93 callback.Run(new base::RefCountedBytes(user_image.raw_image())); | 107 callback.Run(new base::RefCountedBytes(user_image.raw_image())); |
| 94 else | 108 else |
| 95 callback.Run(NULL); | 109 callback.Run(NULL); |
| 96 } | 110 } |
| 97 | 111 |
| 98 bool ImageSource::IsWhitelisted(const std::string& path) const { | 112 bool ImageSource::IsWhitelisted(const std::string& path) const { |
| 99 const char** end = kWhitelistedFiles + arraysize(kWhitelistedFiles); | 113 const char** end = kWhitelistedFiles + arraysize(kWhitelistedFiles); |
| 100 return std::find(kWhitelistedFiles, end, path) != end; | 114 return std::find(kWhitelistedFiles, end, path) != end; |
| 101 } | 115 } |
| 102 | 116 |
| 103 } // namespace chromeos | 117 } // namespace chromeos |
| OLD | NEW |