Storage manager: Show Google Drive offline files.

This CL adds following functions:
- Show the total size of Google Drive offline files.
- Show a confirmation dialog from which users can delete evictable offline files.

BUG=591958
CQ_INCLUDE_TRYBOTS=tryserver.chromium.linux:closure_compilation

Review-Url: https://codereview.chromium.org/2070893003
Cr-Commit-Position: refs/heads/master@{#400575}
diff --git a/chrome/app/chromeos_strings.grdp b/chrome/app/chromeos_strings.grdp
index faa2d14..23c6c42 100644
--- a/chrome/app/chromeos_strings.grdp
+++ b/chrome/app/chromeos_strings.grdp
@@ -2545,6 +2545,12 @@
   <message name="IDS_OPTIONS_SETTINGS_STORAGE_MANAGER_TAB_TITLE" desc="Title for the storage manager tab.">
     Storage
   </message>
+  <message name="IDS_OPTIONS_SETTINGS_STORAGE_CLEAR_DRIVE_CACHE_TAB_TITLE" desc="Title for the storage manager confirmation dialog tab to remove Google Drive offline files.">
+    Offline files
+  </message>
+  <message name="IDS_OPTIONS_SETTINGS_STORAGE_CLEAR_DRIVE_CACHE_DESCRIPTION" desc="Description in the storage manager confirmation dialog to remove Google Drive offline files.">
+    You can free up space by deleting Google Drive offline files. Files marked as "Available offline" or in use will not be deleted.
+  </message>
   <message name="IDS_OPTIONS_SETTINGS_STORAGE_SIZE_CALCULATING" desc="In storage manager overlay, label for storage item's size indicating the size is being calculated.">
     Calculating...
   </message>
@@ -2563,9 +2569,15 @@
   <message name="IDS_OPTIONS_SETTINGS_STORAGE_SUBITEM_LABEL_DOWNLOADS" desc="In storage manager overlay, label for the size of Downloads directory.">
     Downloads
   </message>
+  <message name="IDS_OPTIONS_SETTINGS_STORAGE_SUBITEM_LABEL_DRIVE_CACHE" desc="In storage manager overlay, label for the size of Google Drive's offline files.">
+    Offline files
+  </message>
   <message name="IDS_OPTIONS_SETTINGS_STORAGE_SUBITEM_LABEL_ARC" desc="In storage manager overlay, label for the total size size of Android apps and cache.">
     Android apps and cache
   </message>
+  <message name="IDS_OPTIONS_SETTINGS_STORAGE_DELETE_ALL_BUTTON_TITLE" desc="In storage manager overlay, label for the total size size of Android apps and cache.">
+    Delete all
+  </message>
   <message name="IDS_OPTIONS_ACCOUNTS_ALLOW_BWSI_DESCRIPTION" desc="In the Accounts settings tab, the text on the checkbox to allow browse without signing in.">
     Enable Guest browsing
   </message>
diff --git a/chrome/browser/resources/options/chromeos/storage_clear_drive_cache_overlay.html b/chrome/browser/resources/options/chromeos/storage_clear_drive_cache_overlay.html
new file mode 100644
index 0000000..5bf2aa9
--- /dev/null
+++ b/chrome/browser/resources/options/chromeos/storage_clear_drive_cache_overlay.html
@@ -0,0 +1,14 @@
+<div id="clear-drive-cache-overlay-page" class="page" role="dialog" hidden>
+  <div class="close-button"></div>
+  <h1 i18n-content="storageClearDriveCachePage"></h1>
+  <div class="content-area">
+    <div i18n-content="storageClearDriveCacheDescription"></div>
+  </div>
+  <div class="action-area button-strip">
+    <button id="clear-drive-cache-overlay-cancel-button" i18n-content="cancel">
+    </button>
+    <button id="clear-drive-cache-overlay-delete-button" class="default-button"
+        i18n-content="storageDeleteAllButtonTitle">
+    </button>
+  </div>
+</div>
diff --git a/chrome/browser/resources/options/chromeos/storage_clear_drive_cache_overlay.js b/chrome/browser/resources/options/chromeos/storage_clear_drive_cache_overlay.js
new file mode 100644
index 0000000..38c3072
--- /dev/null
+++ b/chrome/browser/resources/options/chromeos/storage_clear_drive_cache_overlay.js
@@ -0,0 +1,37 @@
+// Copyright 2016 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.
+
+cr.define('options', function() {
+  var Page = cr.ui.pageManager.Page;
+  var PageManager = cr.ui.pageManager.PageManager;
+
+  function StorageClearDriveCacheOverlay() {
+    Page.call(this, 'storageClearDriveCache',
+              loadTimeData.getString('storageClearDriveCachePageTabTitle'),
+              'clear-drive-cache-overlay-page');
+  }
+
+  cr.addSingletonGetter(StorageClearDriveCacheOverlay);
+
+  StorageClearDriveCacheOverlay.prototype = {
+    __proto__: Page.prototype,
+
+    /** @override */
+    initializePage: function() {
+      Page.prototype.initializePage.call(this);
+
+      $('clear-drive-cache-overlay-delete-button').onclick = function(e) {
+        chrome.send('clearDriveCache');
+        PageManager.closeOverlay();
+      };
+      $('clear-drive-cache-overlay-cancel-button').onclick = function(e) {
+        PageManager.closeOverlay();
+      };
+    },
+  };
+
+  return {
+    StorageClearDriveCacheOverlay: StorageClearDriveCacheOverlay
+  };
+});
diff --git a/chrome/browser/resources/options/chromeos/storage_manager.html b/chrome/browser/resources/options/chromeos/storage_manager.html
index da5f6dd..97345b7 100644
--- a/chrome/browser/resources/options/chromeos/storage_manager.html
+++ b/chrome/browser/resources/options/chromeos/storage_manager.html
@@ -21,6 +21,14 @@
             class="storage-manager-item-size"
             i18n-content="storageSizeCalculating"></span>
     </div>
+    <div class="storage-manager-subitem">
+      <a is="action-link"
+         id="storage-manager-label-drive-cache"
+         i18n-content="storageSubitemLabelDriveCache"></a>
+      <span id="storage-manager-size-drive-cache"
+            class="storage-manager-item-size"
+            i18n-content="storageSizeCalculating"></span>
+    </div>
     <div id="storage-manager-item-arc"
          class="storage-manager-subitem" hidden>
       <a is="action-link"
diff --git a/chrome/browser/resources/options/chromeos/storage_manager.js b/chrome/browser/resources/options/chromeos/storage_manager.js
index 65aa199..426e9a8 100644
--- a/chrome/browser/resources/options/chromeos/storage_manager.js
+++ b/chrome/browser/resources/options/chromeos/storage_manager.js
@@ -34,6 +34,9 @@
       $('storage-manager-label-downloads').onclick = function() {
         chrome.send('openDownloads');
       };
+      $('storage-manager-label-drive-cache').onclick = function() {
+        PageManager.showPageByName('storageClearDriveCache');
+      };
       $('storage-manager-label-arc').onclick = function() {
         chrome.send('openArcStorage');
       };
@@ -72,6 +75,16 @@
     },
 
     /**
+     * Updates the size of Google Drive offline files.
+     * @param {string} size Formatted string of the size of Google Drive offline
+     *     files.
+     * @private
+     */
+    setDriveCacheSize_: function(size) {
+      $('storage-manager-size-drive-cache').textContent = size;
+    },
+
+    /**
      * Updates the total size of Android apps and cache.
      * @param {string} size Formatted string of the size of Android apps and
      * cache.
@@ -95,6 +108,7 @@
   cr.makePublic(StorageManager, [
     'setArcSize',
     'setDownloadsSize',
+    'setDriveCacheSize',
     'setSizeStat',
     'showArcItem',
   ]);
diff --git a/chrome/browser/resources/options/options.html b/chrome/browser/resources/options/options.html
index 82b3d365..23175cf 100644
--- a/chrome/browser/resources/options/options.html
+++ b/chrome/browser/resources/options/options.html
@@ -167,6 +167,7 @@
   <include src="chromeos/display_overscan.html">
   <include src="chromeos/internet_detail.html">
   <include src="chromeos/preferred_networks.html">
+  <include src="chromeos/storage_clear_drive_cache_overlay.html">
   <include src="chromeos/third_party_ime_confirm_overlay.html">
   <include src="../help/channel_change_page.html">
 </if>
diff --git a/chrome/browser/resources/options/options.js b/chrome/browser/resources/options/options.js
index 9387495..f950327 100644
--- a/chrome/browser/resources/options/options.js
+++ b/chrome/browser/resources/options/options.js
@@ -203,6 +203,8 @@
     PageManager.registerOverlay(ChangePictureOptions.getInstance(),
                                 BrowserOptions.getInstance(),
                                 [$('account-picture')]);
+    PageManager.registerOverlay(StorageClearDriveCacheOverlay.getInstance(),
+                                StorageManager.getInstance());
     PageManager.registerOverlay(ConsumerManagementOverlay.getInstance(),
                                 BrowserOptions.getInstance());
     PageManager.registerOverlay(DetailsInternetPage.getInstance(),
diff --git a/chrome/browser/resources/options/options_bundle.js b/chrome/browser/resources/options/options_bundle.js
index a458baa..d6a330c 100644
--- a/chrome/browser/resources/options/options_bundle.js
+++ b/chrome/browser/resources/options/options_bundle.js
@@ -39,6 +39,7 @@
 <include src="chromeos/display_overscan.js">
 <include src="chromeos/keyboard_overlay.js">
 <include src="chromeos/pointer_overlay.js">
+<include src="chromeos/storage_clear_drive_cache_overlay.js">
 <include src="chromeos/storage_manager.js">
 <include src="chromeos/third_party_ime_confirm_overlay.js">
 <include src="chromeos/power_overlay.js">
@@ -53,6 +54,7 @@
 var KeyboardOverlay = options.KeyboardOverlay;
 var PointerOverlay = options.PointerOverlay;
 var PowerOverlay = options.PowerOverlay;
+var StorageClearDriveCacheOverlay = options.StorageClearDriveCacheOverlay;
 var StorageManager = options.StorageManager;
 var UIAccountTweaks = uiAccountTweaks.UIAccountTweaks;
 </if>
diff --git a/chrome/browser/ui/webui/options/chromeos/storage_manager_handler.cc b/chrome/browser/ui/webui/options/chromeos/storage_manager_handler.cc
index 34afd55..d821454 100644
--- a/chrome/browser/ui/webui/options/chromeos/storage_manager_handler.cc
+++ b/chrome/browser/ui/webui/options/chromeos/storage_manager_handler.cc
@@ -10,10 +10,12 @@
 #include "base/sys_info.h"
 #include "chrome/browser/browser_process.h"
 #include "chrome/browser/chromeos/arc/arc_auth_service.h"
+#include "chrome/browser/chromeos/drive/file_system_util.h"
 #include "chrome/browser/chromeos/file_manager/path_util.h"
 #include "chrome/browser/platform_util.h"
 #include "chrome/browser/profiles/profile.h"
 #include "chrome/grit/generated_resources.h"
+#include "components/drive/chromeos/file_system_interface.h"
 #include "content/public/browser/browser_thread.h"
 #include "ui/base/l10n/l10n_util.h"
 #include "ui/base/text/bytes_formatting.h"
@@ -47,6 +49,8 @@
 
   RegisterTitle(localized_strings, "storageManagerPage",
                 IDS_OPTIONS_SETTINGS_STORAGE_MANAGER_TAB_TITLE);
+  RegisterTitle(localized_strings, "storageClearDriveCachePage",
+                IDS_OPTIONS_SETTINGS_STORAGE_CLEAR_DRIVE_CACHE_TAB_TITLE);
 
   localized_strings->SetString(
       "storageItemLabelCapacity", l10n_util::GetStringUTF16(
@@ -61,11 +65,20 @@
       "storageSubitemLabelDownloads", l10n_util::GetStringUTF16(
           IDS_OPTIONS_SETTINGS_STORAGE_SUBITEM_LABEL_DOWNLOADS));
   localized_strings->SetString(
+      "storageSubitemLabelDriveCache", l10n_util::GetStringUTF16(
+          IDS_OPTIONS_SETTINGS_STORAGE_SUBITEM_LABEL_DRIVE_CACHE));
+  localized_strings->SetString(
       "storageSubitemLabelArc", l10n_util::GetStringUTF16(
           IDS_OPTIONS_SETTINGS_STORAGE_SUBITEM_LABEL_ARC));
   localized_strings->SetString(
       "storageSizeCalculating", l10n_util::GetStringUTF16(
           IDS_OPTIONS_SETTINGS_STORAGE_SIZE_CALCULATING));
+  localized_strings->SetString(
+      "storageClearDriveCacheDescription", l10n_util::GetStringUTF16(
+          IDS_OPTIONS_SETTINGS_STORAGE_CLEAR_DRIVE_CACHE_DESCRIPTION));
+  localized_strings->SetString(
+      "storageDeleteAllButtonTitle", l10n_util::GetStringUTF16(
+          IDS_OPTIONS_SETTINGS_STORAGE_DELETE_ALL_BUTTON_TITLE));
 }
 
 void StorageManagerHandler::InitializePage() {
@@ -87,12 +100,17 @@
       "openArcStorage",
       base::Bind(&StorageManagerHandler::HandleOpenArcStorage,
                  base::Unretained(this)));
+  web_ui()->RegisterMessageCallback(
+      "clearDriveCache",
+      base::Bind(&StorageManagerHandler::HandleClearDriveCache,
+                 base::Unretained(this)));
 }
 
 void StorageManagerHandler::HandleUpdateStorageInfo(
     const base::ListValue* unused_args) {
   UpdateSizeStat();
   UpdateDownloadsSize();
+  UpdateDriveCacheSize();
   UpdateArcSize();
 }
 
@@ -113,6 +131,16 @@
   arc::ArcStorageManager::Get()->OpenPrivateVolumeSettings();
 }
 
+void StorageManagerHandler::HandleClearDriveCache(
+    const base::ListValue* unused_args) {
+  drive::FileSystemInterface* const file_system =
+      drive::util::GetFileSystemByProfile(Profile::FromWebUI(web_ui()));
+  file_system->FreeDiskSpaceIfNeededFor(
+      std::numeric_limits<int64_t>::max(),  // Removes as much as possible.
+      base::Bind(&StorageManagerHandler::OnClearDriveCacheDone,
+                 weak_ptr_factory_.GetWeakPtr()));
+}
+
 void StorageManagerHandler::UpdateSizeStat() {
   Profile* const profile = Profile::FromWebUI(web_ui());
   const base::FilePath downloads_path =
@@ -164,6 +192,20 @@
       base::StringValue(ui::FormatBytes(size)));
 }
 
+void StorageManagerHandler::UpdateDriveCacheSize() {
+  drive::FileSystemInterface* const file_system =
+      drive::util::GetFileSystemByProfile(Profile::FromWebUI(web_ui()));
+  file_system->CalculateCacheSize(
+      base::Bind(&StorageManagerHandler::OnGetDriveCacheSize,
+                 weak_ptr_factory_.GetWeakPtr()));
+}
+
+void StorageManagerHandler::OnGetDriveCacheSize(int64_t size) {
+  web_ui()->CallJavascriptFunctionUnsafe(
+      "options.StorageManager.setDriveCacheSize",
+      base::StringValue(ui::FormatBytes(size)));
+}
+
 void StorageManagerHandler::UpdateArcSize() {
   Profile* const profile = Profile::FromWebUI(web_ui());
   if (!arc::ArcAuthService::IsAllowedForProfile(profile) ||
@@ -202,5 +244,9 @@
                                          arc_size);
 }
 
+void StorageManagerHandler::OnClearDriveCacheDone(bool success) {
+  UpdateDriveCacheSize();
+}
+
 }  // namespace options
 }  // namespace chromeos
diff --git a/chrome/browser/ui/webui/options/chromeos/storage_manager_handler.h b/chrome/browser/ui/webui/options/chromeos/storage_manager_handler.h
index a4f3969..a8a1fc28 100644
--- a/chrome/browser/ui/webui/options/chromeos/storage_manager_handler.h
+++ b/chrome/browser/ui/webui/options/chromeos/storage_manager_handler.h
@@ -31,6 +31,7 @@
   void HandleUpdateStorageInfo(const base::ListValue* unused_args);
   void HandleOpenDownloads(const base::ListValue* unused_args);
   void HandleOpenArcStorage(const base::ListValue* unused_args);
+  void HandleClearDriveCache(const base::ListValue* unused_args);
 
   // Requests updating disk space information.
   void UpdateSizeStat();
@@ -44,12 +45,21 @@
   // Callback to update the UI about the size of Downloads directory.
   void OnGetDownloadsSize(int64_t size);
 
+  // Requests updating the size of Drive Cache.
+  void UpdateDriveCacheSize();
+
+  // Callback to update the UI about the size of Drive Cache.
+  void OnGetDriveCacheSize(int64_t size);
+
   // Requests updating the space size used by Android apps and cache.
   void UpdateArcSize();
 
   // Callback to update the UI about Android apps and cache.
   void OnGetArcSize(bool succeeded, arc::mojom::ApplicationsSizePtr size);
 
+  // Callback called when clearing Drive cache is done.
+  void OnClearDriveCacheDone(bool success);
+
   base::WeakPtrFactory<StorageManagerHandler> weak_ptr_factory_;
 
   DISALLOW_COPY_AND_ASSIGN(StorageManagerHandler);