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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_DOWNLOAD_NOTIFICATION_DOWNLOAD_ITEM_NOTIFICATION_H_
6 #define CHROME_BROWSER_DOWNLOAD_NOTIFICATION_DOWNLOAD_ITEM_NOTIFICATION_H_
8 #include "base/strings/string_number_conversions.h"
9 #include "chrome/browser/download/download_commands.h"
10 #include "chrome/browser/download/notification/download_notification.h"
11 #include "chrome/browser/image_decoder.h"
12 #include "chrome/browser/notifications/notification.h"
13 #include "chrome/browser/notifications/notification_delegate.h"
14 #include "chrome/browser/notifications/notification_test_util.h"
15 #include "content/public/browser/download_item.h"
16 #include "grit/theme_resources.h"
17 #include "third_party/skia/include/core/SkBitmap.h"
18 #include "ui/message_center/message_center.h"
19 #include "ui/message_center/message_center_observer.h"
21 #if !defined(OS_MACOSX)
22 #include "ui/native_theme/native_theme.h"
26 class DownloadItemNotificationTest
;
30 enum class VectorIconId
;
33 class DownloadItemNotification
: public DownloadNotification
,
34 public ImageDecoder::ImageRequest
{
36 DownloadItemNotification(content::DownloadItem
* item
,
37 DownloadNotificationManagerForProfile
* manager
);
39 ~DownloadItemNotification() override
;
41 // Methods called from NotificationWatcher.
42 void OnDownloadUpdated(content::DownloadItem
* item
) override
;
43 void OnDownloadRemoved(content::DownloadItem
* item
) override
;
44 bool HasNotificationClickedListener() override
;
45 void OnNotificationClose() override
;
46 void OnNotificationClick() override
;
47 void OnNotificationButtonClick(int button_index
) override
;
48 std::string
GetNotificationId() const override
;
51 friend class test::DownloadItemNotificationTest
;
53 enum ImageDecodeStatus
{ NOT_STARTED
, IN_PROGRESS
, DONE
, FAILED
, NOT_IMAGE
};
55 enum NotificationUpdateType
{
61 void CloseNotificationByUser();
62 void CloseNotificationByNonUser();
64 void UpdateNotificationData(NotificationUpdateType type
);
65 void UpdateNotificationIcon();
67 // Set icon of the notification.
68 void SetNotificationIcon(int resource_id
);
70 #if !defined(OS_MACOSX)
71 void SetNotificationVectorIcon(gfx::VectorIconId id
,
72 ui::NativeTheme::ColorId color
);
75 // Set preview image of the notification. Must be called on IO thread.
76 void OnImageLoaded(const std::string
& image_data
);
77 void OnImageCropped(const SkBitmap
& image
);
79 // ImageDecoder::ImageRequest overrides:
80 void OnImageDecoded(const SkBitmap
& decoded_image
) override
;
81 void OnDecodeImageFailed() override
;
83 // Returns a short one-line status string for the download.
84 base::string16
GetTitle() const;
86 // Returns a short one-line status string for a download command.
87 base::string16
GetCommandLabel(DownloadCommands::Command command
) const;
89 // Get the warning text to notify a dangerous download. Should only be called
90 // if IsDangerous() is true.
91 base::string16
GetWarningStatusString() const;
93 // Get the sub status text of the current in-progress download status. Should
94 // be called only for downloads in progress.
95 base::string16
GetInProgressSubStatusString() const;
97 // Get the status text.
98 base::string16
GetStatusString() const;
100 bool IsNotificationVisible() const;
102 Browser
* GetBrowser() const;
103 Profile
* profile() const;
105 // Returns the list of possible extra (all except the default) actions.
106 scoped_ptr
<std::vector
<DownloadCommands::Command
>> GetExtraActions() const;
108 // Flag to show the notification on next update. If true, the notification
109 // goes visible. The initial value is true so it gets shown on initial update.
110 bool show_next_
= true;
111 // Current vilibility status of the notification.
112 bool visible_
= false;
114 int image_resource_id_
= 0;
115 #if !defined(OS_MACOSX)
116 std::pair
<gfx::VectorIconId
, ui::NativeTheme::ColorId
> vector_icon_params_
;
118 content::DownloadItem::DownloadState previous_download_state_
=
119 content::DownloadItem::MAX_DOWNLOAD_STATE
; // As uninitialized state
120 bool previous_dangerous_state_
= false;
121 scoped_ptr
<Notification
> notification_
;
122 content::DownloadItem
* item_
;
123 scoped_ptr
<std::vector
<DownloadCommands::Command
>> button_actions_
;
125 // Status of the preview image decode.
126 ImageDecodeStatus image_decode_status_
= NOT_STARTED
;
128 base::WeakPtrFactory
<DownloadItemNotification
> weak_factory_
;
130 DISALLOW_COPY_AND_ASSIGN(DownloadItemNotification
);
133 #endif // CHROME_BROWSER_DOWNLOAD_NOTIFICATION_DOWNLOAD_ITEM_NOTIFICATION_H_