OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/web_applications/web_app_ui.h" | 5 #include "chrome/browser/ui/web_applications/web_app_ui.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
11 #include "base/string16.h" | 11 #include "base/string16.h" |
12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
13 #include "chrome/browser/extensions/tab_helper.h" | 13 #include "chrome/browser/extensions/tab_helper.h" |
14 #include "chrome/browser/favicon/favicon_download_helper.h" | |
15 #include "chrome/browser/favicon/favicon_download_helper_delegate.h" | |
16 #include "chrome/browser/favicon/favicon_tab_helper.h" | 14 #include "chrome/browser/favicon/favicon_tab_helper.h" |
17 #include "chrome/browser/favicon/favicon_util.h" | 15 #include "chrome/browser/favicon/favicon_util.h" |
18 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
19 #include "chrome/browser/web_applications/web_app.h" | 17 #include "chrome/browser/web_applications/web_app.h" |
20 #include "chrome/common/chrome_notification_types.h" | 18 #include "chrome/common/chrome_notification_types.h" |
21 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
20 #include "content/public/browser/favicon_download_delegate.h" | |
22 #include "content/public/browser/notification_details.h" | 21 #include "content/public/browser/notification_details.h" |
23 #include "content/public/browser/notification_registrar.h" | 22 #include "content/public/browser/notification_registrar.h" |
24 #include "content/public/browser/notification_source.h" | 23 #include "content/public/browser/notification_source.h" |
25 #include "content/public/browser/web_contents.h" | 24 #include "content/public/browser/web_contents.h" |
26 | 25 |
27 #if defined(OS_POSIX) && !defined(OS_MACOSX) | 26 #if defined(OS_POSIX) && !defined(OS_MACOSX) |
28 #include "base/environment.h" | 27 #include "base/environment.h" |
29 #endif | 28 #endif |
30 | 29 |
31 #if defined(OS_WIN) | 30 #if defined(OS_WIN) |
32 #include "base/win/shortcut.h" | 31 #include "base/win/shortcut.h" |
33 #include "base/win/windows_version.h" | 32 #include "base/win/windows_version.h" |
34 #endif | 33 #endif |
35 | 34 |
36 using content::BrowserThread; | 35 using content::BrowserThread; |
37 using content::NavigationController; | 36 using content::NavigationController; |
38 using content::WebContents; | 37 using content::WebContents; |
39 | 38 |
40 namespace { | 39 namespace { |
41 | 40 |
42 #if defined(OS_WIN) | 41 #if defined(OS_WIN) |
43 // UpdateShortcutWorker holds all context data needed for update shortcut. | 42 // UpdateShortcutWorker holds all context data needed for update shortcut. |
44 // It schedules a pre-update check to find all shortcuts that needs to be | 43 // It schedules a pre-update check to find all shortcuts that needs to be |
45 // updated. If there are such shortcuts, it schedules icon download and | 44 // updated. If there are such shortcuts, it schedules icon download and |
46 // update them when icons are downloaded. It observes TAB_CLOSING notification | 45 // update them when icons are downloaded. It observes TAB_CLOSING notification |
47 // and cancels all the work when the underlying tab is closing. | 46 // and cancels all the work when the underlying tab is closing. |
48 class UpdateShortcutWorker : public content::NotificationObserver, | 47 class UpdateShortcutWorker : public content::NotificationObserver, |
49 public FaviconDownloadHelperDelegate { | 48 public content::FaviconDownloadDelegate { |
50 public: | 49 public: |
51 explicit UpdateShortcutWorker(WebContents* web_contents); | 50 explicit UpdateShortcutWorker(WebContents* web_contents); |
52 | 51 |
53 void Run(); | 52 void Run(); |
54 | 53 |
55 private: | 54 private: |
56 // Overridden from content::NotificationObserver: | 55 // Overridden from content::NotificationObserver: |
57 virtual void Observe(int type, | 56 virtual void Observe(int type, |
58 const content::NotificationSource& source, | 57 const content::NotificationSource& source, |
59 const content::NotificationDetails& details); | 58 const content::NotificationDetails& details); |
60 | 59 |
61 // Downloads icon via the FaviconTabHelper. | 60 // Downloads icon via the FaviconTabHelper. |
62 void DownloadIcon(); | 61 void DownloadIcon(); |
63 | 62 |
64 // FaviconDownloadHelperDelegate override. | 63 // content::WebContentsDelegate override. |
jam
2012/11/27 01:16:31
nit: content::FaviconDownloadDelegate
Cait (Slow)
2012/11/28 00:11:45
Done.
| |
65 virtual void OnDidDownloadFavicon( | 64 virtual void DidDownloadFavicon( |
66 int id, | 65 int id, |
67 const GURL& image_url, | 66 const GURL& image_url, |
68 bool errored, | 67 bool errored, |
69 int requested_size, | 68 int requested_size, |
70 const std::vector<SkBitmap>& bitmaps) OVERRIDE; | 69 const std::vector<SkBitmap>& bitmaps) OVERRIDE; |
71 | 70 |
72 // Checks if shortcuts exists on desktop, start menu and quick launch. | 71 // Checks if shortcuts exists on desktop, start menu and quick launch. |
73 void CheckExistingShortcuts(); | 72 void CheckExistingShortcuts(); |
74 | 73 |
75 // Update shortcut files and icons. | 74 // Update shortcut files and icons. |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
114 extensions::TabHelper::FromWebContents(web_contents); | 113 extensions::TabHelper::FromWebContents(web_contents); |
115 web_app::GetShortcutInfoForTab(web_contents_, &shortcut_info_); | 114 web_app::GetShortcutInfoForTab(web_contents_, &shortcut_info_); |
116 web_app::GetIconsInfo(extensions_tab_helper->web_app_info(), | 115 web_app::GetIconsInfo(extensions_tab_helper->web_app_info(), |
117 &unprocessed_icons_); | 116 &unprocessed_icons_); |
118 file_name_ = web_app::internals::GetSanitizedFileName(shortcut_info_.title); | 117 file_name_ = web_app::internals::GetSanitizedFileName(shortcut_info_.title); |
119 | 118 |
120 registrar_.Add( | 119 registrar_.Add( |
121 this, | 120 this, |
122 chrome::NOTIFICATION_TAB_CLOSING, | 121 chrome::NOTIFICATION_TAB_CLOSING, |
123 content::Source<NavigationController>(&web_contents->GetController())); | 122 content::Source<NavigationController>(&web_contents->GetController())); |
123 web_contents_->RegisterFaviconDelegate(this); | |
124 } | 124 } |
125 | 125 |
126 void UpdateShortcutWorker::Run() { | 126 void UpdateShortcutWorker::Run() { |
127 // Starting by downloading app icon. | 127 // Starting by downloading app icon. |
128 DownloadIcon(); | 128 DownloadIcon(); |
129 } | 129 } |
130 | 130 |
131 void UpdateShortcutWorker::Observe( | 131 void UpdateShortcutWorker::Observe( |
132 int type, | 132 int type, |
133 const content::NotificationSource& source, | 133 const content::NotificationSource& source, |
(...skipping 15 matching lines...) Expand all Loading... | |
149 DeleteMe(); // We are done if underlying WebContents is gone. | 149 DeleteMe(); // We are done if underlying WebContents is gone. |
150 return; | 150 return; |
151 } | 151 } |
152 | 152 |
153 if (unprocessed_icons_.empty()) { | 153 if (unprocessed_icons_.empty()) { |
154 // No app icon. Just use the favicon from WebContents. | 154 // No app icon. Just use the favicon from WebContents. |
155 UpdateShortcuts(); | 155 UpdateShortcuts(); |
156 return; | 156 return; |
157 } | 157 } |
158 | 158 |
159 scoped_ptr<FaviconDownloadHelper> download_helper( | 159 web_contents_->DownloadFavicon(unprocessed_icons_.back().url, |
160 new FaviconDownloadHelper(web_contents_, this)); | 160 std::max(unprocessed_icons_.back().width, |
161 | 161 unprocessed_icons_.back().height)); |
162 download_helper->DownloadFavicon(unprocessed_icons_.back().url, | |
163 std::max(unprocessed_icons_.back().width, | |
164 unprocessed_icons_.back().height)); | |
165 unprocessed_icons_.pop_back(); | 162 unprocessed_icons_.pop_back(); |
166 } | 163 } |
167 | 164 |
168 void UpdateShortcutWorker::OnDidDownloadFavicon( | 165 void UpdateShortcutWorker::DidDownloadFavicon( |
169 int id, | 166 int id, |
170 const GURL& image_url, | 167 const GURL& image_url, |
171 bool errored, | 168 bool errored, |
172 int requested_size, | 169 int requested_size, |
173 const std::vector<SkBitmap>& bitmaps) { | 170 const std::vector<SkBitmap>& bitmaps) { |
174 if (web_contents_ == NULL) { | 171 if (web_contents_ == NULL) { |
175 DeleteMe(); // We are done if underlying WebContents is gone. | 172 DeleteMe(); // We are done if underlying WebContents is gone. |
jam
2012/11/27 01:16:31
this can't happen anymore right?
Cait (Slow)
2012/11/28 00:11:45
Done.
| |
176 return; | 173 return; |
177 } | 174 } |
178 | 175 |
179 std::vector<ui::ScaleFactor> scale_factors; | 176 std::vector<ui::ScaleFactor> scale_factors; |
180 scale_factors.push_back(ui::SCALE_FACTOR_100P); | 177 scale_factors.push_back(ui::SCALE_FACTOR_100P); |
181 | 178 |
182 size_t closest_index = | 179 size_t closest_index = |
183 FaviconUtil::SelectBestFaviconFromBitmaps(bitmaps, | 180 FaviconUtil::SelectBestFaviconFromBitmaps(bitmaps, |
184 scale_factors, | 181 scale_factors, |
185 requested_size); | 182 requested_size); |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
347 | 344 |
348 void UpdateShortcutForTabContents(WebContents* web_contents) { | 345 void UpdateShortcutForTabContents(WebContents* web_contents) { |
349 #if defined(OS_WIN) | 346 #if defined(OS_WIN) |
350 // UpdateShortcutWorker will delete itself when it's done. | 347 // UpdateShortcutWorker will delete itself when it's done. |
351 UpdateShortcutWorker* worker = new UpdateShortcutWorker(web_contents); | 348 UpdateShortcutWorker* worker = new UpdateShortcutWorker(web_contents); |
352 worker->Run(); | 349 worker->Run(); |
353 #endif // defined(OS_WIN) | 350 #endif // defined(OS_WIN) |
354 } | 351 } |
355 | 352 |
356 } // namespace web_app | 353 } // namespace web_app |
OLD | NEW |