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

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: fix ImageSource 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/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 image can be found, and starts loading the image if so.
30 // Returns true if the image is found and false otherwise.
stevenjb 2014/12/05 23:28:07 More relevant is that it returns true if it starts
michaelpg 2014/12/08 00:58:19 Done.
31 bool StartOnBlockingPool(
32 const std::string& path,
33 scoped_refptr<UserImageLoader> image_loader,
34 const UserImageLoader::LoadedCallback& image_loaded_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 true;
43 }
44 }
45
46 return false;
47 }
48
27 } // namespace 49 } // namespace
28 50
29 ImageSource::ImageSource() : weak_factory_(this) { 51 ImageSource::ImageSource() : weak_factory_(this) {
30 base::SequencedWorkerPool* blocking_pool = 52 base::SequencedWorkerPool* blocking_pool =
31 BrowserThread::GetBlockingPool(); 53 BrowserThread::GetBlockingPool();
32 task_runner_ = blocking_pool->GetSequencedTaskRunnerWithShutdownBehavior( 54 task_runner_ = blocking_pool->GetSequencedTaskRunnerWithShutdownBehavior(
33 blocking_pool->GetSequenceToken(), 55 blocking_pool->GetSequenceToken(),
34 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN); 56 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN);
35 } 57 }
36 58
37 ImageSource::~ImageSource() { 59 ImageSource::~ImageSource() {
38 } 60 }
39 61
40 std::string ImageSource::GetSource() const { 62 std::string ImageSource::GetSource() const {
41 return chrome::kChromeOSAssetHost; 63 return chrome::kChromeOSAssetHost;
42 } 64 }
43 65
44 void ImageSource::StartDataRequest( 66 void ImageSource::StartDataRequest(
45 const std::string& path, 67 const std::string& path,
46 int render_process_id, 68 int render_process_id,
47 int render_frame_id, 69 int render_frame_id,
48 const content::URLDataSource::GotDataCallback& callback) { 70 const content::URLDataSource::GotDataCallback& got_data_callback) {
49 if (!IsWhitelisted(path)) { 71 if (!IsWhitelisted(path)) {
50 callback.Run(NULL); 72 got_data_callback.Run(NULL);
51 return; 73 return;
52 } 74 }
53 BrowserThread::PostTask( 75
54 BrowserThread::FILE, FROM_HERE, 76 if (!image_loader_) {
55 base::Bind(&ImageSource::StartOnFileThread, 77 image_loader_ = new UserImageLoader(ImageDecoder::DEFAULT_CODEC,
78 task_runner_);
79 }
80
81 UserImageLoader::LoadedCallback image_loaded_callback =
82 base::Bind(&ImageSource::ImageLoaded,
56 weak_factory_.GetWeakPtr(), 83 weak_factory_.GetWeakPtr(),
84 got_data_callback);
85 base::PostTaskAndReplyWithResult(
86 content::BrowserThread::GetBlockingPool(),
87 FROM_HERE,
88 base::Bind(&StartOnBlockingPool,
57 path, 89 path,
58 callback)); 90 image_loader_,
91 image_loaded_callback),
92 base::Bind(&ImageSource::LoadImageAttempted,
93 weak_factory_.GetWeakPtr(),
94 got_data_callback));
59 } 95 }
60 96
61 std::string ImageSource::GetMimeType(const std::string& path) const { 97 std::string ImageSource::GetMimeType(const std::string& path) const {
62 std::string mime_type; 98 std::string mime_type;
63 std::string ext = base::FilePath(path).Extension(); 99 std::string ext = base::FilePath(path).Extension();
64 if (!ext.empty()) 100 if (!ext.empty())
65 net::GetWellKnownMimeTypeFromExtension(ext.substr(1), &mime_type); 101 net::GetWellKnownMimeTypeFromExtension(ext.substr(1), &mime_type);
66 return mime_type; 102 return mime_type;
67 } 103 }
68 104
69 void ImageSource::StartOnFileThread( 105 void ImageSource::LoadImageAttempted(
70 const std::string& path, 106 const content::URLDataSource::GotDataCallback& got_data_callback,
71 const content::URLDataSource::GotDataCallback& callback) { 107 bool load_started) {
72 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 108 if (!load_started)
73 109 got_data_callback.Run(NULL);
stevenjb 2014/12/05 23:28:07 This could be an anonymous non-member function. |g
michaelpg 2014/12/08 00:58:19 Done, along with ImageLoaded.
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 } 110 }
88 111
89 void ImageSource::ImageLoaded( 112 void ImageSource::ImageLoaded(
90 const content::URLDataSource::GotDataCallback& callback, 113 const content::URLDataSource::GotDataCallback& got_data_callback,
91 const user_manager::UserImage& user_image) const { 114 const user_manager::UserImage& user_image) const {
92 if (user_image.has_raw_image()) 115 if (user_image.has_raw_image())
93 callback.Run(new base::RefCountedBytes(user_image.raw_image())); 116 got_data_callback.Run(new base::RefCountedBytes(user_image.raw_image()));
94 else 117 else
95 callback.Run(NULL); 118 got_data_callback.Run(NULL);
96 } 119 }
97 120
98 bool ImageSource::IsWhitelisted(const std::string& path) const { 121 bool ImageSource::IsWhitelisted(const std::string& path) const {
99 const char** end = kWhitelistedFiles + arraysize(kWhitelistedFiles); 122 const char** end = kWhitelistedFiles + arraysize(kWhitelistedFiles);
100 return std::find(kWhitelistedFiles, end, path) != end; 123 return std::find(kWhitelistedFiles, end, path) != end;
101 } 124 }
102 125
103 } // namespace chromeos 126 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698