arc_intent_helper_bridge.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_ARC_INTENT_HELPER_ARC_INTENT_HELPER_BRIDGE_H_
  5. #define COMPONENTS_ARC_INTENT_HELPER_ARC_INTENT_HELPER_BRIDGE_H_
  6. #include <map>
  7. #include <memory>
  8. #include <set>
  9. #include <string>
  10. #include <vector>
  11. #include "ash/components/arc/mojom/intent_helper.mojom.h"
  12. #include "base/memory/weak_ptr.h"
  13. #include "base/observer_list.h"
  14. #include "base/threading/thread_checker.h"
  15. #include "components/arc/common/intent_helper/arc_icon_cache_delegate.h"
  16. #include "components/arc/intent_helper/arc_intent_helper_observer.h"
  17. #include "components/keyed_service/core/keyed_service.h"
  18. #include "url/gurl.h"
  19. class BrowserContextKeyedServiceFactory;
  20. namespace content {
  21. class BrowserContext;
  22. } // namespace content
  23. namespace arc {
  24. class AdaptiveIconDelegate;
  25. class ArcBridgeService;
  26. class ControlCameraAppDelegate;
  27. class IntentFilter;
  28. class OpenUrlDelegate;
  29. // Receives intents from ARC.
  30. class ArcIntentHelperBridge : public KeyedService,
  31. public mojom::IntentHelperHost,
  32. public ArcIconCacheDelegate {
  33. public:
  34. class Delegate {
  35. public:
  36. virtual ~Delegate() = default;
  37. // Resets ARC; this wipes all user data, stops ARC, then
  38. // re-enables ARC.
  39. virtual void ResetArc() = 0;
  40. };
  41. // Returns singleton instance for the given BrowserContext,
  42. // or nullptr if the browser |context| is not allowed to use ARC.
  43. static ArcIntentHelperBridge* GetForBrowserContext(
  44. content::BrowserContext* context);
  45. static ArcIntentHelperBridge* GetForBrowserContextForTesting(
  46. content::BrowserContext* context);
  47. // Returns factory for the ArcIntentHelperBridge.
  48. static BrowserContextKeyedServiceFactory* GetFactory();
  49. // Appends '.' + |to_append| to the intent helper package name.
  50. static std::string AppendStringToIntentHelperPackageName(
  51. const std::string& to_append);
  52. static void SetOpenUrlDelegate(OpenUrlDelegate* delegate);
  53. static void SetControlCameraAppDelegate(ControlCameraAppDelegate* delegate);
  54. // Sets the Delegate instance.
  55. void SetDelegate(std::unique_ptr<Delegate> delegate);
  56. ArcIntentHelperBridge(content::BrowserContext* context,
  57. ArcBridgeService* bridge_service);
  58. ArcIntentHelperBridge(const ArcIntentHelperBridge&) = delete;
  59. ArcIntentHelperBridge& operator=(const ArcIntentHelperBridge&) = delete;
  60. ~ArcIntentHelperBridge() override;
  61. // KeyedService:
  62. void Shutdown() override;
  63. // mojom::IntentHelperHost
  64. void OnIconInvalidated(const std::string& package_name) override;
  65. void OnIntentFiltersUpdated(
  66. std::vector<IntentFilter> intent_filters) override;
  67. void OnOpenDownloads() override;
  68. void OnOpenUrl(const std::string& url) override;
  69. void OnOpenCustomTab(const std::string& url,
  70. int32_t task_id,
  71. OnOpenCustomTabCallback callback) override;
  72. void OnOpenChromePage(mojom::ChromePage page) override;
  73. void FactoryResetArc() override;
  74. void OpenWallpaperPicker() override;
  75. void OpenVolumeControl() override;
  76. void OnOpenWebApp(const std::string& url) override;
  77. void RecordShareFilesMetricsDeprecated(mojom::ShareFiles flag) override;
  78. void LaunchCameraApp(uint32_t intent_id,
  79. arc::mojom::CameraIntentMode mode,
  80. bool should_handle_result,
  81. bool should_down_scale,
  82. bool is_secure,
  83. int32_t task_id) override;
  84. void OnIntentFiltersUpdatedForPackage(
  85. const std::string& package_name,
  86. std::vector<IntentFilter> intent_filters) override;
  87. void CloseCameraApp() override;
  88. void IsChromeAppEnabled(arc::mojom::ChromeApp app,
  89. IsChromeAppEnabledCallback callback) override;
  90. void OnSupportedLinksChanged(
  91. std::vector<arc::mojom::SupportedLinksPtr> added_packages,
  92. std::vector<arc::mojom::SupportedLinksPtr> removed_packages,
  93. arc::mojom::SupportedLinkChangeSource source) override;
  94. void OnDownloadAdded(const std::string& relative_path,
  95. const std::string& owner_package_name) override;
  96. void OnOpenAppWithIntent(const GURL& start_url,
  97. arc::mojom::LaunchIntentPtr intent) override;
  98. void OnOpenGlobalActions() override;
  99. void OnCloseSystemDialogs() override;
  100. // ArcIconCacheDelegete:
  101. GetResult GetActivityIcons(const std::vector<ActivityName>& activities,
  102. OnIconsReadyCallback callback) override;
  103. void SetAdaptiveIconDelegate(AdaptiveIconDelegate* delegate);
  104. void AddObserver(ArcIntentHelperObserver* observer);
  105. void RemoveObserver(ArcIntentHelperObserver* observer);
  106. bool HasObserver(ArcIntentHelperObserver* observer) const;
  107. void HandleCameraResult(
  108. uint32_t intent_id,
  109. arc::mojom::CameraIntentAction action,
  110. const std::vector<uint8_t>& data,
  111. arc::mojom::IntentHelperInstance::HandleCameraResultCallback callback);
  112. void SendNewCaptureBroadcast(bool is_video, std::string file_path);
  113. // Filters out handlers that belong to the intent_helper apk and returns
  114. // a new array.
  115. static std::vector<mojom::IntentHandlerInfoPtr> FilterOutIntentHelper(
  116. std::vector<mojom::IntentHandlerInfoPtr> handlers);
  117. const std::vector<IntentFilter>& GetIntentFilterForPackage(
  118. const std::string& package_name);
  119. private:
  120. THREAD_CHECKER(thread_checker_);
  121. content::BrowserContext* const context_;
  122. ArcBridgeService* const arc_bridge_service_; // Owned by ArcServiceManager.
  123. ActivityIconLoader icon_loader_;
  124. // A map of each package name to the intent filters for that package.
  125. // Used to determine if Chrome should handle a URL without handing off to
  126. // Android.
  127. // TODO(crbug.com/853604): Now the package name exists in the map key as well
  128. // as the IntentFilter struct, it is a duplication. Should update the ARC
  129. // mojom type to optimise the structure.
  130. std::map<std::string, std::vector<IntentFilter>> intent_filters_;
  131. base::ObserverList<ArcIntentHelperObserver>::Unchecked observer_list_;
  132. // Schemes that ARC is known to send via OnOpenUrl.
  133. const std::set<std::string> allowed_arc_schemes_;
  134. std::unique_ptr<Delegate> delegate_;
  135. base::WeakPtrFactory<ArcIntentHelperBridge> weak_ptr_factory_{this};
  136. };
  137. } // namespace arc
  138. #endif // COMPONENTS_ARC_INTENT_HELPER_ARC_INTENT_HELPER_BRIDGE_H_