Home | History | Annotate | Download | only in fileapi
      1 // Copyright 2013 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 #include "webkit/browser/fileapi/sandbox_isolated_origin_database.h"
      6 
      7 #include "base/file_util.h"
      8 #include "base/logging.h"
      9 #include "webkit/browser/fileapi/sandbox_origin_database.h"
     10 
     11 namespace fileapi {
     12 
     13 // Special directory name for isolated origin.
     14 const base::FilePath::CharType
     15 SandboxIsolatedOriginDatabase::kOriginDirectory[] = FILE_PATH_LITERAL("iso");
     16 
     17 SandboxIsolatedOriginDatabase::SandboxIsolatedOriginDatabase(
     18     const std::string& origin,
     19     const base::FilePath& file_system_directory)
     20     : migration_checked_(false),
     21       origin_(origin),
     22       file_system_directory_(file_system_directory) {
     23 }
     24 
     25 SandboxIsolatedOriginDatabase::~SandboxIsolatedOriginDatabase() {
     26 }
     27 
     28 bool SandboxIsolatedOriginDatabase::HasOriginPath(
     29     const std::string& origin) {
     30   MigrateDatabaseIfNeeded();
     31   return (origin_ == origin);
     32 }
     33 
     34 bool SandboxIsolatedOriginDatabase::GetPathForOrigin(
     35     const std::string& origin, base::FilePath* directory) {
     36   MigrateDatabaseIfNeeded();
     37   if (origin != origin_)
     38     return false;
     39   *directory = base::FilePath(kOriginDirectory);
     40   return true;
     41 }
     42 
     43 bool SandboxIsolatedOriginDatabase::RemovePathForOrigin(
     44     const std::string& origin) {
     45   return true;
     46 }
     47 
     48 bool SandboxIsolatedOriginDatabase::ListAllOrigins(
     49     std::vector<OriginRecord>* origins) {
     50   MigrateDatabaseIfNeeded();
     51   origins->push_back(OriginRecord(origin_, base::FilePath(kOriginDirectory)));
     52   return true;
     53 }
     54 
     55 void SandboxIsolatedOriginDatabase::DropDatabase() {
     56 }
     57 
     58 void SandboxIsolatedOriginDatabase::MigrateBackDatabase(
     59     const std::string& origin,
     60     const base::FilePath& file_system_directory,
     61     SandboxOriginDatabase* database) {
     62   base::FilePath isolated_directory =
     63       file_system_directory.Append(kOriginDirectory);
     64 
     65   if (database->HasOriginPath(origin)) {
     66     // Don't bother.
     67     base::DeleteFile(isolated_directory, true /* recursive */);
     68     return;
     69   }
     70 
     71   base::FilePath directory_name;
     72   if (database->GetPathForOrigin(origin, &directory_name)) {
     73     base::FilePath origin_directory =
     74         file_system_directory.Append(directory_name);
     75     base::DeleteFile(origin_directory, true /* recursive */);
     76     base::Move(isolated_directory, origin_directory);
     77   }
     78 }
     79 
     80 void SandboxIsolatedOriginDatabase::MigrateDatabaseIfNeeded() {
     81   if (migration_checked_)
     82     return;
     83 
     84   migration_checked_ = true;
     85   // See if we have non-isolated version of sandbox database.
     86   scoped_ptr<SandboxOriginDatabase> database(
     87       new SandboxOriginDatabase(file_system_directory_));
     88   if (!database->HasOriginPath(origin_))
     89     return;
     90 
     91   base::FilePath directory_name;
     92   if (database->GetPathForOrigin(origin_, &directory_name) &&
     93       directory_name != base::FilePath(kOriginDirectory)) {
     94     base::FilePath from_path = file_system_directory_.Append(directory_name);
     95     base::FilePath to_path = file_system_directory_.Append(kOriginDirectory);
     96 
     97     if (base::PathExists(to_path))
     98       base::DeleteFile(to_path, true /* recursive */);
     99     base::Move(from_path, to_path);
    100   }
    101 
    102   database->RemoveDatabase();
    103 }
    104 
    105 }  // namespace fileapi
    106