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

Side by Side Diff: chrome/browser/image_loading_tracker.cc

Issue 256022: Loads local resources from current locale subtree if available, if not it fal... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 2 months 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 | Annotate | Revision Log
« no previous file with comments | « chrome/browser/image_loading_tracker.h ('k') | chrome/browser/net/file_reader.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/image_loading_tracker.h"
6
7 #include "app/gfx/favicon_size.h"
8 #include "base/file_util.h"
9 #include "base/logging.h"
10 #include "base/message_loop.h"
11 #include "base/scoped_ptr.h"
12 #include "base/task.h"
13 #include "base/thread.h"
14 #include "chrome/browser/browser_process.h"
15 #include "skia/ext/image_operations.h"
16 #include "third_party/skia/include/core/SkBitmap.h"
17 #include "webkit/glue/image_decoder.h"
18
19 ////////////////////////////////////////////////////////////////////////////////
20 // ImageLoadingTracker::LoadImageTask
21
22 // The LoadImageTask is for asynchronously loading the image on the file thread.
23 // If the image is successfully loaded and decoded it will report back on the
24 // |callback_loop| to let the caller know the image is done loading.
25 class ImageLoadingTracker::LoadImageTask : public Task {
26 public:
27 // Constructor for the LoadImageTask class. |tracker| is the object that
28 // we use to communicate back to the entity that wants the image after we
29 // decode it. |path| is the path to load the image from. |index| is an
30 // identifier for the image that we pass back to the caller.
31 LoadImageTask(ImageLoadingTracker* tracker,
32 const FilePath& path,
33 size_t index)
34 : callback_loop_(MessageLoop::current()),
35 tracker_(tracker),
36 path_(path),
37 index_(index) {}
38
39 void ReportBack(SkBitmap* image) {
40 DCHECK(image);
41 callback_loop_->PostTask(FROM_HERE, NewRunnableMethod(tracker_,
42 &ImageLoadingTracker::OnImageLoaded,
43 image,
44 index_));
45 }
46
47 virtual void Run() {
48 // Read the file from disk.
49 std::string file_contents;
50 if (!file_util::PathExists(path_) ||
51 !file_util::ReadFileToString(path_, &file_contents)) {
52 ReportBack(NULL);
53 return;
54 }
55
56 // Decode the image using WebKit's image decoder.
57 const unsigned char* data =
58 reinterpret_cast<const unsigned char*>(file_contents.data());
59 webkit_glue::ImageDecoder decoder(gfx::Size(kFavIconSize, kFavIconSize));
60 scoped_ptr<SkBitmap> decoded(new SkBitmap());
61 *decoded = decoder.Decode(data, file_contents.length());
62 if (decoded->empty()) {
63 ReportBack(NULL);
64 return; // Unable to decode.
65 }
66
67 if (decoded->width() != kFavIconSize || decoded->height() != kFavIconSize) {
68 // The bitmap is not the correct size, re-sample.
69 int new_width = decoded->width();
70 int new_height = decoded->height();
71 // Calculate what dimensions to use within the constraints (16x16 max).
72 calc_favicon_target_size(&new_width, &new_height);
73 *decoded = skia::ImageOperations::Resize(
74 *decoded, skia::ImageOperations::RESIZE_LANCZOS3,
75 new_width, new_height);
76 }
77
78 ReportBack(decoded.release());
79 }
80
81 private:
82 // The message loop that we need to call back on to report that we are done.
83 MessageLoop* callback_loop_;
84
85 // The object that is waiting for us to respond back.
86 ImageLoadingTracker* tracker_;
87
88 // The path to the image to load asynchronously.
89 FilePath path_;
90
91 // The index of the icon being loaded.
92 size_t index_;
93 };
94
95 ////////////////////////////////////////////////////////////////////////////////
96 // ImageLoadingTracker
97
98 void ImageLoadingTracker::PostLoadImageTask(FilePath path) {
99 MessageLoop* file_loop = g_browser_process->file_thread()->message_loop();
100 file_loop->PostTask(FROM_HERE, new LoadImageTask(this, path,
101 posted_count_++));
102 }
103
104 void ImageLoadingTracker::OnImageLoaded(SkBitmap* image, size_t index) {
105 if (image == NULL) {
106 NOTREACHED() << "Image failed to decode.";
107 image = new SkBitmap();
108 }
109 if (observer_)
110 observer_->OnImageLoaded(image, index);
111 delete image;
112 if (--image_count_ == 0)
113 Release(); // We are no longer needed.
114 }
OLDNEW
« no previous file with comments | « chrome/browser/image_loading_tracker.h ('k') | chrome/browser/net/file_reader.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698