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 "chrome/browser/chromeos/extensions/file_manager/private_api_misc.h" 6 7 #include "base/files/file_path.h" 8 #include "base/prefs/pref_service.h" 9 #include "base/values.h" 10 #include "chrome/browser/chromeos/drive/drive_integration_service.h" 11 #include "chrome/browser/chromeos/drive/logging.h" 12 #include "chrome/browser/chromeos/extensions/file_manager/private_api_util.h" 13 #include "chrome/browser/chromeos/settings/cros_settings.h" 14 #include "chrome/browser/lifetime/application_lifetime.h" 15 #include "chrome/browser/profiles/profile.h" 16 #include "chrome/common/pref_names.h" 17 #include "content/public/browser/render_view_host.h" 18 #include "content/public/common/page_zoom.h" 19 #include "url/gurl.h" 20 21 namespace file_manager { 22 23 LogoutUserFunction::LogoutUserFunction() { 24 } 25 26 LogoutUserFunction::~LogoutUserFunction() { 27 } 28 29 bool LogoutUserFunction::RunImpl() { 30 chrome::AttemptUserExit(); 31 return true; 32 } 33 34 GetPreferencesFunction::GetPreferencesFunction() { 35 } 36 37 GetPreferencesFunction::~GetPreferencesFunction() { 38 } 39 40 bool GetPreferencesFunction::RunImpl() { 41 scoped_ptr<DictionaryValue> value(new DictionaryValue()); 42 43 const PrefService* service = profile_->GetPrefs(); 44 45 drive::DriveIntegrationService* integration_service = 46 drive::DriveIntegrationServiceFactory::GetForProfile(profile_); 47 bool drive_enabled = (integration_service != NULL); 48 49 value->SetBoolean("driveEnabled", drive_enabled); 50 51 value->SetBoolean("cellularDisabled", 52 service->GetBoolean(prefs::kDisableDriveOverCellular)); 53 54 value->SetBoolean("hostedFilesDisabled", 55 service->GetBoolean(prefs::kDisableDriveHostedFiles)); 56 57 value->SetBoolean("use24hourClock", 58 service->GetBoolean(prefs::kUse24HourClock)); 59 60 { 61 bool allow = true; 62 if (!chromeos::CrosSettings::Get()->GetBoolean( 63 chromeos::kAllowRedeemChromeOsRegistrationOffers, &allow)) { 64 allow = true; 65 } 66 value->SetBoolean("allowRedeemOffers", allow); 67 } 68 69 SetResult(value.release()); 70 71 drive::util::Log(logging::LOG_INFO, "%s succeeded.", name().c_str()); 72 return true; 73 } 74 75 SetPreferencesFunction::SetPreferencesFunction() { 76 } 77 78 SetPreferencesFunction::~SetPreferencesFunction() { 79 } 80 81 bool SetPreferencesFunction::RunImpl() { 82 base::DictionaryValue* value = NULL; 83 84 if (!args_->GetDictionary(0, &value) || !value) 85 return false; 86 87 PrefService* service = profile_->GetPrefs(); 88 89 bool tmp; 90 91 if (value->GetBoolean("cellularDisabled", &tmp)) 92 service->SetBoolean(prefs::kDisableDriveOverCellular, tmp); 93 94 if (value->GetBoolean("hostedFilesDisabled", &tmp)) 95 service->SetBoolean(prefs::kDisableDriveHostedFiles, tmp); 96 97 drive::util::Log(logging::LOG_INFO, "%s succeeded.", name().c_str()); 98 return true; 99 } 100 101 ZipSelectionFunction::ZipSelectionFunction() { 102 } 103 104 ZipSelectionFunction::~ZipSelectionFunction() { 105 } 106 107 bool ZipSelectionFunction::RunImpl() { 108 if (args_->GetSize() < 3) { 109 return false; 110 } 111 112 // First param is the source directory URL. 113 std::string dir_url; 114 if (!args_->GetString(0, &dir_url) || dir_url.empty()) 115 return false; 116 117 base::FilePath src_dir = util::GetLocalPathFromURL( 118 render_view_host(), profile(), GURL(dir_url)); 119 if (src_dir.empty()) 120 return false; 121 122 // Second param is the list of selected file URLs. 123 ListValue* selection_urls = NULL; 124 args_->GetList(1, &selection_urls); 125 if (!selection_urls || !selection_urls->GetSize()) 126 return false; 127 128 std::vector<base::FilePath> files; 129 for (size_t i = 0; i < selection_urls->GetSize(); ++i) { 130 std::string file_url; 131 selection_urls->GetString(i, &file_url); 132 base::FilePath path = util::GetLocalPathFromURL( 133 render_view_host(), profile(), GURL(file_url)); 134 if (path.empty()) 135 return false; 136 files.push_back(path); 137 } 138 139 // Third param is the name of the output zip file. 140 std::string dest_name; 141 if (!args_->GetString(2, &dest_name) || dest_name.empty()) 142 return false; 143 144 // Check if the dir path is under Drive mount point. 145 // TODO(hshi): support create zip file on Drive (crbug.com/158690). 146 if (drive::util::IsUnderDriveMountPoint(src_dir)) 147 return false; 148 149 base::FilePath dest_file = src_dir.Append(dest_name); 150 std::vector<base::FilePath> src_relative_paths; 151 for (size_t i = 0; i != files.size(); ++i) { 152 const base::FilePath& file_path = files[i]; 153 154 // Obtain the relative path of |file_path| under |src_dir|. 155 base::FilePath relative_path; 156 if (!src_dir.AppendRelativePath(file_path, &relative_path)) 157 return false; 158 src_relative_paths.push_back(relative_path); 159 } 160 161 zip_file_creator_ = new ZipFileCreator(this, 162 src_dir, 163 src_relative_paths, 164 dest_file); 165 166 // Keep the refcount until the zipping is complete on utility process. 167 AddRef(); 168 169 zip_file_creator_->Start(); 170 return true; 171 } 172 173 void ZipSelectionFunction::OnZipDone(bool success) { 174 SetResult(new base::FundamentalValue(success)); 175 SendResponse(true); 176 Release(); 177 } 178 179 ZoomFunction::ZoomFunction() { 180 } 181 182 ZoomFunction::~ZoomFunction() { 183 } 184 185 bool ZoomFunction::RunImpl() { 186 content::RenderViewHost* const view_host = render_view_host(); 187 std::string operation; 188 args_->GetString(0, &operation); 189 content::PageZoom zoom_type; 190 if (operation == "in") { 191 zoom_type = content::PAGE_ZOOM_IN; 192 } else if (operation == "out") { 193 zoom_type = content::PAGE_ZOOM_OUT; 194 } else if (operation == "reset") { 195 zoom_type = content::PAGE_ZOOM_RESET; 196 } else { 197 NOTREACHED(); 198 return false; 199 } 200 view_host->Zoom(zoom_type); 201 return true; 202 } 203 204 } // namespace file_manager 205