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