1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/extensions/file_manager/private_api_misc.h"
#include "ash/frame/frame_util.h"
#include "base/files/file_path.h"
#include "base/prefs/pref_service.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chromeos/drive/file_system_util.h"
#include "chrome/browser/chromeos/extensions/file_manager/file_browser_private_api.h"
#include "chrome/browser/chromeos/extensions/file_manager/private_api_util.h"
#include "chrome/browser/chromeos/file_manager/app_installer.h"
#include "chrome/browser/chromeos/file_manager/zip_file_creator.h"
#include "chrome/browser/chromeos/profiles/profile_helper.h"
#include "chrome/browser/chromeos/settings/cros_settings.h"
#include "chrome/browser/devtools/devtools_window.h"
#include "chrome/browser/drive/event_logger.h"
#include "chrome/browser/extensions/devtools_util.h"
#include "chrome/browser/lifetime/application_lifetime.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/profiles/profiles_state.h"
#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
#include "chrome/browser/signin/signin_manager_factory.h"
#include "chrome/browser/ui/ash/multi_user/multi_user_util.h"
#include "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h"
#include "chrome/common/extensions/api/file_browser_private.h"
#include "chrome/common/pref_names.h"
#include "components/signin/core/browser/profile_oauth2_token_service.h"
#include "components/signin/core/browser/signin_manager.h"
#include "components/user_manager/user_manager.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/page_zoom.h"
#include "extensions/browser/app_window/app_window.h"
#include "extensions/browser/app_window/app_window_registry.h"
#include "google_apis/drive/auth_service.h"
#include "ui/base/webui/web_ui_util.h"
#include "url/gurl.h"
namespace extensions {
namespace {
const char kCWSScope[] = "https://www.googleapis.com/auth/chromewebstore";
const char kGoogleCastApiExtensionId[] = "mafeflapfdfljijmlienjedomfjfmhpd";
// Obtains the current app window.
AppWindow* GetCurrentAppWindow(ChromeSyncExtensionFunction* function) {
AppWindowRegistry* const app_window_registry =
AppWindowRegistry::Get(function->GetProfile());
content::WebContents* const contents = function->GetAssociatedWebContents();
content::RenderViewHost* const render_view_host =
contents ? contents->GetRenderViewHost() : NULL;
return render_view_host ? app_window_registry->GetAppWindowForRenderViewHost(
render_view_host)
: NULL;
}
std::vector<linked_ptr<api::file_browser_private::ProfileInfo> >
GetLoggedInProfileInfoList() {
DCHECK(user_manager::UserManager::IsInitialized());
const std::vector<Profile*>& profiles =
g_browser_process->profile_manager()->GetLoadedProfiles();
std::set<Profile*> original_profiles;
std::vector<linked_ptr<api::file_browser_private::ProfileInfo> >
result_profiles;
for (size_t i = 0; i < profiles.size(); ++i) {
// Filter the profile.
Profile* const profile = profiles[i]->GetOriginalProfile();
if (original_profiles.count(profile))
continue;
original_profiles.insert(profile);
const user_manager::User* const user =
chromeos::ProfileHelper::Get()->GetUserByProfile(profile);
if (!user || !user->is_logged_in())
continue;
// Make a ProfileInfo.
linked_ptr<api::file_browser_private::ProfileInfo> profile_info(
new api::file_browser_private::ProfileInfo());
profile_info->profile_id = multi_user_util::GetUserIDFromProfile(profile);
profile_info->display_name = UTF16ToUTF8(user->GetDisplayName());
// TODO(hirono): Remove the property from the profile_info.
profile_info->is_current_profile = true;
result_profiles.push_back(profile_info);
}
return result_profiles;
}
} // namespace
bool FileBrowserPrivateLogoutUserForReauthenticationFunction::RunSync() {
user_manager::User* user =
chromeos::ProfileHelper::Get()->GetUserByProfile(GetProfile());
if (user) {
user_manager::UserManager::Get()->SaveUserOAuthStatus(
user->email(), user_manager::User::OAUTH2_TOKEN_STATUS_INVALID);
}
chrome::AttemptUserExit();
return true;
}
bool FileBrowserPrivateGetPreferencesFunction::RunSync() {
api::file_browser_private::Preferences result;
const PrefService* const service = GetProfile()->GetPrefs();
result.drive_enabled = drive::util::IsDriveEnabledForProfile(GetProfile());
result.cellular_disabled =
service->GetBoolean(prefs::kDisableDriveOverCellular);
result.hosted_files_disabled =
service->GetBoolean(prefs::kDisableDriveHostedFiles);
result.use24hour_clock = service->GetBoolean(prefs::kUse24HourClock);
result.allow_redeem_offers = true;
if (!chromeos::CrosSettings::Get()->GetBoolean(
chromeos::kAllowRedeemChromeOsRegistrationOffers,
&result.allow_redeem_offers)) {
result.allow_redeem_offers = true;
}
SetResult(result.ToValue().release());
drive::EventLogger* logger = file_manager::util::GetLogger(GetProfile());
if (logger)
logger->Log(logging::LOG_INFO, "%s succeeded.", name().c_str());
return true;
}
bool FileBrowserPrivateSetPreferencesFunction::RunSync() {
using extensions::api::file_browser_private::SetPreferences::Params;
const scoped_ptr<Params> params(Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params);
PrefService* const service = GetProfile()->GetPrefs();
if (params->change_info.cellular_disabled)
service->SetBoolean(prefs::kDisableDriveOverCellular,
*params->change_info.cellular_disabled);
if (params->change_info.hosted_files_disabled)
service->SetBoolean(prefs::kDisableDriveHostedFiles,
*params->change_info.hosted_files_disabled);
drive::EventLogger* logger = file_manager::util::GetLogger(GetProfile());
if (logger)
logger->Log(logging::LOG_INFO, "%s succeeded.", name().c_str());
return true;
}
FileBrowserPrivateZipSelectionFunction::
FileBrowserPrivateZipSelectionFunction() {}
FileBrowserPrivateZipSelectionFunction::
~FileBrowserPrivateZipSelectionFunction() {}
bool FileBrowserPrivateZipSelectionFunction::RunAsync() {
using extensions::api::file_browser_private::ZipSelection::Params;
const scoped_ptr<Params> params(Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params);
// First param is the source directory URL.
if (params->dir_url.empty())
return false;
base::FilePath src_dir = file_manager::util::GetLocalPathFromURL(
render_view_host(), GetProfile(), GURL(params->dir_url));
if (src_dir.empty())
return false;
// Second param is the list of selected file URLs.
if (params->selection_urls.empty())
return false;
std::vector<base::FilePath> files;
for (size_t i = 0; i < params->selection_urls.size(); ++i) {
base::FilePath path = file_manager::util::GetLocalPathFromURL(
render_view_host(), GetProfile(), GURL(params->selection_urls[i]));
if (path.empty())
return false;
files.push_back(path);
}
// Third param is the name of the output zip file.
if (params->dest_name.empty())
return false;
// Check if the dir path is under Drive mount point.
// TODO(hshi): support create zip file on Drive (crbug.com/158690).
if (drive::util::IsUnderDriveMountPoint(src_dir))
return false;
base::FilePath dest_file = src_dir.Append(params->dest_name);
std::vector<base::FilePath> src_relative_paths;
for (size_t i = 0; i != files.size(); ++i) {
const base::FilePath& file_path = files[i];
// Obtain the relative path of |file_path| under |src_dir|.
base::FilePath relative_path;
if (!src_dir.AppendRelativePath(file_path, &relative_path))
return false;
src_relative_paths.push_back(relative_path);
}
(new file_manager::ZipFileCreator(
base::Bind(&FileBrowserPrivateZipSelectionFunction::OnZipDone, this),
src_dir,
src_relative_paths,
dest_file))->Start();
return true;
}
void FileBrowserPrivateZipSelectionFunction::OnZipDone(bool success) {
SetResult(new base::FundamentalValue(success));
SendResponse(true);
}
bool FileBrowserPrivateZoomFunction::RunSync() {
using extensions::api::file_browser_private::Zoom::Params;
const scoped_ptr<Params> params(Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params);
content::PageZoom zoom_type;
switch (params->operation) {
case api::file_browser_private::ZOOM_OPERATION_TYPE_IN:
zoom_type = content::PAGE_ZOOM_IN;
break;
case api::file_browser_private::ZOOM_OPERATION_TYPE_OUT:
zoom_type = content::PAGE_ZOOM_OUT;
break;
case api::file_browser_private::ZOOM_OPERATION_TYPE_RESET:
zoom_type = content::PAGE_ZOOM_RESET;
break;
default:
NOTREACHED();
return false;
}
render_view_host()->Zoom(zoom_type);
return true;
}
bool FileBrowserPrivateInstallWebstoreItemFunction::RunAsync() {
using extensions::api::file_browser_private::InstallWebstoreItem::Params;
const scoped_ptr<Params> params(Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params);
if (params->item_id.empty())
return false;
const extensions::WebstoreStandaloneInstaller::Callback callback =
base::Bind(
&FileBrowserPrivateInstallWebstoreItemFunction::OnInstallComplete,
this);
// Only GoogleCastAPI extension can use silent installation.
if (params->silent_installation &&
params->item_id != kGoogleCastApiExtensionId) {
SetError("Only whitelisted items can do silent installation.");
return false;
}
scoped_refptr<file_manager::AppInstaller> installer(
new file_manager::AppInstaller(GetAssociatedWebContents(),
params->item_id,
GetProfile(),
params->silent_installation,
callback));
// installer will be AddRef()'d in BeginInstall().
installer->BeginInstall();
return true;
}
void FileBrowserPrivateInstallWebstoreItemFunction::OnInstallComplete(
bool success,
const std::string& error,
extensions::webstore_install::Result result) {
drive::EventLogger* logger = file_manager::util::GetLogger(GetProfile());
if (success) {
if (logger) {
logger->Log(logging::LOG_INFO,
"App install succeeded. (item id: %s)",
webstore_item_id_.c_str());
}
} else {
if (logger) {
logger->Log(logging::LOG_ERROR,
"App install failed. (item id: %s, reason: %s)",
webstore_item_id_.c_str(),
error.c_str());
}
SetError(error);
}
SendResponse(success);
}
FileBrowserPrivateRequestWebStoreAccessTokenFunction::
FileBrowserPrivateRequestWebStoreAccessTokenFunction() {
}
FileBrowserPrivateRequestWebStoreAccessTokenFunction::
~FileBrowserPrivateRequestWebStoreAccessTokenFunction() {
}
bool FileBrowserPrivateRequestWebStoreAccessTokenFunction::RunAsync() {
std::vector<std::string> scopes;
scopes.push_back(kCWSScope);
ProfileOAuth2TokenService* oauth_service =
ProfileOAuth2TokenServiceFactory::GetForProfile(GetProfile());
net::URLRequestContextGetter* url_request_context_getter =
g_browser_process->system_request_context();
if (!oauth_service) {
drive::EventLogger* logger = file_manager::util::GetLogger(GetProfile());
if (logger) {
logger->Log(logging::LOG_ERROR,
"CWS OAuth token fetch failed. OAuth2TokenService can't "
"be retrieved.");
}
SetResult(base::Value::CreateNullValue());
return false;
}
SigninManagerBase* signin_manager =
SigninManagerFactory::GetForProfile(GetProfile());
auth_service_.reset(new google_apis::AuthService(
oauth_service,
signin_manager->GetAuthenticatedAccountId(),
url_request_context_getter,
scopes));
auth_service_->StartAuthentication(base::Bind(
&FileBrowserPrivateRequestWebStoreAccessTokenFunction::
OnAccessTokenFetched,
this));
return true;
}
void FileBrowserPrivateRequestWebStoreAccessTokenFunction::OnAccessTokenFetched(
google_apis::GDataErrorCode code,
const std::string& access_token) {
drive::EventLogger* logger = file_manager::util::GetLogger(GetProfile());
if (code == google_apis::HTTP_SUCCESS) {
DCHECK(auth_service_->HasAccessToken());
DCHECK(access_token == auth_service_->access_token());
if (logger)
logger->Log(logging::LOG_INFO, "CWS OAuth token fetch succeeded.");
SetResult(new base::StringValue(access_token));
SendResponse(true);
} else {
if (logger) {
logger->Log(logging::LOG_ERROR,
"CWS OAuth token fetch failed. (GDataErrorCode: %s)",
google_apis::GDataErrorCodeToString(code).c_str());
}
SetResult(base::Value::CreateNullValue());
SendResponse(false);
}
}
bool FileBrowserPrivateGetProfilesFunction::RunSync() {
const std::vector<linked_ptr<api::file_browser_private::ProfileInfo> >&
profiles = GetLoggedInProfileInfoList();
// Obtains the display profile ID.
AppWindow* const app_window = GetCurrentAppWindow(this);
chrome::MultiUserWindowManager* const window_manager =
chrome::MultiUserWindowManager::GetInstance();
const std::string current_profile_id =
multi_user_util::GetUserIDFromProfile(GetProfile());
const std::string display_profile_id =
window_manager && app_window ? window_manager->GetUserPresentingWindow(
app_window->GetNativeWindow())
: "";
results_ = api::file_browser_private::GetProfiles::Results::Create(
profiles,
current_profile_id,
display_profile_id.empty() ? current_profile_id : display_profile_id);
return true;
}
bool FileBrowserPrivateVisitDesktopFunction::RunSync() {
using api::file_browser_private::VisitDesktop::Params;
const scoped_ptr<Params> params(Params::Create(*args_));
const std::vector<linked_ptr<api::file_browser_private::ProfileInfo> >&
profiles = GetLoggedInProfileInfoList();
chrome::MultiUserWindowManager* const window_manager =
chrome::MultiUserWindowManager::GetInstance();
DCHECK(window_manager);
// Check if the target user is logged-in or not.
bool logged_in = false;
for (size_t i = 0; i < profiles.size(); ++i) {
if (profiles[i]->profile_id == params->profile_id) {
logged_in = true;
break;
}
}
if (!logged_in) {
SetError("The user is not logged-in now.");
return false;
}
// Look for the current app window.
AppWindow* const app_window = GetCurrentAppWindow(this);
if (!app_window) {
SetError("Target window is not found.");
return false;
}
// Move the window to the user's desktop.
window_manager->ShowWindowForUser(app_window->GetNativeWindow(),
params->profile_id);
// Check the result.
if (!window_manager->IsWindowOnDesktopOfUser(app_window->GetNativeWindow(),
params->profile_id)) {
SetError("The window cannot visit the desktop.");
return false;
}
return true;
}
bool FileBrowserPrivateOpenInspectorFunction::RunSync() {
using extensions::api::file_browser_private::OpenInspector::Params;
const scoped_ptr<Params> params(Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params);
switch (params->type) {
case extensions::api::file_browser_private::INSPECTION_TYPE_NORMAL:
// Open inspector for foreground page.
DevToolsWindow::OpenDevToolsWindow(
content::WebContents::FromRenderViewHost(render_view_host()));
break;
case extensions::api::file_browser_private::INSPECTION_TYPE_CONSOLE:
// Open inspector for foreground page and bring focus to the console.
DevToolsWindow::OpenDevToolsWindow(
content::WebContents::FromRenderViewHost(render_view_host()),
DevToolsToggleAction::ShowConsole());
break;
case extensions::api::file_browser_private::INSPECTION_TYPE_ELEMENT:
// Open inspector for foreground page in inspect element mode.
DevToolsWindow::OpenDevToolsWindow(
content::WebContents::FromRenderViewHost(render_view_host()),
DevToolsToggleAction::Inspect());
break;
case extensions::api::file_browser_private::INSPECTION_TYPE_BACKGROUND:
// Open inspector for background page.
extensions::devtools_util::InspectBackgroundPage(extension(),
GetProfile());
break;
default:
NOTREACHED();
SetError(
base::StringPrintf("Unexpected inspection type(%d) is specified.",
static_cast<int>(params->type)));
return false;
}
return true;
}
} // namespace extensions
|