Home | History | Annotate | Download | only in history
      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
      3 // found in the LICENSE file.
      4 
      5 #ifndef CHROME_BROWSER_HISTORY_DOWNLOAD_DATABASE_H_
      6 #define CHROME_BROWSER_HISTORY_DOWNLOAD_DATABASE_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/gtest_prod_util.h"
     12 #include "base/threading/platform_thread.h"
     13 #include "content/public/browser/download_item.h"
     14 #include "sql/meta_table.h"
     15 
     16 namespace sql {
     17 class Connection;
     18 }
     19 
     20 namespace history {
     21 
     22 struct DownloadRow;
     23 
     24 // Maintains a table of downloads.
     25 class DownloadDatabase {
     26  public:
     27   // Must call InitDownloadTable before using any other functions.
     28   DownloadDatabase();
     29   virtual ~DownloadDatabase();
     30 
     31   void GetNextDownloadId(uint32* id);
     32 
     33   // Get all the downloads from the database.
     34   void QueryDownloads(
     35       std::vector<DownloadRow>* results);
     36 
     37   // Update the state of one download. Returns true if successful.
     38   // Does not update |url|, |start_time|; uses |id| only
     39   // to select the row in the database table to update.
     40   bool UpdateDownload(const DownloadRow& data);
     41 
     42   // Create a new database entry for one download and return true if the
     43   // creation succeeded, false otherwise.
     44   bool CreateDownload(const DownloadRow& info);
     45 
     46   // Remove |id| from the database.
     47   void RemoveDownload(uint32 id);
     48 
     49   size_t CountDownloads();
     50 
     51  protected:
     52   // Returns the database for the functions in this interface.
     53   virtual sql::Connection& GetDB() = 0;
     54 
     55   // Returns true if able to successfully rewrite the invalid values for the
     56   // |state| field from 3 to 4. Returns false if there was an error fixing the
     57   // database. See http://crbug.com/140687
     58   bool MigrateDownloadsState();
     59 
     60   // Returns true if able to successfully add the last interrupt reason and the
     61   // two target paths to downloads.
     62   bool MigrateDownloadsReasonPathsAndDangerType();
     63 
     64   // Returns true if able to successfully add the referrer column to the
     65   // downloads table.
     66   bool MigrateReferrer();
     67 
     68   // Returns true if able to successfully add the by_ext_id and by_ext_name
     69   // columns to the downloads table.
     70   bool MigrateDownloadedByExtension();
     71 
     72   // Returns true if able to successfully add the etag and last-modified columns
     73   // to the downloads table.
     74   bool MigrateDownloadValidators();
     75 
     76   // Creates the downloads table if needed.
     77   bool InitDownloadTable();
     78 
     79   // Used to quickly clear the downloads. First you would drop it, then you
     80   // would re-initialize it.
     81   bool DropDownloadTable();
     82 
     83  private:
     84   FRIEND_TEST_ALL_PREFIXES(
     85       HistoryBackendDBTest, ConfirmDownloadInProgressCleanup);
     86 
     87   // Values used in the database for DownloadItem::State.
     88   static const int kStateInvalid;
     89   static const int kStateInProgress;
     90   static const int kStateComplete;
     91   static const int kStateCancelled;
     92   static const int kStateBug140687;
     93   static const int kStateInterrupted;
     94 
     95   // Values used in the database for DownloadItem::DangerType
     96   static const int kDangerTypeInvalid;
     97   static const int kDangerTypeNotDangerous;
     98   static const int kDangerTypeDangerousFile;
     99   static const int kDangerTypeDangerousUrl;
    100   static const int kDangerTypeDangerousContent;
    101   static const int kDangerTypeMaybeDangerousContent;
    102   static const int kDangerTypeUncommonContent;
    103   static const int kDangerTypeUserValidated;
    104   static const int kDangerTypeDangerousHost;
    105   static const int kDangerTypePotentiallyUnwanted;
    106 
    107   // Fixes state of the download entries. Sometimes entries with IN_PROGRESS
    108   // state are not updated during browser shutdown (particularly when crashing).
    109   // On the next start such entries are considered interrupted with
    110   // interrupt reason |DOWNLOAD_INTERRUPT_REASON_CRASH|.  This function
    111   // fixes such entries.
    112   void EnsureInProgressEntriesCleanedUp();
    113 
    114   bool EnsureColumnExists(const std::string& name, const std::string& type);
    115 
    116   void RemoveDownloadURLs(uint32 id);
    117 
    118   // Utility functions for conversion between DownloadItem types
    119   // and DownloadDatabase constants.
    120   static int StateToInt(content::DownloadItem::DownloadState state);
    121   static content::DownloadItem::DownloadState IntToState(int state);
    122   static int DangerTypeToInt(content::DownloadDangerType danger_type);
    123   static content::DownloadDangerType IntToDangerType(int danger_type);
    124 
    125   bool owning_thread_set_;
    126   base::PlatformThreadId owning_thread_;
    127 
    128   // Initialized to false on construction, and checked in all functional
    129   // routines post-migration in the database for a possible call to
    130   // CleanUpInProgressEntries().  This allows us to avoid
    131   // doing the cleanup until after any DB migration and unless we are
    132   // actually use the downloads database.
    133   bool in_progress_entry_cleanup_completed_;
    134 
    135   DISALLOW_COPY_AND_ASSIGN(DownloadDatabase);
    136 };
    137 
    138 }  // namespace history
    139 
    140 #endif  // CHROME_BROWSER_HISTORY_DOWNLOAD_DATABASE_H_
    141