Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / media_galleries / fileapi / picasa_finder.cc
blob5473a2a35ab6c07219876b2b5ed6b82146570901
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/media_galleries/fileapi/picasa_finder.h"
7 #if defined(OS_WIN)
8 #include <windows.h>
9 #endif
11 #include "base/base_paths.h"
12 #include "base/bind.h"
13 #include "base/files/file_util.h"
14 #include "base/path_service.h"
15 #include "base/strings/string16.h"
16 #include "chrome/common/media_galleries/picasa_types.h"
17 #include "components/storage_monitor/storage_info.h"
18 #include "content/public/browser/browser_thread.h"
20 #if defined(OS_WIN)
21 #include "base/win/registry.h"
22 #endif
24 namespace picasa {
26 #if defined(OS_WIN)
27 const wchar_t kPicasaRegistryPath[] =
28 L"Software\\Google\\Picasa\\Picasa2\\Preferences";
29 const wchar_t kPicasaRegistryAppDataPathKey[] = L"AppLocalDataPath";
30 #endif
32 namespace {
34 #if defined(OS_WIN)
35 base::FilePath GetCustomPicasaAppDataPathFromWinRegistry() {
36 base::win::RegKey key;
37 if (key.Open(HKEY_CURRENT_USER, kPicasaRegistryPath, KEY_READ) !=
38 ERROR_SUCCESS || !key.Valid()) {
39 return base::FilePath();
42 base::string16 value;
43 if (key.ReadValue(kPicasaRegistryAppDataPathKey, &value) != ERROR_SUCCESS)
44 return base::FilePath();
45 if (value.empty())
46 return base::FilePath();
48 return base::FilePath(value);
51 base::FilePath GetPicasaDatabasePath() {
52 base::FilePath path = GetCustomPicasaAppDataPathFromWinRegistry();
53 if (path.empty() && !PathService::Get(base::DIR_LOCAL_APP_DATA, &path))
54 return base::FilePath();
55 return MakePicasaDatabasePath(path);
57 #endif // OS_WIN
59 #if defined(OS_MACOSX)
60 base::FilePath GetPicasaDatabasePath() {
61 base::FilePath path = GetCustomPicasaAppDataPathFromMacPreferences();
62 if (path.empty() && !PathService::Get(base::DIR_APP_DATA, &path))
63 return base::FilePath();
64 return MakePicasaDatabasePath(path);
66 #endif // OS_MACOSX
68 // Returns path of Picasa's DB3 database directory. May only be called on
69 // threads that allow for disk IO, like the FILE thread or MediaTaskRunner.
70 base::FilePath FindPicasaDatabaseOnFileThread() {
71 DCHECK_CURRENTLY_ON(content::BrowserThread::FILE);
73 #if defined(OS_WIN) || defined(OS_MACOSX)
74 base::FilePath path = GetPicasaDatabasePath();
75 // Verify actual existence
76 if (!base::DirectoryExists(path))
77 path.clear();
79 return path;
80 #else
81 return base::FilePath();
82 #endif
85 void FinishOnOriginalThread(const DeviceIDCallback& callback,
86 const base::FilePath& database_path) {
87 std::string device_id;
88 if (!database_path.empty()) {
89 device_id = storage_monitor::StorageInfo::MakeDeviceId(
90 storage_monitor::StorageInfo::PICASA, database_path.AsUTF8Unsafe());
92 callback.Run(device_id);
95 } // namespace
97 void FindPicasaDatabase(const DeviceIDCallback& callback) {
98 content::BrowserThread::PostTaskAndReplyWithResult(
99 content::BrowserThread::FILE,
100 FROM_HERE,
101 base::Bind(&FindPicasaDatabaseOnFileThread),
102 base::Bind(&FinishOnOriginalThread, callback));
105 base::FilePath MakePicasaDatabasePath(
106 const base::FilePath& picasa_app_data_path) {
107 #if defined(OS_WIN)
108 return picasa_app_data_path.AppendASCII("Google").AppendASCII("Picasa2")
109 .AppendASCII(kPicasaDatabaseDirName);
110 #elif defined(OS_MACOSX)
111 return picasa_app_data_path.AppendASCII("Google").AppendASCII("Picasa3")
112 .AppendASCII(kPicasaDatabaseDirName);
113 #else
114 return base::FilePath();
115 #endif
118 } // namespace picasa