summaryrefslogtreecommitdiffstats
path: root/chrome/browser/views/tab_icon_view.cc
blob: 245a38dded8dd07ff91b2fe7050643c8c076c496 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <windows.h>
#include <shellapi.h>

#include "chrome/browser/views/tab_icon_view.h"

#include "base/file_util.h"
#include "base/path_service.h"
#include "chrome/app/theme/theme_resources.h"
#include "chrome/common/gfx/chrome_canvas.h"
#include "chrome/common/gfx/favicon_size.h"
#include "chrome/common/gfx/icon_util.h"
#include "chrome/common/resource_bundle.h"
#include "chrome/browser/tab_contents.h"
#include "chrome/app/chrome_dll_resource.h"

static bool g_initialized = false;
static SkBitmap* g_default_fav_icon = NULL;
static SkBitmap* g_throbber_frames = NULL;
static SkBitmap* g_throbber_frames_light = NULL;
static int g_throbber_frame_count;

// static
void TabIconView::InitializeIfNeeded() {
  if (!g_initialized) {
    ResourceBundle &rb = ResourceBundle::GetSharedInstance();

    // The default window icon is the application icon, not the default
    // favicon.
    std::wstring exe_path;
    PathService::Get(base::DIR_EXE, &exe_path);
    file_util::AppendToPath(&exe_path, L"chrome.exe");
    HICON app_icon = ExtractIcon(NULL, exe_path.c_str(), 0);
    g_default_fav_icon =
        IconUtil::CreateSkBitmapFromHICON(app_icon, gfx::Size(16, 16));
    DestroyIcon(app_icon);

    g_throbber_frames = rb.GetBitmapNamed(IDR_THROBBER);
    g_throbber_frames_light = rb.GetBitmapNamed(IDR_THROBBER_LIGHT);
    g_throbber_frame_count = g_throbber_frames->width() /
        g_throbber_frames->height();

    // Verify that our light and dark styles have the same number of frames.
    DCHECK(g_throbber_frame_count ==
        g_throbber_frames_light->width() / g_throbber_frames_light->height());
    g_initialized = true;
  }
}

TabIconView::TabIconView(TabContentsProvider* provider)
    : provider_(provider),
      is_light_(false),
      throbber_running_(false),
      throbber_frame_(0) {
  InitializeIfNeeded();
}

TabIconView::~TabIconView() {
}

void TabIconView::Update() {
  TabContents* contents = provider_->GetCurrentTabContents();
  if (throbber_running_) {
    // We think the tab is loading.
    if (!contents || !contents->is_loading()) {
      // Woops, tab is invalid or not loading, reset our status and schedule
      // a paint.
      throbber_running_ = false;
      SchedulePaint();
    } else {
      // The tab is still loading, increment the frame.
      throbber_frame_ = (throbber_frame_ + 1) % g_throbber_frame_count;
      SchedulePaint();
    }
  } else if (contents && contents->is_loading()) {
    // We didn't think we were loading, but the tab is loading. Reset the
    // frame and status and schedule a paint.
    throbber_running_ = true;
    throbber_frame_ = 0;
    SchedulePaint();
  }
}

void TabIconView::PaintThrobber(ChromeCanvas* canvas) {
  int image_size = g_throbber_frames->height();
  int image_offset = throbber_frame_ * image_size;
  canvas->DrawBitmapInt(is_light_ ? *g_throbber_frames_light :
                                    *g_throbber_frames,
                        image_offset, 0, image_size, image_size,
                        0, 0, image_size, image_size, false);
}

void TabIconView::PaintFavIcon(ChromeCanvas* canvas, const SkBitmap& bitmap) {
  int bw = bitmap.width();
  int bh = bitmap.height();
  if (bw <= kFavIconSize && bh <= kFavIconSize) {
    canvas->DrawBitmapInt(bitmap, (width() - kFavIconSize) / 2,
                          (height() - kFavIconSize) / 2);
  } else {
    canvas->DrawBitmapInt(bitmap, 0, 0, bw, bh, 0, 0, width(), height(),
                          true);
  }
}

void TabIconView::Paint(ChromeCanvas* canvas) {
  TabContents* contents = provider_->GetCurrentTabContents();
  bool rendered = false;

  if (contents) {
    if (throbber_running_) {
      rendered = true;
      PaintThrobber(canvas);
    } else {
      SkBitmap favicon = provider_->GetFavIcon();
      if (!favicon.isNull()) {
        rendered = true;
        PaintFavIcon(canvas, favicon);
      }
    }
  }

  if (!rendered) {
    PaintFavIcon(canvas, *g_default_fav_icon);
  }
}

gfx::Size TabIconView::GetPreferredSize() {
  return gfx::Size(kFavIconSize, kFavIconSize);
}