 Chromium Code Reviews
 Chromium Code Reviews Issue 209613002:
  Download shelf autohides on showing in shell, just same as regular open 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master
    
  
    Issue 209613002:
  Download shelf autohides on showing in shell, just same as regular open 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master| Index: chrome/browser/download/download_item_model.cc | 
| diff --git a/chrome/browser/download/download_item_model.cc b/chrome/browser/download/download_item_model.cc | 
| index 171d3b00dd6d990ef0dc754e15ae5cfa66250539..0cd0e3e9aebfe2d6f7abc5abcf0361d97d84c535 100644 | 
| --- a/chrome/browser/download/download_item_model.cc | 
| +++ b/chrome/browser/download/download_item_model.cc | 
| @@ -38,7 +38,8 @@ namespace { | 
| // Per DownloadItem data used by DownloadItemModel. The model doesn't keep any | 
| // state since there could be multiple models associated with a single | 
| // DownloadItem, and the lifetime of the model is shorter than the DownloadItem. | 
| -class DownloadItemModelData : public base::SupportsUserData::Data { | 
| +class DownloadItemModelData : public base::SupportsUserData::Data, | 
| + public DownloadItem::Observer { | 
| 
asanka
2014/06/12 21:08:47
Let's try to avoid having the DownloadItemModelDat
 | 
| public: | 
| // Get the DownloadItemModelData object for |download|. Returns NULL if | 
| // there's no model data. | 
| @@ -65,12 +66,27 @@ class DownloadItemModelData : public base::SupportsUserData::Data { | 
| should_prefer_opening_in_browser_ = preference; | 
| } | 
| + bool user_acted() const { | 
| + return user_acted_; | 
| + } | 
| + void set_user_acted(bool acted) { | 
| + user_acted_ = acted; | 
| + } | 
| + | 
| private: | 
| - DownloadItemModelData(); | 
| - virtual ~DownloadItemModelData() {} | 
| + virtual void OnDownloadOpened(DownloadItem* download) OVERRIDE; | 
| + virtual void OnDownloadShown(DownloadItem* download) OVERRIDE; | 
| + virtual void OnDownloadDestroyed(DownloadItem* download) OVERRIDE; | 
| + | 
| + private: | 
| + explicit DownloadItemModelData(DownloadItem* download); | 
| + virtual ~DownloadItemModelData(); | 
| static const char kKey[]; | 
| + // Download item that holds this data. | 
| + DownloadItem* download_; | 
| + | 
| // Whether the download should be displayed in the download shelf. True by | 
| // default. | 
| bool should_show_in_shelf_; | 
| @@ -81,6 +97,9 @@ class DownloadItemModelData : public base::SupportsUserData::Data { | 
| // Whether the download should be opened in the browser vs. the system handler | 
| // for the file type. | 
| bool should_prefer_opening_in_browser_; | 
| + | 
| + // Whether user acted on this item, so that download shelf can autohide. | 
| + bool user_acted_; | 
| }; | 
| // static | 
| @@ -98,16 +117,53 @@ DownloadItemModelData* DownloadItemModelData::GetOrCreate( | 
| DownloadItemModelData* data = | 
| static_cast<DownloadItemModelData*>(download->GetUserData(kKey)); | 
| if (data == NULL) { | 
| - data = new DownloadItemModelData(); | 
| + data = new DownloadItemModelData(download); | 
| download->SetUserData(kKey, data); | 
| } | 
| return data; | 
| } | 
| -DownloadItemModelData::DownloadItemModelData() | 
| - : should_show_in_shelf_(true), | 
| +DownloadItemModelData::DownloadItemModelData(DownloadItem* download) | 
| + : download_(download), | 
| + should_show_in_shelf_(true), | 
| was_ui_notified_(false), | 
| - should_prefer_opening_in_browser_(false) { | 
| + should_prefer_opening_in_browser_(false), | 
| + user_acted_(false) { | 
| + DCHECK(download); | 
| + | 
| + // As |download| might be loaded from history, update our own state. | 
| + user_acted_ = download->GetOpened(); | 
| + download_->AddObserver(this); | 
| +} | 
| + | 
| +DownloadItemModelData::~DownloadItemModelData() { | 
| + if (download_) | 
| + download_->RemoveObserver(this); | 
| +} | 
| + | 
| +void DownloadItemModelData::OnDownloadOpened(DownloadItem* download) { | 
| + DCHECK_EQ(download_, download); | 
| + user_acted_ = true; | 
| +} | 
| + | 
| +void DownloadItemModelData::OnDownloadShown(DownloadItem* download) { | 
| + DCHECK_EQ(download_, download); | 
| + | 
| + // The user can show download in shell even when it is not yet completed, | 
| + // as this flag is used to 'hide' the download item from user's sight, it is | 
| + // fair to do so only for completed downloads. | 
| + if (download->GetState() == DownloadItem::COMPLETE) { | 
| + user_acted_ = true; | 
| + } | 
| +} | 
| + | 
| +void DownloadItemModelData::OnDownloadDestroyed(DownloadItem* download) { | 
| + DCHECK_EQ(download_, download); | 
| + | 
| + // This is called from DownloadItem's destructor - so that | 
| + // base::SupportsUserData would destroy this model. | 
| + download_->RemoveObserver(this); | 
| + download_ = NULL; | 
| } | 
| base::string16 InterruptReasonStatusMessage(int reason) { | 
| @@ -690,3 +746,13 @@ void DownloadItemModel::OpenUsingPlatformHandler() { | 
| delegate->OpenDownloadUsingPlatformHandler(download_); | 
| RecordDownloadOpenMethod(DOWNLOAD_OPEN_METHOD_USER_PLATFORM); | 
| } | 
| + | 
| +void DownloadItemModel::SetUserActed(bool acted) { | 
| + DownloadItemModelData* data = DownloadItemModelData::GetOrCreate(download_); | 
| + data->set_user_acted(acted); | 
| +} | 
| + | 
| +bool DownloadItemModel::GetUserActed() const { | 
| + DownloadItemModelData* data = DownloadItemModelData::GetOrCreate(download_); | 
| + return data->user_acted(); | 
| +} |