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   uint32 GetNextDownloadId();
     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 add mime types to the downloads table.
     56   bool MigrateMimeType();
     57 
     58   // Returns true if able to successfully rewrite the invalid values for the
     59   // |state| field from 3 to 4. Returns false if there was an error fixing the
     60   // database. See http://crbug.com/140687
     61   bool MigrateDownloadsState();
     62 
     63   // Returns true if able to successfully add the last interrupt reason and the
     64   // two target paths to downloads.
     65   bool MigrateDownloadsReasonPathsAndDangerType();
     66 
     67   // Returns true if able to successfully add the referrer column to the
     68   // downloads table.
     69   bool MigrateReferrer();
     70 
     71   // Returns true if able to successfully add the by_ext_id and by_ext_name
     72   // columns to the downloads table.
     73   bool MigrateDownloadedByExtension();
     74 
     75   // Returns true if able to successfully add the etag and last-modified columns
     76   // to the downloads table.
     77   bool MigrateDownloadValidators();
     78 
     79   // Creates the downloads table if needed.
     80   bool InitDownloadTable();
     81 
     82   // Used to quickly clear the downloads. First you would drop it, then you
     83   // would re-initialize it.
     84   bool DropDownloadTable();
     85 
     86  private:
     87   FRIEND_TEST_ALL_PREFIXES(
     88       HistoryBackendDBTest, ConfirmDownloadInProgressCleanup);
     89 
     90   // Values used in the database for DownloadItem::State.
     91   static const int kStateInvalid;
     92   static const int kStateInProgress;
     93   static const int kStateComplete;
     94   static const int kStateCancelled;
     95   static const int kStateBug140687;
     96   static const int kStateInterrupted;
     97 
     98   // Values used in the database for DownloadItem::DangerType
     99   static const int kDangerTypeInvalid;
    100   static const int kDangerTypeNotDangerous;
    101   static const int kDangerTypeDangerousFile;
    102   static const int kDangerTypeDangerousUrl;
    103   static const int kDangerTypeDangerousContent;
    104   static const int kDangerTypeMaybeDangerousContent;
    105   static const int kDangerTypeUncommonContent;
    106   static const int kDangerTypeUserValidated;
    107   static const int kDangerTypeDangerousHost;
    108   static const int kDangerTypePotentiallyUnwanted;
    109 
    110   // Fixes state of the download entries. Sometimes entries with IN_PROGRESS
    111   // state are not updated during browser shutdown (particularly when crashing).
    112   // On the next start such entries are considered interrupted with
    113   // interrupt reason |DOWNLOAD_INTERRUPT_REASON_CRASH|.  This function
    114   // fixes such entries.
    115   void EnsureInProgressEntriesCleanedUp();
    116 
    117   bool EnsureColumnExists(const std::string& name, const std::string& type);
    118 
    119   void RemoveDownloadURLs(uint32 id);
    120 
    121   // Utility functions for conversion between DownloadItem types
    122   // and DownloadDatabase constants.
    123   static int StateToInt(content::DownloadItem::DownloadState state);
    124   static content::DownloadItem::DownloadState IntToState(int state);
    125   static int DangerTypeToInt(content::DownloadDangerType danger_type);
    126   static content::DownloadDangerType IntToDangerType(int danger_type);
    127 
    128   bool owning_thread_set_;
    129   base::PlatformThreadId owning_thread_;
    130 
    131   // Initialized to false on construction, and checked in all functional
    132   // routines post-migration in the database for a possible call to
    133   // CleanUpInProgressEntries().  This allows us to avoid
    134   // doing the cleanup until after any DB migration and unless we are
    135   // actually use the downloads database.
    136   bool in_progress_entry_cleanup_completed_;
    137 
    138   DISALLOW_COPY_AND_ASSIGN(DownloadDatabase);
    139 };
    140 
    141 }  // namespace history
    142 
    143 #endif  // CHROME_BROWSER_HISTORY_DOWNLOAD_DATABASE_H_
    144