Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(197)

Side by Side Diff: chrome/browser/ui/webui/chromeos/image_source.cc

Issue 780203002: Fix threading bugs in product label UI. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address satorux's comments Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "chrome/browser/chromeos/login/users/avatar/user_image_loader.h" 12 #include "chrome/browser/chromeos/login/users/avatar/user_image_loader.h"
13 #include "chrome/common/url_constants.h" 13 #include "chrome/common/url_constants.h"
14 #include "components/user_manager/user_image/user_image.h" 14 #include "components/user_manager/user_image/user_image.h"
15 #include "content/public/browser/browser_thread.h" 15 #include "content/public/browser/browser_thread.h"
16 #include "net/base/mime_util.h" 16 #include "net/base/mime_util.h"
17 17
18 using content::BrowserThread; 18 using content::BrowserThread;
19 19
20 namespace chromeos { 20 namespace chromeos {
21 namespace { 21 namespace {
22 22
23 const char* kWhitelistedFiles[] = { 23 const char* kWhitelistedFiles[] = {
24 "fcc/label.png" 24 "fcc/label.png"
25 }; 25 };
26 26
27 // Looks for the image at |path| under the shared assets directory. If found,
28 // starts loading the image and returns true. Otherwise, returns false.
29 bool StartOnBlockingPool(
30 const std::string& path,
31 scoped_refptr<UserImageLoader> image_loader,
32 const UserImageLoader::LoadedCallback& image_loaded_callback) {
33 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
34
35 const base::FilePath asset_dir(FILE_PATH_LITERAL(chrome::kChromeOSAssetPath));
36 const base::FilePath image_path = asset_dir.AppendASCII(path);
37 if (base::PathExists(image_path)) {
38 image_loader->Start(image_path.value(), 0, image_loaded_callback);
39 return true;
40 }
41
42 return false;
43 }
44
45 // Callback after attempting to start loading the image. Runs
46 // |got_data_callback| if loading wasn't started.
47 void LoadImageAttempted(
48 const content::URLDataSource::GotDataCallback& got_data_callback,
49 bool load_started) {
satorux1 2014/12/09 05:56:40 maybe add a DCHECK to ensure this runs on the UI t
michaelpg 2014/12/10 00:48:51 fn removed
50 if (!load_started)
51 got_data_callback.Run(NULL);
52 }
53
54 // Callback for user_manager::UserImageLoader.
55 void ImageLoaded(
satorux1 2014/12/09 05:56:40 maybe add a DCHECK to ensure this runs on the UI t
michaelpg 2014/12/10 00:48:51 Done.
56 const content::URLDataSource::GotDataCallback& got_data_callback,
57 const user_manager::UserImage& user_image) {
58 if (user_image.has_raw_image())
59 got_data_callback.Run(new base::RefCountedBytes(user_image.raw_image()));
60 else
61 got_data_callback.Run(NULL);
62 }
63
27 } // namespace 64 } // namespace
28 65
29 ImageSource::ImageSource() : weak_factory_(this) { 66 ImageSource::ImageSource() : weak_factory_(this) {
30 base::SequencedWorkerPool* blocking_pool = 67 base::SequencedWorkerPool* blocking_pool =
31 BrowserThread::GetBlockingPool(); 68 BrowserThread::GetBlockingPool();
32 task_runner_ = blocking_pool->GetSequencedTaskRunnerWithShutdownBehavior( 69 task_runner_ = blocking_pool->GetSequencedTaskRunnerWithShutdownBehavior(
33 blocking_pool->GetSequenceToken(), 70 blocking_pool->GetSequenceToken(),
34 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN); 71 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN);
35 } 72 }
36 73
37 ImageSource::~ImageSource() { 74 ImageSource::~ImageSource() {
38 } 75 }
39 76
40 std::string ImageSource::GetSource() const { 77 std::string ImageSource::GetSource() const {
41 return chrome::kChromeOSAssetHost; 78 return chrome::kChromeOSAssetHost;
42 } 79 }
43 80
44 void ImageSource::StartDataRequest( 81 void ImageSource::StartDataRequest(
45 const std::string& path, 82 const std::string& path,
46 int render_process_id, 83 int render_process_id,
47 int render_frame_id, 84 int render_frame_id,
48 const content::URLDataSource::GotDataCallback& callback) { 85 const content::URLDataSource::GotDataCallback& got_data_callback) {
49 if (!IsWhitelisted(path)) { 86 if (!IsWhitelisted(path)) {
50 callback.Run(NULL); 87 got_data_callback.Run(NULL);
51 return; 88 return;
52 } 89 }
53 BrowserThread::PostTask( 90
54 BrowserThread::FILE, FROM_HERE, 91 if (!image_loader_) {
55 base::Bind(&ImageSource::StartOnFileThread, 92 image_loader_ = new UserImageLoader(ImageDecoder::DEFAULT_CODEC,
56 weak_factory_.GetWeakPtr(), 93 task_runner_);
94 }
95
96 UserImageLoader::LoadedCallback image_loaded_callback =
97 base::Bind(&ImageLoaded, got_data_callback);
98 base::PostTaskAndReplyWithResult(
99 content::BrowserThread::GetBlockingPool(),
100 FROM_HERE,
101 base::Bind(&StartOnBlockingPool,
57 path, 102 path,
58 callback)); 103 image_loader_,
104 image_loaded_callback),
105 base::Bind(LoadImageAttempted, got_data_callback));
satorux1 2014/12/09 05:56:40 got_data_callback can be called either from LoadIm
michaelpg 2014/12/10 00:48:51 I could not find a way to run base::Bind on got_da
satorux1 2014/12/10 01:18:17 Try passing nullptr instead of NULL.
michaelpg 2014/12/10 01:45:11 Done.
59 } 106 }
60 107
61 std::string ImageSource::GetMimeType(const std::string& path) const { 108 std::string ImageSource::GetMimeType(const std::string& path) const {
62 std::string mime_type; 109 std::string mime_type;
63 std::string ext = base::FilePath(path).Extension(); 110 std::string ext = base::FilePath(path).Extension();
64 if (!ext.empty()) 111 if (!ext.empty())
65 net::GetWellKnownMimeTypeFromExtension(ext.substr(1), &mime_type); 112 net::GetWellKnownMimeTypeFromExtension(ext.substr(1), &mime_type);
66 return mime_type; 113 return mime_type;
67 } 114 }
68 115
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
89 void ImageSource::ImageLoaded(
90 const content::URLDataSource::GotDataCallback& callback,
91 const user_manager::UserImage& user_image) const {
92 if (user_image.has_raw_image())
93 callback.Run(new base::RefCountedBytes(user_image.raw_image()));
94 else
95 callback.Run(NULL);
96 }
97
98 bool ImageSource::IsWhitelisted(const std::string& path) const { 116 bool ImageSource::IsWhitelisted(const std::string& path) const {
99 const char** end = kWhitelistedFiles + arraysize(kWhitelistedFiles); 117 const char** end = kWhitelistedFiles + arraysize(kWhitelistedFiles);
100 return std::find(kWhitelistedFiles, end, path) != end; 118 return std::find(kWhitelistedFiles, end, path) != end;
101 } 119 }
102 120
103 } // namespace chromeos 121 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698