Skip to content

Commit

Permalink
[Files] Implement methods to get device connection status
Browse files Browse the repository at this point in the history
The Files app currently keeps track of the drive connection status to
grey out unavailable files when offline.

To prepare for a similar functionality with the OneDrive volume,
implement the methods to keep track of the device network connection
status.

The DOM used a 'connection' attribute to reflect the Drive connection
status. We now have both 'device-connection' and 'drive-connection'.

Bug: b:293547300
Change-Id: I70cf77372a565cf1e13ca84f8de65d45873766e6
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4755690
Reviewed-by: Austin Tankiang <austinct@chromium.org>
Commit-Queue: Jeremie Boulic <jboulic@chromium.org>
Reviewed-by: Giovanni Ortuno Urquidi <ortuno@chromium.org>
Reviewed-by: Luciano Pacheco <lucmult@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1187663}
  • Loading branch information
Jérémie Boulic authored and Chromium LUCI CQ committed Aug 24, 2023
1 parent 99add8d commit d8bcb9d
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 12 deletions.
19 changes: 19 additions & 0 deletions chrome/browser/ash/extensions/file_manager/event_router.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
#include "components/prefs/pref_service.h"
#include "components/services/app_service/public/cpp/app_registry_cache.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/network_service_instance.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/storage_partition.h"
#include "extensions/browser/event_router.h"
Expand Down Expand Up @@ -701,6 +702,8 @@ void EventRouter::Shutdown() {
dlp_client->RemoveObserver(this);
}

content::GetNetworkConnectionTracker()->RemoveNetworkConnectionObserver(this);

profile_ = nullptr;
}

Expand Down Expand Up @@ -818,6 +821,8 @@ void EventRouter::ObserveEvents() {
if (dlp_client) {
dlp_client->AddObserver(this);
}

content::GetNetworkConnectionTracker()->AddNetworkConnectionObserver(this);
}

// File watch setup routines.
Expand Down Expand Up @@ -1617,4 +1622,18 @@ void EventRouter::OnAppRegistryCacheWillBeDestroyed(
app_registry_cache_observer_.Reset();
}

