Home | History | Annotate | Download | only in browser
      1 // Copyright (c) 2012 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/icon_loader.h"
      6 
      7 #import <AppKit/AppKit.h>
      8 
      9 #include "base/bind.h"
     10 #include "base/files/file_path.h"
     11 #include "base/message_loop/message_loop.h"
     12 #include "base/strings/sys_string_conversions.h"
     13 #include "base/threading/thread.h"
     14 #include "ui/gfx/image/image_skia.h"
     15 #include "ui/gfx/image/image_skia_util_mac.h"
     16 
     17 // static
     18 IconGroupID IconLoader::ReadGroupIDFromFilepath(
     19     const base::FilePath& filepath) {
     20   return filepath.Extension();
     21 }
     22 
     23 // static
     24 bool IconLoader::IsIconMutableFromFilepath(const base::FilePath&) {
     25   return false;
     26 }
     27 
     28 // static
     29 content::BrowserThread::ID IconLoader::ReadIconThreadID() {
     30   return content::BrowserThread::FILE;
     31 }
     32 
     33 void IconLoader::ReadIcon() {
     34   NSString* group = base::SysUTF8ToNSString(group_);
     35   NSWorkspace* workspace = [NSWorkspace sharedWorkspace];
     36   NSImage* icon = [workspace iconForFileType:group];
     37 
     38   if (icon_size_ == ALL) {
     39     // The NSImage already has all sizes.
     40     image_.reset(new gfx::Image([icon retain]));
     41   } else {
     42     NSSize size = NSZeroSize;
     43     switch (icon_size_) {
     44       case IconLoader::SMALL:
     45         size = NSMakeSize(16, 16);
     46         break;
     47       case IconLoader::NORMAL:
     48         size = NSMakeSize(32, 32);
     49         break;
     50       default:
     51         NOTREACHED();
     52     }
     53     gfx::ImageSkia image_skia(gfx::ImageSkiaFromResizedNSImage(icon, size));
     54     if (!image_skia.isNull()) {
     55       image_skia.MakeThreadSafe();
     56       image_.reset(new gfx::Image(image_skia));
     57     }
     58   }
     59 
     60   target_message_loop_->PostTask(FROM_HERE,
     61       base::Bind(&IconLoader::NotifyDelegate, this));
     62 }
     63