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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
|
// Copyright (c) 2010 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/first_run/first_run.h"
#include "build/build_config.h"
// TODO(port): move more code in back from the first_run_win.cc module.
#if defined(OS_WIN)
#include "chrome/installer/util/google_update_settings.h"
#include "chrome/installer/util/install_util.h"
#endif
#include "base/command_line.h"
#include "base/file_util.h"
#include "base/path_service.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/importer/importer.h"
#include "chrome/browser/metrics/user_metrics.h"
#include "chrome/browser/pref_service.h"
#include "chrome/browser/process_singleton.h"
#include "chrome/browser/profile_manager.h"
#include "chrome/browser/shell_integration.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "chrome/installer/util/master_preferences.h"
#include "chrome/installer/util/util_constants.h"
namespace {
// The kSentinelFile file absence will tell us it is a first run.
const char kSentinelFile[] = "First Run";
FilePath GetDefaultPrefFilePath(bool create_profile_dir,
const FilePath& user_data_dir) {
FilePath default_pref_dir =
ProfileManager::GetDefaultProfileDir(user_data_dir);
if (create_profile_dir) {
if (!file_util::PathExists(default_pref_dir)) {
if (!file_util::CreateDirectory(default_pref_dir))
return FilePath();
}
}
return ProfileManager::GetProfilePrefsPath(default_pref_dir);
}
} // namespace
FirstRun::FirstRunState FirstRun::first_run_ = FIRST_RUN_UNKNOWN;
// TODO(port): Import switches need to be ported to both Mac and Linux. Not all
// import switches here are implemented for Linux. None are implemented for Mac
// (as this function will not be called on Mac).
int FirstRun::ImportNow(Profile* profile, const CommandLine& cmdline) {
int return_code = true;
if (cmdline.HasSwitch(switches::kImportFromFile)) {
// Silently import preset bookmarks from file.
// This is an OEM scenario.
return_code = ImportFromFile(profile, cmdline);
}
if (cmdline.HasSwitch(switches::kImport)) {
#if defined(OS_WIN)
return_code = ImportFromBrowser(profile, cmdline);
#else
NOTIMPLEMENTED();
#endif
}
return return_code;
}
// static
bool FirstRun::ProcessMasterPreferences(const FilePath& user_data_dir,
MasterPrefs* out_prefs) {
DCHECK(!user_data_dir.empty());
// The standard location of the master prefs is next to the chrome binary.
FilePath master_prefs;
if (!PathService::Get(base::DIR_EXE, &master_prefs))
return true;
master_prefs = master_prefs.AppendASCII(installer_util::kDefaultMasterPrefs);
scoped_ptr<DictionaryValue> prefs(
installer_util::ParseDistributionPreferences(master_prefs));
if (!prefs.get())
return true;
out_prefs->new_tabs = installer_util::GetFirstRunTabs(prefs.get());
bool value = false;
#if defined(OS_WIN)
// RLZ is currently a Windows-only phenomenon. When it comes to the Mac/
// Linux, enable it here.
if (!installer_util::GetDistroIntegerPreference(prefs.get(),
installer_util::master_preferences::kDistroPingDelay,
&out_prefs->ping_delay)) {
// 90 seconds is the default that we want to use in case master
// preferences is missing, corrupt or ping_delay is missing.
out_prefs->ping_delay = 90;
}
if (installer_util::GetDistroBooleanPreference(prefs.get(),
installer_util::master_preferences::kRequireEula, &value) && value) {
// Show the post-installation EULA. This is done by setup.exe and the
// result determines if we continue or not. We wait here until the user
// dismisses the dialog.
// The actual eula text is in a resource in chrome. We extract it to
// a text file so setup.exe can use it as an inner frame.
FilePath inner_html;
if (WriteEULAtoTempFile(&inner_html)) {
int retcode = 0;
if (!LaunchSetupWithParam(installer_util::switches::kShowEula,
inner_html.ToWStringHack(), &retcode) ||
(retcode == installer_util::EULA_REJECTED)) {
LOG(WARNING) << "EULA rejected. Fast exit.";
::ExitProcess(1);
}
if (retcode == installer_util::EULA_ACCEPTED) {
LOG(INFO) << "EULA : no collection";
GoogleUpdateSettings::SetCollectStatsConsent(false);
} else if (retcode == installer_util::EULA_ACCEPTED_OPT_IN) {
LOG(INFO) << "EULA : collection consent";
GoogleUpdateSettings::SetCollectStatsConsent(true);
}
}
}
#endif
if (installer_util::GetDistroBooleanPreference(prefs.get(),
installer_util::master_preferences::kAltFirstRunBubble, &value) && value)
FirstRun::SetOEMFirstRunBubblePref();
FilePath user_prefs = GetDefaultPrefFilePath(true, user_data_dir);
if (user_prefs.empty())
return true;
// The master prefs are regular prefs so we can just copy the file
// to the default place and they just work.
if (!file_util::CopyFile(master_prefs, user_prefs))
return true;
#if defined(OS_WIN)
DictionaryValue* extensions = 0;
if (installer_util::HasExtensionsBlock(prefs.get(), &extensions)) {
LOG(INFO) << "Extensions block found in master preferences";
DoDelayedInstallExtensions();
}
#endif
if (installer_util::GetDistroBooleanPreference(prefs.get(),
installer_util::master_preferences::kDistroImportSearchPref, &value)) {
if (value) {
out_prefs->do_import_items |= importer::SEARCH_ENGINES;
} else {
out_prefs->dont_import_items |= importer::SEARCH_ENGINES;
}
}
// Check to see if search engine logos should be randomized.
if (installer_util::GetDistroBooleanPreference(prefs.get(),
installer_util::master_preferences::kSearchEngineExperimentRandomizePref,
&value) && value)
out_prefs->randomize_search_engine_experiment = true;
// If we're suppressing the first-run bubble, set that preference now.
// Otherwise, wait until the user has completed first run to set it, so the
// user is guaranteed to see the bubble iff he or she has completed the first
// run process.
if (installer_util::GetDistroBooleanPreference(prefs.get(),
installer_util::master_preferences::kDistroSuppressFirstRunBubble,
&value) && value)
FirstRun::SetShowFirstRunBubblePref(false);
if (installer_util::GetDistroBooleanPreference(prefs.get(),
installer_util::master_preferences::kDistroImportHistoryPref, &value)) {
if (value) {
out_prefs->do_import_items |= importer::HISTORY;
} else {
out_prefs->dont_import_items |= importer::HISTORY;
}
}
std::string not_used;
out_prefs->homepage_defined = prefs->GetString(prefs::kHomePage, ¬_used);
if (installer_util::GetDistroBooleanPreference(prefs.get(),
installer_util::master_preferences::kDistroImportHomePagePref, &value)) {
if (value) {
out_prefs->do_import_items |= importer::HOME_PAGE;
} else {
out_prefs->dont_import_items |= importer::HOME_PAGE;
}
}
// Bookmarks are never imported unless specifically turned on.
if (installer_util::GetDistroBooleanPreference(prefs.get(),
installer_util::master_preferences::kDistroImportBookmarksPref, &value)
&& value) {
out_prefs->do_import_items |= importer::FAVORITES;
}
if (installer_util::GetDistroBooleanPreference(prefs.get(),
installer_util::master_preferences::kMakeChromeDefaultForUser, &value) &&
value)
ShellIntegration::SetAsDefaultBrowser();
// TODO(mirandac): Refactor skip-first-run-ui process into regular first run
// import process. http://crbug.com/49647
// Note we are skipping all other master preferences if skip-first-run-ui
// is *not* specified. (That is, we continue only if skipping first run ui.)
if (!installer_util::GetDistroBooleanPreference(prefs.get(),
installer_util::master_preferences::kDistroSkipFirstRunPref, &value) ||
!value)
return true;
#if !defined(OS_WIN)
// From here on we won't show first run so we need to do the work to show the
// bubble anyway, unless it's already been explicitly suppressed.
FirstRun::SetShowFirstRunBubblePref(true);
#endif
// We need to be able to create the first run sentinel or else we cannot
// proceed because ImportSettings will launch the importer process which
// would end up here if the sentinel is not present.
if (!FirstRun::CreateSentinel())
return false;
if (installer_util::GetDistroBooleanPreference(prefs.get(),
installer_util::master_preferences::kDistroShowWelcomePage, &value) &&
value)
FirstRun::SetShowWelcomePagePref();
std::string import_bookmarks_path;
installer_util::GetDistroStringPreference(prefs.get(),
installer_util::master_preferences::kDistroImportBookmarksFromFilePref,
&import_bookmarks_path);
#if defined(OS_WIN)
std::wstring brand;
GoogleUpdateSettings::GetBrand(&brand);
// This should generally be true, as skip_first_run_ui is a setting used for
// non-organic builds.
if (!GoogleUpdateSettings::IsOrganic(brand)) {
// If search engines aren't explicitly imported, don't import.
if (!(out_prefs->do_import_items & importer::SEARCH_ENGINES)) {
out_prefs->dont_import_items |= importer::SEARCH_ENGINES;
}
// If home page isn't explicitly imported, don't import.
if (!(out_prefs->do_import_items & importer::HOME_PAGE)) {
out_prefs->dont_import_items |= importer::HOME_PAGE;
}
// If history isn't explicitly forbidden, do import.
if (!(out_prefs->dont_import_items & importer::HISTORY)) {
out_prefs->do_import_items |= importer::HISTORY;
}
}
if (out_prefs->do_import_items || !import_bookmarks_path.empty()) {
// There is something to import from the default browser. This launches
// the importer process and blocks until done or until it fails.
scoped_refptr<ImporterHost> importer_host = new ImporterHost();
if (!FirstRun::ImportSettings(NULL,
importer_host->GetSourceProfileInfoAt(0).browser_type,
out_prefs->do_import_items,
FilePath::FromWStringHack(UTF8ToWide(import_bookmarks_path)),
true, NULL)) {
LOG(WARNING) << "silent import failed";
}
}
#else
if (!import_bookmarks_path.empty()) {
// There are bookmarks to import from a file.
FilePath path = FilePath::FromWStringHack(UTF8ToWide(
import_bookmarks_path));
if (!FirstRun::ImportBookmarks(path)) {
LOG(WARNING) << "silent bookmark import failed";
}
}
#endif
return false;
}
// static
bool FirstRun::IsChromeFirstRun() {
if (first_run_ != FIRST_RUN_UNKNOWN)
return first_run_ == FIRST_RUN_TRUE;
FilePath first_run_sentinel;
if (!GetFirstRunSentinelFilePath(&first_run_sentinel) ||
file_util::PathExists(first_run_sentinel)) {
first_run_ = FIRST_RUN_FALSE;
return false;
}
first_run_ = FIRST_RUN_TRUE;
return true;
}
// static
bool FirstRun::RemoveSentinel() {
FilePath first_run_sentinel;
if (!GetFirstRunSentinelFilePath(&first_run_sentinel))
return false;
return file_util::Delete(first_run_sentinel, false);
}
// static
bool FirstRun::CreateSentinel() {
FilePath first_run_sentinel;
if (!GetFirstRunSentinelFilePath(&first_run_sentinel))
return false;
return file_util::WriteFile(first_run_sentinel, "", 0) != -1;
}
// static
bool FirstRun::SetShowFirstRunBubblePref(bool show_bubble) {
PrefService* local_state = g_browser_process->local_state();
if (!local_state)
return false;
if (!local_state->FindPreference(prefs::kShouldShowFirstRunBubble)) {
local_state->RegisterBooleanPref(prefs::kShouldShowFirstRunBubble, false);
local_state->SetBoolean(prefs::kShouldShowFirstRunBubble, show_bubble);
}
return true;
}
// static
bool FirstRun::SetShowWelcomePagePref() {
PrefService* local_state = g_browser_process->local_state();
if (!local_state)
return false;
if (!local_state->FindPreference(prefs::kShouldShowWelcomePage)) {
local_state->RegisterBooleanPref(prefs::kShouldShowWelcomePage, false);
local_state->SetBoolean(prefs::kShouldShowWelcomePage, true);
}
return true;
}
// static
bool FirstRun::SetOEMFirstRunBubblePref() {
PrefService* local_state = g_browser_process->local_state();
if (!local_state)
return false;
if (!local_state->FindPreference(prefs::kShouldUseOEMFirstRunBubble)) {
local_state->RegisterBooleanPref(prefs::kShouldUseOEMFirstRunBubble,
false);
local_state->SetBoolean(prefs::kShouldUseOEMFirstRunBubble, true);
}
return true;
}
// static
bool FirstRun::SetMinimalFirstRunBubblePref() {
PrefService* local_state = g_browser_process->local_state();
if (!local_state)
return false;
if (!local_state->FindPreference(prefs::kShouldUseMinimalFirstRunBubble)) {
local_state->RegisterBooleanPref(prefs::kShouldUseMinimalFirstRunBubble,
false);
local_state->SetBoolean(prefs::kShouldUseMinimalFirstRunBubble, true);
}
return true;
}
// static
int FirstRun::ImportFromFile(Profile* profile, const CommandLine& cmdline) {
FilePath file_path = cmdline.GetSwitchValuePath(switches::kImportFromFile);
if (file_path.empty()) {
NOTREACHED();
return false;
}
scoped_refptr<ImporterHost> importer_host = new ImporterHost();
FirstRunImportObserver observer;
importer_host->set_headless();
ProfileInfo profile_info;
profile_info.browser_type = importer::BOOKMARKS_HTML;
profile_info.source_path = file_path;
StartImportingWithUI(
NULL,
importer::FAVORITES,
importer_host,
profile_info,
profile,
&observer,
true);
observer.RunLoop();
return observer.import_result();
}
// static
bool FirstRun::GetFirstRunSentinelFilePath(FilePath* path) {
FilePath first_run_sentinel;
#if defined(OS_WIN)
FilePath exe_path;
if (!PathService::Get(base::DIR_EXE, &exe_path))
return false;
if (InstallUtil::IsPerUserInstall(exe_path.value().c_str())) {
first_run_sentinel = exe_path;
} else {
if (!PathService::Get(chrome::DIR_USER_DATA, &first_run_sentinel))
return false;
}
#else
if (!PathService::Get(chrome::DIR_USER_DATA, &first_run_sentinel))
return false;
#endif
*path = first_run_sentinel.AppendASCII(kSentinelFile);
return true;
}
#if (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS)
// static
void Upgrade::RelaunchChromeBrowserWithNewCommandLineIfNeeded() {
if (new_command_line_) {
if (!RelaunchChromeBrowser(*new_command_line_)) {
DLOG(ERROR) << "Launching a new instance of the browser failed.";
} else {
DLOG(WARNING) << "Launched a new instance of the browser.";
}
delete new_command_line_;
new_command_line_ = NULL;
}
}
#endif // (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS)
int FirstRunImportObserver::import_result() const {
return import_result_;
}
void FirstRunImportObserver::ImportCanceled() {
import_result_ = ResultCodes::IMPORTER_CANCEL;
Finish();
}
void FirstRunImportObserver::ImportComplete() {
import_result_ = ResultCodes::NORMAL_EXIT;
Finish();
}
void FirstRunImportObserver::RunLoop() {
loop_running_ = true;
MessageLoop::current()->Run();
}
void FirstRunImportObserver::Finish() {
if (loop_running_)
MessageLoop::current()->Quit();
}
// TODO(avi): port the relevant pieces and enable this.
#if !defined(OS_MACOSX)
// static
void FirstRun::AutoImport(
Profile* profile,
bool homepage_defined,
int import_items,
int dont_import_items,
bool search_engine_experiment,
bool randomize_search_engine_experiment,
ProcessSingleton* process_singleton) {
// We need to avoid dispatching new tabs when we are importing because
// that will lead to data corruption or a crash. Because there is no UI for
// the import process, we pass NULL as the window to bring to the foreground
// when a CopyData message comes in; this causes the message to be silently
// discarded, which is the correct behavior during the import process.
process_singleton->Lock(NULL);
PlatformSetup();
scoped_refptr<ImporterHost> importer_host;
importer_host = new ImporterHost();
// Do import if there is an available profile for us to import.
if (importer_host->GetAvailableProfileCount() > 0) {
// Don't show the warning dialog if import fails.
importer_host->set_headless();
int items = 0;
// History is always imported unless turned off in master_preferences.
if (!(dont_import_items & importer::HISTORY))
items = items | importer::HISTORY;
// Home page is imported in organic builds only unless turned off or
// defined in master_preferences.
if (IsOrganic()) {
if (!(dont_import_items & importer::HOME_PAGE) && !homepage_defined)
items = items | importer::HOME_PAGE;
} else {
if (import_items & importer::HOME_PAGE)
items = items | importer::HOME_PAGE;
}
// Search engines are imported in organic builds only unless overriden
// in master_preferences.
if (IsOrganic()) {
if (!(dont_import_items & importer::SEARCH_ENGINES))
items = items | importer::SEARCH_ENGINES;
} else {
if (import_items & importer::SEARCH_ENGINES)
items = items | importer::SEARCH_ENGINES;
}
// Bookmarks are never imported, unless turned on in master_preferences.
if (import_items & importer::FAVORITES)
items = items | importer::FAVORITES;
ImportSettings(profile, importer_host, items);
}
UserMetrics::RecordAction(UserMetricsAction("FirstRunDef_Accept"));
// Launch the search engine dialog only if build is organic.
if (IsOrganic()) {
// The home page string may be set in the preferences, but the user should
// initially use Chrome with the NTP as home page in organic builds.
profile->GetPrefs()->SetBoolean(prefs::kHomePageIsNewTabPage, true);
ShowFirstRunDialog(profile, randomize_search_engine_experiment);
}
FirstRun::SetShowFirstRunBubblePref(true);
// Set the first run bubble to minimal.
FirstRun::SetMinimalFirstRunBubblePref();
FirstRun::SetShowWelcomePagePref();
process_singleton->Unlock();
FirstRun::CreateSentinel();
}
#endif // !defined(OS_MACOSX)
|