void EventRouter::OnConnectionChanged(
const network::mojom::ConnectionType type) {
file_manager_private::DeviceConnectionState result =
content::GetNetworkConnectionTracker()->IsOffline()
? file_manager_private::DEVICE_CONNECTION_STATE_OFFLINE
: file_manager_private::DEVICE_CONNECTION_STATE_ONLINE;
BroadcastEvent(
profile_,
extensions::events::
FILE_MANAGER_PRIVATE_ON_DEVICE_CONNECTION_STATUS_CHANGED,
file_manager_private::OnDeviceConnectionStatusChanged::kEventName,
file_manager_private::OnDeviceConnectionStatusChanged::Create(result));
}

} // namespace file_manager
30 changes: 18 additions & 12 deletions chrome/browser/ash/extensions/file_manager/event_router.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "components/keyed_service/core/keyed_service.h"
#include "components/services/app_service/public/cpp/app_registry_cache.h"
#include "extensions/browser/extension_registry_observer.h"
#include "services/network/public/cpp/network_connection_tracker.h"
#include "storage/browser/file_system/file_system_operation.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
#include "url/gurl.h"
Expand All @@ -62,18 +63,20 @@ namespace file_manager {

// Monitors changes in disk mounts, network connection state and preferences
// affecting File Manager. Dispatches appropriate File Browser events.
class EventRouter : public KeyedService,
public extensions::ExtensionRegistryObserver,
public ash::system::TimezoneSettings::Observer,
public VolumeManagerObserver,
public arc::ArcIntentHelperObserver,
public drive::DriveIntegrationServiceObserver,
public guest_os::GuestOsSharePath::Observer,
public ash::TabletModeObserver,
public file_manager::io_task::IOTaskController::Observer,
public guest_os::GuestOsMountProviderRegistry::Observer,
public chromeos::DlpClient::Observer,
public apps::AppRegistryCache::Observer {
class EventRouter
: public KeyedService,
public extensions::ExtensionRegistryObserver,
public ash::system::TimezoneSettings::Observer,
public VolumeManagerObserver,
public arc::ArcIntentHelperObserver,
public drive::DriveIntegrationServiceObserver,
public guest_os::GuestOsSharePath::Observer,
public ash::TabletModeObserver,
public file_manager::io_task::IOTaskController::Observer,
public guest_os::GuestOsMountProviderRegistry::Observer,
public chromeos::DlpClient::Observer,
public apps::AppRegistryCache::Observer,
public network::NetworkConnectionTracker::NetworkConnectionObserver {
public:
using DispatchDirectoryChangeEventImplCallback =
base::RepeatingCallback<void(const base::FilePath& virtual_path,
Expand Down Expand Up @@ -212,6 +215,9 @@ class EventRouter : public KeyedService,
void OnAppRegistryCacheWillBeDestroyed(
apps::AppRegistryCache* cache) override;

// network::NetworkConnectionTracker::NetworkConnectionObserver:
void OnConnectionChanged(const network::mojom::ConnectionType type) override;

// Use this method for unit tests to bypass checking if there are any SWA
// windows.
void ForceBroadcastingForTesting(bool enabled) {
Expand Down
13 changes: 13 additions & 0 deletions chrome/browser/ash/extensions/file_manager/private_api_misc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
#include "components/signin/public/identity_manager/identity_manager.h"
#include "components/user_manager/user_manager.h"
#include "components/zoom/page_zoom.h"
#include "content/public/browser/network_service_instance.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/page_zoom.h"
#include "extensions/browser/api/file_handlers/mime_util.h"
Expand Down Expand Up @@ -1148,4 +1149,16 @@ FileManagerPrivateSendFeedbackFunction::Run() {
return RespondNow(NoArguments());
}

ExtensionFunction::ResponseAction
FileManagerPrivateGetDeviceConnectionStateFunction::Run() {
api::file_manager_private::DeviceConnectionState result =
content::GetNetworkConnectionTracker()->IsOffline()
? api::file_manager_private::DEVICE_CONNECTION_STATE_OFFLINE
: api::file_manager_private::DEVICE_CONNECTION_STATE_ONLINE;

return RespondNow(ArgumentList(
api::file_manager_private::GetDeviceConnectionState::Results::Create(
result)));
}

} // namespace extensions
13 changes: 13 additions & 0 deletions chrome/browser/ash/extensions/file_manager/private_api_misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,19 @@ class FileManagerPrivateSendFeedbackFunction : public ExtensionFunction {
ResponseAction Run() override;
};

// Implements the chrome.fileManagerPrivate.getDeviceConnectionState method.
class FileManagerPrivateGetDeviceConnectionStateFunction
: public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("fileManagerPrivate.getDeviceConnectionState",
FILEMANAGERPRIVATE_GETDEVICECONNECTIONSTATE)

protected:
~FileManagerPrivateGetDeviceConnectionStateFunction() override = default;

ResponseAction Run() override;
};

} // namespace extensions

#endif // CHROME_BROWSER_ASH_EXTENSIONS_FILE_MANAGER_PRIVATE_API_MISC_H_
11 changes: 11 additions & 0 deletions chrome/common/extensions/api/file_manager_private.idl
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ enum VolumeType { drive, downloads, removable, archive, provided, mtp,
// Device type. Available if this is removable volume.
enum DeviceType { usb, sd, optical, mobile, unknown };

// List of device connection statuses.
enum DeviceConnectionState {OFFLINE, ONLINE};

// List of connection types of drive.
enum DriveConnectionStateType {OFFLINE, METERED, ONLINE};

Expand Down Expand Up @@ -1387,6 +1390,8 @@ callback SearchFilesCallback = void([instanceOf=Entry] object[] entries);
// contain at most one path per hash.
callback SearchFilesByHashesCallback = void(object paths);

callback GetDeviceConnectionStateCallback = void(DeviceConnectionState result);

callback GetDriveConnectionStateCallback = void(DriveConnectionState result);

// |result| true if the length is in the valid range, false otherwise.
Expand Down Expand Up @@ -1732,6 +1737,10 @@ interface Functions {
static void searchFiles(SearchMetadataParams searchParams,
SearchFilesCallback callback);

// Retrieves the current device connection status.
// |callback|
static void getDeviceConnectionState(GetDeviceConnectionStateCallback callback);

// Retrieves the state of the current drive connection.
// |callback|
[supportsPromises]
Expand Down Expand Up @@ -2010,6 +2019,8 @@ interface Events {

static void onPreferencesChanged();

static void onDeviceConnectionStatusChanged(DeviceConnectionState state);

static void onDriveConnectionStatusChanged();

static void onDeviceChanged(DeviceEvent event);
Expand Down
1 change: 1 addition & 0 deletions extensions/browser/extension_event_histogram_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,7 @@ enum HistogramValue {
OS_DIAGNOSTICS_ON_MEMORY_ROUTINE_FINISHED = 542,
OS_EVENTS_ON_TOUCHSCREEN_TOUCH_EVENT = 543,
OS_EVENTS_ON_TOUCHSCREEN_CONNECTED_EVENT = 544,
FILE_MANAGER_PRIVATE_ON_DEVICE_CONNECTION_STATUS_CHANGED = 545,
// Last entry: Add new entries above, then run:
// tools/metrics/histograms/update_extension_histograms.py
ENUM_BOUNDARY
Expand Down
1 change: 1 addition & 0 deletions extensions/browser/extension_function_histogram_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -1882,6 +1882,7 @@ enum HistogramValue {
FILEMANAGERPRIVATE_DISMISSIOTASK = 1820,
ACCESSIBILITY_PRIVATE_SHOWTOAST = 1821,
USERSCRIPTS_GETSCRIPTS = 1822,
FILEMANAGERPRIVATE_GETDEVICECONNECTIONSTATE = 1823,
// Last entry: Add new entries above, then run:
// tools/metrics/histograms/update_extension_histograms.py
ENUM_BOUNDARY
Expand Down
20 changes: 20 additions & 0 deletions third_party/closure_compiler/externs/file_manager_private.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ chrome.fileManagerPrivate.DeviceType = {
UNKNOWN: 'unknown',
};

/**
* @enum {string}
*/
chrome.fileManagerPrivate.DeviceConnectionState = {
OFFLINE: 'OFFLINE',
ONLINE: 'ONLINE',
};

/**
* @enum {string}
*/
Expand Down Expand Up @@ -1366,6 +1374,13 @@ chrome.fileManagerPrivate.searchFilesByHashes = function(volumeId, hashList, cal
*/
chrome.fileManagerPrivate.searchFiles = function(searchParams, callback) {};

/**
* Retrieves the state of the current device connection. |callback|
* @param {function(!chrome.fileManagerPrivate.DeviceConnectionState): void}
* callback
*/
chrome.fileManagerPrivate.getDeviceConnectionState = function(callback) {};

/**
* Retrieves the state of the current drive connection. |callback|
* @param {function(!chrome.fileManagerPrivate.DriveConnectionState): void}
Expand Down Expand Up @@ -1766,6 +1781,11 @@ chrome.fileManagerPrivate.onDirectoryChanged;
*/
chrome.fileManagerPrivate.onPreferencesChanged;

/**
* @type {!ChromeEvent}
*/
chrome.fileManagerPrivate.onDeviceConnectionStatusChanged;

/**
* @type {!ChromeEvent}
*/
Expand Down
3 changes: 3 additions & 0 deletions tools/metrics/histograms/enums.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35684,6 +35684,8 @@ Called by update_extension_histograms.py.-->
<int value="542" label="OS_DIAGNOSTICS_ON_MEMORY_ROUTINE_FINISHED"/>
<int value="543" label="OS_EVENTS_ON_TOUCHSCREEN_TOUCH_EVENT"/>
<int value="544" label="OS_EVENTS_ON_TOUCHSCREEN_CONNECTED_EVENT"/>
<int value="545"
label="FILE_MANAGER_PRIVATE_ON_DEVICE_CONNECTION_STATUS_CHANGED"/>
</enum>

<enum name="ExtensionFileWriteResult">
Expand Down Expand Up @@ -37602,6 +37604,7 @@ Called by update_extension_histograms.py.-->
<int value="1820" label="FILEMANAGERPRIVATE_DISMISSIOTASK"/>
<int value="1821" label="ACCESSIBILITY_PRIVATE_SHOWTOAST"/>
<int value="1822" label="USERSCRIPTS_GETSCRIPTS"/>
<int value="1823" label="FILEMANAGERPRIVATE_GETDEVICECONNECTIONSTATE"/>
</enum>

<enum name="ExtensionIconState">
Expand Down
15 changes: 15 additions & 0 deletions ui/file_manager/file_manager/foreground/js/file_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,12 @@ export class FileManager extends EventTarget {
store.init(getEmptyState());
this.initUIFocus_();
metrics.recordInterval('Load.InitUI');

chrome.fileManagerPrivate.onDeviceConnectionStatusChanged.addListener(
this.updateDeviceConnectionState_.bind(this));
chrome.fileManagerPrivate.getDeviceConnectionState(
this.updateDeviceConnectionState_.bind(this));

return fileSystemUIPromise;
}

Expand Down Expand Up @@ -1798,6 +1804,15 @@ export class FileManager extends EventTarget {
}
}

/**
* Invoked when the device connection status changes.
* @param {chrome.fileManagerPrivate.DeviceConnectionState} state
* @private
*/
updateDeviceConnectionState_(state) {
// TODO(jboulic): Update device connection state in the store.
}

/**
* Toggles the trash root visibility when the `trashEnabled` preference is
* updated.
Expand Down

0 comments on commit d8bcb9d

Please sign in to comment.