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.
5 #include "chrome/browser/chromeos/extensions/file_manager/private_api_misc.h"
7 #include "apps/app_window.h"
8 #include "apps/app_window_registry.h"
9 #include "ash/frame/frame_util.h"
10 #include "base/files/file_path.h"
11 #include "base/prefs/pref_service.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/chromeos/drive/file_system_util.h"
15 #include "chrome/browser/chromeos/extensions/file_manager/event_router.h"
16 #include "chrome/browser/chromeos/extensions/file_manager/file_browser_private_api.h"
17 #include "chrome/browser/chromeos/extensions/file_manager/private_api_util.h"
18 #include "chrome/browser/chromeos/file_manager/app_installer.h"
19 #include "chrome/browser/chromeos/login/user_manager.h"
20 #include "chrome/browser/chromeos/settings/cros_settings.h"
21 #include "chrome/browser/drive/event_logger.h"
22 #include "chrome/browser/lifetime/application_lifetime.h"
23 #include "chrome/browser/profiles/profile.h"
24 #include "chrome/browser/profiles/profile_manager.h"
25 #include "chrome/browser/profiles/profiles_state.h"
26 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
27 #include "chrome/browser/signin/signin_manager.h"
28 #include "chrome/browser/signin/signin_manager_factory.h"
29 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h"
30 #include "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h"
31 #include "chrome/common/extensions/api/file_browser_private.h"
32 #include "chrome/common/pref_names.h"
33 #include "components/signin/core/browser/profile_oauth2_token_service.h"
34 #include "content/public/browser/render_view_host.h"
35 #include "content/public/browser/web_contents.h"
36 #include "content/public/common/page_zoom.h"
37 #include "google_apis/drive/auth_service.h"
38 #include "ui/base/webui/web_ui_util.h"
41 namespace extensions {
44 const char kCWSScope[] = "https://www.googleapis.com/auth/chromewebstore";
46 // Obtains the current app window.
47 apps::AppWindow* GetCurrentAppWindow(ChromeSyncExtensionFunction* function) {
48 apps::AppWindowRegistry* const app_window_registry =
49 apps::AppWindowRegistry::Get(function->GetProfile());
50 content::WebContents* const contents = function->GetAssociatedWebContents();
51 content::RenderViewHost* const render_view_host =
52 contents ? contents->GetRenderViewHost() : NULL;
53 return render_view_host ? app_window_registry->GetAppWindowForRenderViewHost(
58 std::vector<linked_ptr<api::file_browser_private::ProfileInfo> >
59 GetLoggedInProfileInfoList(content::WebContents* contents) {
60 DCHECK(chromeos::UserManager::IsInitialized());
61 const std::vector<Profile*>& profiles =
62 g_browser_process->profile_manager()->GetLoadedProfiles();
63 std::set<Profile*> original_profiles;
64 std::vector<linked_ptr<api::file_browser_private::ProfileInfo> >
67 for (size_t i = 0; i < profiles.size(); ++i) {
68 // Filter the profile.
69 Profile* const profile = profiles[i]->GetOriginalProfile();
70 if (original_profiles.count(profile))
72 original_profiles.insert(profile);
73 const chromeos::User* const user =
74 chromeos::UserManager::Get()->GetUserByProfile(profile);
75 if (!user || !user->is_logged_in())
78 // Make a ProfileInfo.
79 linked_ptr<api::file_browser_private::ProfileInfo> profile_info(
80 new api::file_browser_private::ProfileInfo());
81 profile_info->profile_id = multi_user_util::GetUserIDFromProfile(profile);
82 profile_info->display_name = UTF16ToUTF8(user->GetDisplayName());
83 // TODO(hirono): Remove the property from the profile_info.
84 profile_info->is_current_profile = true;
86 // Make an icon URL of the profile.
88 const gfx::Image& image =
89 ash::GetAvatarImageForContext(contents->GetBrowserContext());
90 const SkBitmap* const bitmap = image.ToSkBitmap();
92 profile_info->image_uri.reset(
93 new std::string(webui::GetBitmapDataUrl(*bitmap)));
96 result_profiles.push_back(profile_info);
99 return result_profiles;
103 bool FileBrowserPrivateLogoutUserForReauthenticationFunction::RunImpl() {
104 chromeos::User* user =
105 chromeos::UserManager::Get()->GetUserByProfile(GetProfile());
107 chromeos::UserManager::Get()->SaveUserOAuthStatus(
109 chromeos::User::OAUTH2_TOKEN_STATUS_INVALID);
112 chrome::AttemptUserExit();
116 bool FileBrowserPrivateGetPreferencesFunction::RunImpl() {
117 api::file_browser_private::Preferences result;
118 const PrefService* const service = GetProfile()->GetPrefs();
120 result.drive_enabled = drive::util::IsDriveEnabledForProfile(GetProfile());
121 result.cellular_disabled =
122 service->GetBoolean(prefs::kDisableDriveOverCellular);
123 result.hosted_files_disabled =
124 service->GetBoolean(prefs::kDisableDriveHostedFiles);
125 result.use24hour_clock = service->GetBoolean(prefs::kUse24HourClock);
126 result.allow_redeem_offers = true;
127 if (!chromeos::CrosSettings::Get()->GetBoolean(
128 chromeos::kAllowRedeemChromeOsRegistrationOffers,
129 &result.allow_redeem_offers)) {
130 result.allow_redeem_offers = true;
133 SetResult(result.ToValue().release());
135 drive::EventLogger* logger = file_manager::util::GetLogger(GetProfile());
137 logger->Log(logging::LOG_INFO, "%s succeeded.", name().c_str());
141 bool FileBrowserPrivateSetPreferencesFunction::RunImpl() {
142 using extensions::api::file_browser_private::SetPreferences::Params;
143 const scoped_ptr<Params> params(Params::Create(*args_));
144 EXTENSION_FUNCTION_VALIDATE(params);
146 PrefService* const service = GetProfile()->GetPrefs();
148 if (params->change_info.cellular_disabled)
149 service->SetBoolean(prefs::kDisableDriveOverCellular,
150 *params->change_info.cellular_disabled);
152 if (params->change_info.hosted_files_disabled)
153 service->SetBoolean(prefs::kDisableDriveHostedFiles,
154 *params->change_info.hosted_files_disabled);
156 drive::EventLogger* logger = file_manager::util::GetLogger(GetProfile());
158 logger->Log(logging::LOG_INFO, "%s succeeded.", name().c_str());
162 FileBrowserPrivateZipSelectionFunction::
163 FileBrowserPrivateZipSelectionFunction() {}
165 FileBrowserPrivateZipSelectionFunction::
166 ~FileBrowserPrivateZipSelectionFunction() {}
168 bool FileBrowserPrivateZipSelectionFunction::RunImpl() {
169 using extensions::api::file_browser_private::ZipSelection::Params;
170 const scoped_ptr<Params> params(Params::Create(*args_));
171 EXTENSION_FUNCTION_VALIDATE(params);
173 // First param is the source directory URL.
174 if (params->dir_url.empty())
177 base::FilePath src_dir = file_manager::util::GetLocalPathFromURL(
178 render_view_host(), GetProfile(), GURL(params->dir_url));
182 // Second param is the list of selected file URLs.
183 if (params->selection_urls.empty())
186 std::vector<base::FilePath> files;
187 for (size_t i = 0; i < params->selection_urls.size(); ++i) {
188 base::FilePath path = file_manager::util::GetLocalPathFromURL(
189 render_view_host(), GetProfile(), GURL(params->selection_urls[i]));
192 files.push_back(path);
195 // Third param is the name of the output zip file.
196 if (params->dest_name.empty())
199 // Check if the dir path is under Drive mount point.
200 // TODO(hshi): support create zip file on Drive (crbug.com/158690).
201 if (drive::util::IsUnderDriveMountPoint(src_dir))
204 base::FilePath dest_file = src_dir.Append(params->dest_name);
205 std::vector<base::FilePath> src_relative_paths;
206 for (size_t i = 0; i != files.size(); ++i) {
207 const base::FilePath& file_path = files[i];
209 // Obtain the relative path of |file_path| under |src_dir|.
210 base::FilePath relative_path;
211 if (!src_dir.AppendRelativePath(file_path, &relative_path))
213 src_relative_paths.push_back(relative_path);
216 zip_file_creator_ = new file_manager::ZipFileCreator(this,
221 // Keep the refcount until the zipping is complete on utility process.
224 zip_file_creator_->Start();
228 void FileBrowserPrivateZipSelectionFunction::OnZipDone(bool success) {
229 SetResult(new base::FundamentalValue(success));
234 bool FileBrowserPrivateZoomFunction::RunImpl() {
235 using extensions::api::file_browser_private::Zoom::Params;
236 const scoped_ptr<Params> params(Params::Create(*args_));
237 EXTENSION_FUNCTION_VALIDATE(params);
239 content::PageZoom zoom_type;
240 switch (params->operation) {
241 case api::file_browser_private::ZOOM_OPERATION_TYPE_IN:
242 zoom_type = content::PAGE_ZOOM_IN;
244 case api::file_browser_private::ZOOM_OPERATION_TYPE_OUT:
245 zoom_type = content::PAGE_ZOOM_OUT;
247 case api::file_browser_private::ZOOM_OPERATION_TYPE_RESET:
248 zoom_type = content::PAGE_ZOOM_RESET;
254 render_view_host()->Zoom(zoom_type);
258 bool FileBrowserPrivateInstallWebstoreItemFunction::RunImpl() {
259 using extensions::api::file_browser_private::InstallWebstoreItem::Params;
260 const scoped_ptr<Params> params(Params::Create(*args_));
261 EXTENSION_FUNCTION_VALIDATE(params);
263 if (params->item_id.empty())
266 const extensions::WebstoreStandaloneInstaller::Callback callback =
268 &FileBrowserPrivateInstallWebstoreItemFunction::OnInstallComplete,
271 scoped_refptr<file_manager::AppInstaller> installer(
272 new file_manager::AppInstaller(
273 GetAssociatedWebContents(),
277 // installer will be AddRef()'d in BeginInstall().
278 installer->BeginInstall();
282 void FileBrowserPrivateInstallWebstoreItemFunction::OnInstallComplete(
284 const std::string& error) {
285 drive::EventLogger* logger = file_manager::util::GetLogger(GetProfile());
288 logger->Log(logging::LOG_INFO,
289 "App install succeeded. (item id: %s)",
290 webstore_item_id_.c_str());
294 logger->Log(logging::LOG_ERROR,
295 "App install failed. (item id: %s, reason: %s)",
296 webstore_item_id_.c_str(),
302 SendResponse(success);
305 FileBrowserPrivateRequestWebStoreAccessTokenFunction::
306 FileBrowserPrivateRequestWebStoreAccessTokenFunction() {
309 FileBrowserPrivateRequestWebStoreAccessTokenFunction::
310 ~FileBrowserPrivateRequestWebStoreAccessTokenFunction() {
313 bool FileBrowserPrivateRequestWebStoreAccessTokenFunction::RunImpl() {
314 std::vector<std::string> scopes;
315 scopes.push_back(kCWSScope);
317 ProfileOAuth2TokenService* oauth_service =
318 ProfileOAuth2TokenServiceFactory::GetForProfile(GetProfile());
319 net::URLRequestContextGetter* url_request_context_getter =
320 g_browser_process->system_request_context();
322 if (!oauth_service) {
323 drive::EventLogger* logger = file_manager::util::GetLogger(GetProfile());
325 logger->Log(logging::LOG_ERROR,
326 "CWS OAuth token fetch failed. OAuth2TokenService can't "
329 SetResult(base::Value::CreateNullValue());
333 SigninManagerBase* signin_manager =
334 SigninManagerFactory::GetForProfile(GetProfile());
335 auth_service_.reset(new google_apis::AuthService(
337 signin_manager->GetAuthenticatedAccountId(),
338 url_request_context_getter,
340 auth_service_->StartAuthentication(base::Bind(
341 &FileBrowserPrivateRequestWebStoreAccessTokenFunction::
342 OnAccessTokenFetched,
348 void FileBrowserPrivateRequestWebStoreAccessTokenFunction::OnAccessTokenFetched(
349 google_apis::GDataErrorCode code,
350 const std::string& access_token) {
351 drive::EventLogger* logger = file_manager::util::GetLogger(GetProfile());
353 if (code == google_apis::HTTP_SUCCESS) {
354 DCHECK(auth_service_->HasAccessToken());
355 DCHECK(access_token == auth_service_->access_token());
357 logger->Log(logging::LOG_INFO, "CWS OAuth token fetch succeeded.");
358 SetResult(new base::StringValue(access_token));
362 logger->Log(logging::LOG_ERROR,
363 "CWS OAuth token fetch failed. (GDataErrorCode: %s)",
364 google_apis::GDataErrorCodeToString(code).c_str());
366 SetResult(base::Value::CreateNullValue());
371 bool FileBrowserPrivateGetProfilesFunction::RunImpl() {
372 const std::vector<linked_ptr<api::file_browser_private::ProfileInfo> >&
373 profiles = GetLoggedInProfileInfoList(GetAssociatedWebContents());
375 // Obtains the display profile ID.
376 apps::AppWindow* const app_window = GetCurrentAppWindow(this);
377 chrome::MultiUserWindowManager* const window_manager =
378 chrome::MultiUserWindowManager::GetInstance();
379 const std::string current_profile_id =
380 multi_user_util::GetUserIDFromProfile(GetProfile());
381 const std::string display_profile_id =
382 window_manager && app_window ? window_manager->GetUserPresentingWindow(
383 app_window->GetNativeWindow())
386 results_ = api::file_browser_private::GetProfiles::Results::Create(
389 display_profile_id.empty() ? current_profile_id : display_profile_id);
393 bool FileBrowserPrivateVisitDesktopFunction::RunImpl() {
394 using api::file_browser_private::VisitDesktop::Params;
395 const scoped_ptr<Params> params(Params::Create(*args_));
396 const std::vector<linked_ptr<api::file_browser_private::ProfileInfo> >&
397 profiles = GetLoggedInProfileInfoList(GetAssociatedWebContents());
399 // Check the multi-profile support.
400 if (!profiles::IsMultipleProfilesEnabled()) {
401 SetError("Multi-profile support is not enabled.");
405 chrome::MultiUserWindowManager* const window_manager =
406 chrome::MultiUserWindowManager::GetInstance();
407 DCHECK(window_manager);
409 // Check if the target user is logged-in or not.
410 bool logged_in = false;
411 for (size_t i = 0; i < profiles.size(); ++i) {
412 if (profiles[i]->profile_id == params->profile_id) {
418 SetError("The user is not logged-in now.");
422 // Look for the current app window.
423 apps::AppWindow* const app_window = GetCurrentAppWindow(this);
425 SetError("Target window is not found.");
429 // Observe owner changes of windows.
430 file_manager::EventRouter* const event_router =
431 file_manager::FileBrowserPrivateAPI::Get(GetProfile())->event_router();
432 event_router->RegisterMultiUserWindowManagerObserver();
434 // Move the window to the user's desktop.
435 window_manager->ShowWindowForUser(app_window->GetNativeWindow(),
439 if (!window_manager->IsWindowOnDesktopOfUser(app_window->GetNativeWindow(),
440 params->profile_id)) {
441 SetError("The window cannot visit the desktop.");
448 } // namespace extensions