OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 #ifndef CHROME_BROWSER_DOWNLOAD_NOTIFICATION_DOWNLOAD_NOTIFICATION_ITEM_H_ | 5 #ifndef CHROME_BROWSER_DOWNLOAD_NOTIFICATION_DOWNLOAD_NOTIFICATION_ITEM_H_ |
6 #define CHROME_BROWSER_DOWNLOAD_NOTIFICATION_DOWNLOAD_NOTIFICATION_ITEM_H_ | 6 #define CHROME_BROWSER_DOWNLOAD_NOTIFICATION_DOWNLOAD_NOTIFICATION_ITEM_H_ |
7 | 7 |
8 #include "chrome/browser/download/notification/download_notification_item.h" | 8 #include "chrome/browser/download/notification/download_notification_item.h" |
9 | 9 |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
11 #include "chrome/browser/download/download_commands.h" | 11 #include "chrome/browser/download/download_commands.h" |
| 12 #include "chrome/browser/image_decoder.h" |
12 #include "chrome/browser/notifications/notification.h" | 13 #include "chrome/browser/notifications/notification.h" |
13 #include "chrome/browser/notifications/notification_delegate.h" | 14 #include "chrome/browser/notifications/notification_delegate.h" |
14 #include "chrome/browser/notifications/notification_test_util.h" | 15 #include "chrome/browser/notifications/notification_test_util.h" |
15 #include "content/public/browser/download_item.h" | 16 #include "content/public/browser/download_item.h" |
16 #include "grit/theme_resources.h" | 17 #include "grit/theme_resources.h" |
| 18 #include "third_party/skia/include/core/SkBitmap.h" |
17 #include "ui/message_center/message_center.h" | 19 #include "ui/message_center/message_center.h" |
18 #include "ui/message_center/message_center_observer.h" | 20 #include "ui/message_center/message_center_observer.h" |
19 | 21 |
20 namespace test { | 22 namespace test { |
21 class DownloadNotificationItemTest; | 23 class DownloadNotificationItemTest; |
22 } | 24 } |
23 | 25 |
24 class DownloadNotificationItem : public content::DownloadItem::Observer { | 26 class DownloadNotificationItem : public content::DownloadItem::Observer, |
| 27 public ImageDecoder::ImageRequest { |
25 public: | 28 public: |
26 class Delegate { | 29 class Delegate { |
27 public: | 30 public: |
28 virtual void OnCreated(DownloadNotificationItem* item) = 0; | 31 virtual void OnCreated(DownloadNotificationItem* item) = 0; |
29 virtual void OnDownloadStarted(DownloadNotificationItem* item) = 0; | 32 virtual void OnDownloadStarted(DownloadNotificationItem* item) = 0; |
30 virtual void OnDownloadStopped(DownloadNotificationItem* item) = 0; | 33 virtual void OnDownloadStopped(DownloadNotificationItem* item) = 0; |
31 virtual void OnDownloadRemoved(DownloadNotificationItem* item) = 0; | 34 virtual void OnDownloadRemoved(DownloadNotificationItem* item) = 0; |
32 }; | 35 }; |
33 | 36 |
34 static const char kDownloadNotificationOrigin[]; | 37 static const char kDownloadNotificationOrigin[]; |
35 | 38 |
36 DownloadNotificationItem(content::DownloadItem* item, | 39 DownloadNotificationItem(content::DownloadItem* item, |
37 Profile* profile, | 40 Profile* profile, |
38 Delegate* delegate); | 41 Delegate* delegate); |
39 | 42 |
40 ~DownloadNotificationItem() override; | 43 ~DownloadNotificationItem() override; |
41 | 44 |
42 private: | 45 private: |
43 friend class test::DownloadNotificationItemTest; | 46 friend class test::DownloadNotificationItemTest; |
44 | 47 |
45 enum NotificationUpdateType { | 48 enum NotificationUpdateType { |
46 ADD_NEW, | 49 ADD_NEW, |
47 UPDATE_EXISTING | 50 UPDATE_EXISTING |
48 }; | 51 }; |
49 | 52 |
| 53 enum ImageDecodeStatus { |
| 54 NOT_STARTED, |
| 55 IN_PROGRESS, |
| 56 DONE, |
| 57 FAILED, |
| 58 NOT_IMAGE |
| 59 }; |
| 60 |
50 class NotificationWatcher : public NotificationDelegate { | 61 class NotificationWatcher : public NotificationDelegate { |
51 public: | 62 public: |
52 explicit NotificationWatcher(DownloadNotificationItem* item); | 63 explicit NotificationWatcher(DownloadNotificationItem* item); |
53 | 64 |
54 // NotificationDelegate overrides: | 65 // NotificationDelegate overrides: |
55 void Close(bool by_user) override; | 66 void Close(bool by_user) override; |
56 void Click() override; | 67 void Click() override; |
57 bool HasClickedListener() override; | 68 bool HasClickedListener() override; |
58 void ButtonClick(int button_index) override; | 69 void ButtonClick(int button_index) override; |
59 std::string id() const override; | 70 std::string id() const override; |
60 | 71 |
61 private: | 72 private: |
62 ~NotificationWatcher() override; | 73 ~NotificationWatcher() override; |
63 | 74 |
64 DownloadNotificationItem* item_; | 75 DownloadNotificationItem* item_; |
65 }; | 76 }; |
66 | 77 |
67 // For testing. This is set by SetStubNotificationUIManagerForTesting method. | 78 // For testing. This is set by SetStubNotificationUIManagerForTesting method. |
68 static StubNotificationUIManager* stub_notification_ui_manager_for_testing_; | 79 static StubNotificationUIManager* stub_notification_ui_manager_for_testing_; |
69 | 80 |
70 // Replaces the NotificationUIManager for tests. | 81 // Replaces the NotificationUIManager for tests. |
71 static void SetStubNotificationUIManagerForTesting( | 82 static void SetStubNotificationUIManagerForTesting( |
72 StubNotificationUIManager* stub_notification_ui_manager) { | 83 StubNotificationUIManager* stub_notification_ui_manager) { |
73 stub_notification_ui_manager_for_testing_ = stub_notification_ui_manager; | 84 stub_notification_ui_manager_for_testing_ = stub_notification_ui_manager; |
74 } | 85 } |
75 | 86 |
76 // Methods called from NotificationWatcher. | 87 // Methods called from NotificationWatcher. |
77 void OnNotificationClick(); | 88 void OnNotificationClick(); |
78 void OnNotificationButtonClick(int button_index); | 89 void OnNotificationButtonClick(int button_index); |
| 90 void OnNotificationClose(bool by_user); |
79 | 91 |
80 // DownloadItem::Observer overrides: | 92 // DownloadItem::Observer overrides: |
81 void OnDownloadUpdated(content::DownloadItem* item) override; | 93 void OnDownloadUpdated(content::DownloadItem* item) override; |
82 void OnDownloadOpened(content::DownloadItem* item) override; | 94 void OnDownloadOpened(content::DownloadItem* item) override; |
83 void OnDownloadRemoved(content::DownloadItem* item) override; | 95 void OnDownloadRemoved(content::DownloadItem* item) override; |
84 void OnDownloadDestroyed(content::DownloadItem* item) override; | 96 void OnDownloadDestroyed(content::DownloadItem* item) override; |
85 | 97 |
86 void CloseNotificationByUser(); | 98 void CloseNotificationByUser(); |
87 void CloseNotificationByNonUser(); | 99 void CloseNotificationByNonUser(); |
88 void UpdateNotificationData(NotificationUpdateType type); | 100 void UpdateNotificationData(NotificationUpdateType type); |
89 void SetNotificationImage(int resource_id); | 101 |
| 102 // Set icon of the notification. |
| 103 void SetNotificationIcon(int resource_id); |
| 104 |
| 105 // ImageDecoder::ImageRequest overrides: |
| 106 void OnImageDecoded(const SkBitmap& decoded_image) override; |
| 107 void OnDecodeImageFailed() override; |
90 | 108 |
91 NotificationUIManager* notification_ui_manager() const; | 109 NotificationUIManager* notification_ui_manager() const; |
92 | 110 |
93 // Returns a short one-line status string for the download. | 111 // Returns a short one-line status string for the download. |
94 base::string16 GetTitle() const; | 112 base::string16 GetTitle() const; |
95 | 113 |
96 // Returns a short one-line status string for a download command. | 114 // Returns a short one-line status string for a download command. |
97 base::string16 GetCommandLabel(DownloadCommands::Command command) const; | 115 base::string16 GetCommandLabel(DownloadCommands::Command command) const; |
98 | 116 |
99 // Get the warning test to notify a dangerous download. Should only be called | 117 // Get the warning test to notify a dangerous download. Should only be called |
100 // if IsDangerous() is true. | 118 // if IsDangerous() is true. |
101 base::string16 GetWarningText() const; | 119 base::string16 GetWarningText() const; |
102 | 120 |
103 Browser* GetBrowser(); | 121 Browser* GetBrowser(); |
104 | 122 |
105 // Returns the list of possible extra (all except the default) actions. | 123 // Returns the list of possible extra (all except the default) actions. |
106 scoped_ptr<std::vector<DownloadCommands::Command>> GetExtraActions() const; | 124 scoped_ptr<std::vector<DownloadCommands::Command>> GetExtraActions() const; |
107 | 125 |
108 int image_resource_id_ = 0; | 126 int image_resource_id_ = 0; |
109 content::DownloadItem::DownloadState previous_download_state_ = | 127 content::DownloadItem::DownloadState previous_download_state_ = |
110 content::DownloadItem::MAX_DOWNLOAD_STATE; // As uninitialized state | 128 content::DownloadItem::MAX_DOWNLOAD_STATE; // As uninitialized state |
111 Profile* profile_; | 129 Profile* profile_; |
112 scoped_refptr<NotificationWatcher> watcher_; | 130 scoped_refptr<NotificationWatcher> watcher_; |
113 scoped_ptr<Notification> notification_; | 131 scoped_ptr<Notification> notification_; |
114 content::DownloadItem* item_; | 132 content::DownloadItem* item_; |
115 scoped_ptr<std::vector<DownloadCommands::Command>> button_actions_; | 133 scoped_ptr<std::vector<DownloadCommands::Command>> button_actions_; |
116 Delegate* const delegate_; | 134 Delegate* const delegate_; |
117 | 135 |
| 136 // Status of the preview image decode. |
| 137 ImageDecodeStatus image_decode_status_ = NOT_STARTED; |
| 138 |
| 139 base::WeakPtrFactory<DownloadNotificationItem> weak_factory_; |
| 140 |
118 DISALLOW_COPY_AND_ASSIGN(DownloadNotificationItem); | 141 DISALLOW_COPY_AND_ASSIGN(DownloadNotificationItem); |
119 }; | 142 }; |
120 | 143 |
121 #endif // CHROME_BROWSER_DOWNLOAD_NOTIFICATION_DOWNLOAD_NOTIFICATION_ITEM_H_ | 144 #endif // CHROME_BROWSER_DOWNLOAD_NOTIFICATION_DOWNLOAD_NOTIFICATION_ITEM_H_ |
OLD | NEW |