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

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: 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 // Callback for user_manager::UserImageLoader.
28 void ImageLoaded(
29 const content::URLDataSource::GotDataCallback& got_data_callback,
30 const user_manager::UserImage& user_image) {
31 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
32
33 if (user_image.has_raw_image())
34 got_data_callback.Run(new base::RefCountedBytes(user_image.raw_image()));
35 else
36 got_data_callback.Run(NULL);
37 }
38
39 // Looks for the image at |path| under the shared assets directory.
40 void StartOnBlockingPool(
41 const std::string& path,
42 scoped_refptr<UserImageLoader> image_loader,
43 const content::URLDataSource::GotDataCallback& got_data_callback,
44 scoped_refptr<base::MessageLoopProxy> message_loop_proxy) {
45 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
46
47 const base::FilePath asset_dir(FILE_PATH_LITERAL(chrome::kChromeOSAssetPath));
48 const base::FilePath image_path = asset_dir.AppendASCII(path);
49 if (base::PathExists(image_path)) {
50 image_loader->Start(image_path.value(), 0,
51 base::Bind(&ImageLoaded, got_data_callback));
52 } else {
53 message_loop_proxy->PostTask(FROM_HERE,
54 base::Bind(got_data_callback, nullptr));
55 }
56 }
57
27 } // namespace 58 } // namespace
28 59
29 ImageSource::ImageSource() : weak_factory_(this) { 60 ImageSource::ImageSource() : weak_factory_(this) {
30 base::SequencedWorkerPool* blocking_pool = 61 base::SequencedWorkerPool* blocking_pool =
31 BrowserThread::GetBlockingPool(); 62 BrowserThread::GetBlockingPool();
32 task_runner_ = blocking_pool->GetSequencedTaskRunnerWithShutdownBehavior( 63 task_runner_ = blocking_pool->GetSequencedTaskRunnerWithShutdownBehavior(
33 blocking_pool->GetSequenceToken(), 64 blocking_pool->GetSequenceToken(),
34 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN); 65 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN);
35 } 66 }
36 67
37 ImageSource::~ImageSource() { 68 ImageSource::~ImageSource() {
38 } 69 }
39 70
40 std::string ImageSource::GetSource() const { 71 std::string ImageSource::GetSource() const {
41 return chrome::kChromeOSAssetHost; 72 return chrome::kChromeOSAssetHost;
42 } 73 }
43 74
44 void ImageSource::StartDataRequest( 75 void ImageSource::StartDataRequest(
45 const std::string& path, 76 const std::string& path,
46 int render_process_id, 77 int render_process_id,
47 int render_frame_id, 78 int render_frame_id,
48 const content::URLDataSource::GotDataCallback& callback) { 79 const content::URLDataSource::GotDataCallback& got_data_callback) {
49 if (!IsWhitelisted(path)) { 80 if (!IsWhitelisted(path)) {
50 callback.Run(NULL); 81 got_data_callback.Run(NULL);
51 return; 82 return;
52 } 83 }
53 BrowserThread::PostTask( 84
54 BrowserThread::FILE, FROM_HERE, 85 if (!image_loader_) {
55 base::Bind(&ImageSource::StartOnFileThread, 86 image_loader_ = new UserImageLoader(ImageDecoder::DEFAULT_CODEC,
56 weak_factory_.GetWeakPtr(), 87 task_runner_);
88 }
89
90 content::BrowserThread::GetBlockingPool()->PostTask(
91 FROM_HERE,
92 base::Bind(&StartOnBlockingPool,
57 path, 93 path,
58 callback)); 94 image_loader_,
95 got_data_callback,
96 base::MessageLoopProxy::current()));
59 } 97 }
60 98
61 std::string ImageSource::GetMimeType(const std::string& path) const { 99 std::string ImageSource::GetMimeType(const std::string& path) const {
62 std::string mime_type; 100 std::string mime_type;
63 std::string ext = base::FilePath(path).Extension(); 101 std::string ext = base::FilePath(path).Extension();
64 if (!ext.empty()) 102 if (!ext.empty())
65 net::GetWellKnownMimeTypeFromExtension(ext.substr(1), &mime_type); 103 net::GetWellKnownMimeTypeFromExtension(ext.substr(1), &mime_type);
66 return mime_type; 104 return mime_type;
67 } 105 }
68 106
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 { 107 bool ImageSource::IsWhitelisted(const std::string& path) const {
99 const char** end = kWhitelistedFiles + arraysize(kWhitelistedFiles); 108 const char** end = kWhitelistedFiles + arraysize(kWhitelistedFiles);
100 return std::find(kWhitelistedFiles, end, path) != end; 109 return std::find(kWhitelistedFiles, end, path) != end;
101 } 110 }
102 111
103 } // namespace chromeos 112 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/ui/webui/chromeos/image_source.h ('k') | chrome/browser/ui/webui/help/help_handler.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698