OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/note_taking_helper.h" | 5 #include "chrome/browser/chromeos/note_taking_helper.h" |
6 | 6 |
| 7 #include <algorithm> |
7 #include <utility> | 8 #include <utility> |
8 | 9 |
9 #include "apps/launcher.h" | 10 #include "apps/launcher.h" |
10 #include "ash/common/system/chromeos/palette/palette_utils.h" | 11 #include "ash/common/system/chromeos/palette/palette_utils.h" |
11 #include "base/bind.h" | 12 #include "base/bind.h" |
12 #include "base/command_line.h" | 13 #include "base/command_line.h" |
13 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
14 #include "base/logging.h" | 15 #include "base/logging.h" |
15 #include "base/memory/ptr_util.h" | 16 #include "base/memory/ptr_util.h" |
16 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
17 #include "base/strings/string_split.h" | 18 #include "base/strings/string_split.h" |
| 19 #include "chrome/browser/browser_process.h" |
| 20 #include "chrome/browser/chrome_notification_types.h" |
18 #include "chrome/browser/chromeos/file_manager/path_util.h" | 21 #include "chrome/browser/chromeos/file_manager/path_util.h" |
19 #include "chrome/browser/profiles/profile.h" | 22 #include "chrome/browser/profiles/profile.h" |
| 23 #include "chrome/browser/profiles/profile_manager.h" |
20 #include "chrome/common/pref_names.h" | 24 #include "chrome/common/pref_names.h" |
21 #include "chromeos/chromeos_switches.h" | 25 #include "chromeos/chromeos_switches.h" |
22 #include "components/arc/arc_bridge_service.h" | 26 #include "components/arc/arc_bridge_service.h" |
23 #include "components/arc/common/intent_helper.mojom.h" | 27 #include "components/arc/common/intent_helper.mojom.h" |
24 #include "components/prefs/pref_service.h" | 28 #include "components/prefs/pref_service.h" |
25 #include "content/public/browser/browser_thread.h" | 29 #include "content/public/browser/browser_thread.h" |
| 30 #include "content/public/browser/notification_service.h" |
26 #include "extensions/browser/extension_registry.h" | 31 #include "extensions/browser/extension_registry.h" |
27 #include "extensions/common/api/app_runtime.h" | 32 #include "extensions/common/api/app_runtime.h" |
28 #include "extensions/common/extension.h" | 33 #include "extensions/common/extension.h" |
29 #include "url/gurl.h" | 34 #include "url/gurl.h" |
30 | 35 |
31 namespace app_runtime = extensions::api::app_runtime; | 36 namespace app_runtime = extensions::api::app_runtime; |
32 | 37 |
33 namespace chromeos { | 38 namespace chromeos { |
34 namespace { | 39 namespace { |
35 | 40 |
(...skipping 29 matching lines...) Expand all Loading... |
65 // Creates a new Mojo IntentInfo struct for launching an Android note-taking app | 70 // Creates a new Mojo IntentInfo struct for launching an Android note-taking app |
66 // with an optional ClipData URI. | 71 // with an optional ClipData URI. |
67 arc::mojom::IntentInfoPtr CreateIntentInfo(const GURL& clip_data_uri) { | 72 arc::mojom::IntentInfoPtr CreateIntentInfo(const GURL& clip_data_uri) { |
68 arc::mojom::IntentInfoPtr intent = arc::mojom::IntentInfo::New(); | 73 arc::mojom::IntentInfoPtr intent = arc::mojom::IntentInfo::New(); |
69 intent->action = NoteTakingHelper::kIntentAction; | 74 intent->action = NoteTakingHelper::kIntentAction; |
70 if (!clip_data_uri.is_empty()) | 75 if (!clip_data_uri.is_empty()) |
71 intent->clip_data_uri = clip_data_uri.spec(); | 76 intent->clip_data_uri = clip_data_uri.spec(); |
72 return intent; | 77 return intent; |
73 } | 78 } |
74 | 79 |
75 // Returns all installed and enabled whitelisted Chrome note-taking apps. | |
76 std::vector<const extensions::Extension*> GetChromeApps(Profile* profile) { | |
77 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
78 // TODO(derat): Query for additional Chrome apps once http://crbug.com/657139 | |
79 // is resolved. | |
80 std::vector<extensions::ExtensionId> ids; | |
81 const std::string switch_value = | |
82 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | |
83 switches::kNoteTakingAppIds); | |
84 if (!switch_value.empty()) { | |
85 ids = base::SplitString(switch_value, ",", base::TRIM_WHITESPACE, | |
86 base::SPLIT_WANT_NONEMPTY); | |
87 } | |
88 ids.insert(ids.end(), kExtensionIds, | |
89 kExtensionIds + arraysize(kExtensionIds)); | |
90 | |
91 const extensions::ExtensionRegistry* extension_registry = | |
92 extensions::ExtensionRegistry::Get(profile); | |
93 const extensions::ExtensionSet& enabled_extensions = | |
94 extension_registry->enabled_extensions(); | |
95 | |
96 std::vector<const extensions::Extension*> extensions; | |
97 for (const auto& id : ids) { | |
98 if (enabled_extensions.Contains(id)) { | |
99 extensions.push_back(extension_registry->GetExtensionById( | |
100 id, extensions::ExtensionRegistry::ENABLED)); | |
101 } | |
102 } | |
103 return extensions; | |
104 } | |
105 | |
106 } // namespace | 80 } // namespace |
107 | 81 |
108 const char NoteTakingHelper::kIntentAction[] = | 82 const char NoteTakingHelper::kIntentAction[] = |
109 "org.chromium.arc.intent.action.CREATE_NOTE"; | 83 "org.chromium.arc.intent.action.CREATE_NOTE"; |
110 const char NoteTakingHelper::kDevKeepExtensionId[] = | 84 const char NoteTakingHelper::kDevKeepExtensionId[] = |
111 "ogfjaccbdfhecploibfbhighmebiffla"; | 85 "ogfjaccbdfhecploibfbhighmebiffla"; |
112 const char NoteTakingHelper::kProdKeepExtensionId[] = | 86 const char NoteTakingHelper::kProdKeepExtensionId[] = |
113 "hmjkmjkepdijhoojdojkdfohbdgmmhki"; | 87 "hmjkmjkepdijhoojdojkdfohbdgmmhki"; |
114 | 88 |
115 // static | 89 // static |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 android_apps_.clear(); | 185 android_apps_.clear(); |
212 android_apps_received_ = false; | 186 android_apps_received_ = false; |
213 } | 187 } |
214 for (auto& observer : observers_) | 188 for (auto& observer : observers_) |
215 observer.OnAvailableNoteTakingAppsUpdated(); | 189 observer.OnAvailableNoteTakingAppsUpdated(); |
216 } | 190 } |
217 | 191 |
218 NoteTakingHelper::NoteTakingHelper() | 192 NoteTakingHelper::NoteTakingHelper() |
219 : launch_chrome_app_callback_( | 193 : launch_chrome_app_callback_( |
220 base::Bind(&apps::LaunchPlatformAppWithAction)), | 194 base::Bind(&apps::LaunchPlatformAppWithAction)), |
| 195 extension_registry_observer_(this), |
221 weak_ptr_factory_(this) { | 196 weak_ptr_factory_(this) { |
222 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 197 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
223 | 198 |
| 199 const std::string switch_value = |
| 200 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 201 switches::kNoteTakingAppIds); |
| 202 if (!switch_value.empty()) { |
| 203 whitelisted_chrome_app_ids_ = base::SplitString( |
| 204 switch_value, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); |
| 205 } |
| 206 whitelisted_chrome_app_ids_.insert(whitelisted_chrome_app_ids_.end(), |
| 207 kExtensionIds, |
| 208 kExtensionIds + arraysize(kExtensionIds)); |
| 209 |
| 210 // Track profiles so we can observe their extension registries. |
| 211 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_ADDED, |
| 212 content::NotificationService::AllBrowserContextsAndSources()); |
| 213 for (Profile* profile : |
| 214 g_browser_process->profile_manager()->GetLoadedProfiles()) { |
| 215 extension_registry_observer_.Add( |
| 216 extensions::ExtensionRegistry::Get(profile)); |
| 217 } |
| 218 |
224 // Check if the primary profile has already enabled ARC and watch for changes. | 219 // Check if the primary profile has already enabled ARC and watch for changes. |
225 auto session_manager = arc::ArcSessionManager::Get(); | 220 auto session_manager = arc::ArcSessionManager::Get(); |
226 session_manager->AddObserver(this); | 221 session_manager->AddObserver(this); |
227 android_enabled_ = session_manager->IsArcEnabled(); | 222 android_enabled_ = session_manager->IsArcEnabled(); |
228 | 223 |
229 // ArcServiceManager will notify us about changes to the list of available | 224 // ArcServiceManager will notify us about changes to the list of available |
230 // Android apps. | 225 // Android apps. |
231 auto service_manager = arc::ArcServiceManager::Get(); | 226 auto service_manager = arc::ArcServiceManager::Get(); |
232 service_manager->AddObserver(this); | 227 service_manager->AddObserver(this); |
233 | 228 |
234 // If the ARC intent helper is ready, get the Android apps. Otherwise, | 229 // If the ARC intent helper is ready, get the Android apps. Otherwise, |
235 // UpdateAndroidApps() will be called when ArcServiceManager calls | 230 // UpdateAndroidApps() will be called when ArcServiceManager calls |
236 // OnIntentFiltersUpdated(). | 231 // OnIntentFiltersUpdated(). |
237 if (android_enabled_ && | 232 if (android_enabled_ && |
238 arc::ArcServiceManager::Get() | 233 arc::ArcServiceManager::Get() |
239 ->arc_bridge_service() | 234 ->arc_bridge_service() |
240 ->intent_helper() | 235 ->intent_helper() |
241 ->has_instance()) | 236 ->has_instance()) |
242 UpdateAndroidApps(); | 237 UpdateAndroidApps(); |
243 } | 238 } |
244 | 239 |
245 NoteTakingHelper::~NoteTakingHelper() { | 240 NoteTakingHelper::~NoteTakingHelper() { |
246 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 241 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 242 |
247 // ArcSessionManagerTest shuts down ARC before NoteTakingHelper. | 243 // ArcSessionManagerTest shuts down ARC before NoteTakingHelper. |
248 if (arc::ArcServiceManager::Get()) | 244 if (arc::ArcServiceManager::Get()) |
249 arc::ArcServiceManager::Get()->RemoveObserver(this); | 245 arc::ArcServiceManager::Get()->RemoveObserver(this); |
250 if (arc::ArcSessionManager::Get()) | 246 if (arc::ArcSessionManager::Get()) |
251 arc::ArcSessionManager::Get()->RemoveObserver(this); | 247 arc::ArcSessionManager::Get()->RemoveObserver(this); |
252 } | 248 } |
253 | 249 |
| 250 bool NoteTakingHelper::IsWhitelistedChromeApp( |
| 251 const extensions::Extension* extension) const { |
| 252 DCHECK(extension); |
| 253 return std::find(whitelisted_chrome_app_ids_.begin(), |
| 254 whitelisted_chrome_app_ids_.end(), |
| 255 extension->id()) != whitelisted_chrome_app_ids_.end(); |
| 256 } |
| 257 |
| 258 std::vector<const extensions::Extension*> NoteTakingHelper::GetChromeApps( |
| 259 Profile* profile) const { |
| 260 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 261 const extensions::ExtensionRegistry* extension_registry = |
| 262 extensions::ExtensionRegistry::Get(profile); |
| 263 const extensions::ExtensionSet& enabled_extensions = |
| 264 extension_registry->enabled_extensions(); |
| 265 |
| 266 // TODO(derat): Query for additional Chrome apps once http://crbug.com/657139 |
| 267 // is resolved. |
| 268 std::vector<const extensions::Extension*> extensions; |
| 269 for (const auto& id : whitelisted_chrome_app_ids_) { |
| 270 if (enabled_extensions.Contains(id)) { |
| 271 extensions.push_back(extension_registry->GetExtensionById( |
| 272 id, extensions::ExtensionRegistry::ENABLED)); |
| 273 } |
| 274 } |
| 275 return extensions; |
| 276 } |
| 277 |
254 void NoteTakingHelper::UpdateAndroidApps() { | 278 void NoteTakingHelper::UpdateAndroidApps() { |
255 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 279 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
256 auto* helper = GetIntentHelper("RequestIntentHandlerList", 12); | 280 auto* helper = GetIntentHelper("RequestIntentHandlerList", 12); |
257 if (!helper) | 281 if (!helper) |
258 return; | 282 return; |
259 helper->RequestIntentHandlerList( | 283 helper->RequestIntentHandlerList( |
260 CreateIntentInfo(GURL()), base::Bind(&NoteTakingHelper::OnGotAndroidApps, | 284 CreateIntentInfo(GURL()), base::Bind(&NoteTakingHelper::OnGotAndroidApps, |
261 weak_ptr_factory_.GetWeakPtr())); | 285 weak_ptr_factory_.GetWeakPtr())); |
262 } | 286 } |
263 | 287 |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
320 return false; | 344 return false; |
321 } | 345 } |
322 auto action_data = base::MakeUnique<app_runtime::ActionData>(); | 346 auto action_data = base::MakeUnique<app_runtime::ActionData>(); |
323 action_data->action_type = app_runtime::ActionType::ACTION_TYPE_NEW_NOTE; | 347 action_data->action_type = app_runtime::ActionType::ACTION_TYPE_NEW_NOTE; |
324 launch_chrome_app_callback_.Run(profile, app, std::move(action_data), path); | 348 launch_chrome_app_callback_.Run(profile, app, std::move(action_data), path); |
325 } | 349 } |
326 | 350 |
327 return true; | 351 return true; |
328 } | 352 } |
329 | 353 |
| 354 void NoteTakingHelper::Observe(int type, |
| 355 const content::NotificationSource& source, |
| 356 const content::NotificationDetails& details) { |
| 357 DCHECK_EQ(type, chrome::NOTIFICATION_PROFILE_ADDED); |
| 358 auto registry = extensions::ExtensionRegistry::Get( |
| 359 content::Source<Profile>(source).ptr()); |
| 360 DCHECK(!extension_registry_observer_.IsObserving(registry)); |
| 361 extension_registry_observer_.Add(registry); |
| 362 } |
| 363 |
| 364 void NoteTakingHelper::OnExtensionLoaded( |
| 365 content::BrowserContext* browser_context, |
| 366 const extensions::Extension* extension) { |
| 367 if (IsWhitelistedChromeApp(extension)) { |
| 368 for (auto& observer : observers_) |
| 369 observer.OnAvailableNoteTakingAppsUpdated(); |
| 370 } |
| 371 } |
| 372 |
| 373 void NoteTakingHelper::OnExtensionUnloaded( |
| 374 content::BrowserContext* browser_context, |
| 375 const extensions::Extension* extension, |
| 376 extensions::UnloadedExtensionInfo::Reason reason) { |
| 377 if (IsWhitelistedChromeApp(extension)) { |
| 378 for (auto& observer : observers_) |
| 379 observer.OnAvailableNoteTakingAppsUpdated(); |
| 380 } |
| 381 } |
| 382 |
| 383 void NoteTakingHelper::OnShutdown(extensions::ExtensionRegistry* registry) { |
| 384 extension_registry_observer_.Remove(registry); |
| 385 } |
| 386 |
330 } // namespace chromeos | 387 } // namespace chromeos |
OLD | NEW |