Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / media_galleries / media_galleries_preferences.cc
index 5575cc6..7fe1a34 100644 (file)
 #include "chrome/browser/browser_process.h"
 #include "chrome/browser/extensions/api/media_galleries_private/media_galleries_private_api.h"
 #include "chrome/browser/extensions/extension_service.h"
-#include "chrome/browser/extensions/extension_system.h"
 #include "chrome/browser/media_galleries/fileapi/iapps_finder.h"
 #include "chrome/browser/media_galleries/fileapi/picasa_finder.h"
 #include "chrome/browser/media_galleries/media_file_system_registry.h"
 #include "chrome/browser/media_galleries/media_galleries_histograms.h"
 #include "chrome/browser/profiles/profile.h"
-#include "chrome/browser/storage_monitor/media_storage_util.h"
-#include "chrome/browser/storage_monitor/storage_monitor.h"
 #include "chrome/common/chrome_paths.h"
 #include "chrome/common/extensions/permissions/media_galleries_permission.h"
 #include "chrome/common/pref_names.h"
+#include "components/storage_monitor/media_storage_util.h"
+#include "components/storage_monitor/storage_monitor.h"
 #include "components/user_prefs/pref_registry_syncable.h"
 #include "content/public/browser/browser_thread.h"
+#include "extensions/browser/extension_system.h"
 #include "extensions/browser/pref_names.h"
 #include "extensions/common/extension.h"
 #include "extensions/common/extension_set.h"
@@ -63,8 +63,8 @@ const char kMediaGalleriesVendorNameKey[] = "vendorName";
 const char kMediaGalleriesModelNameKey[] = "modelName";
 const char kMediaGalleriesSizeKey[] = "totalSize";
 const char kMediaGalleriesLastAttachTimeKey[] = "lastAttachTime";
+const char kMediaGalleriesScanAudioCountKey[] = "audioCount";
 const char kMediaGalleriesScanImageCountKey[] = "imageCount";
-const char kMediaGalleriesScanMusicCountKey[] = "musicCount";
 const char kMediaGalleriesScanVideoCountKey[] = "videoCount";
 const char kMediaGalleriesPrefsVersionKey[] = "preferencesVersion";
 
@@ -180,8 +180,8 @@ bool PopulateGalleryPrefInfoFromDictionary(
   double total_size_in_bytes = 0.0;
   double last_attach_time = 0.0;
   bool volume_metadata_valid = false;
+  int audio_count = 0;
   int image_count = 0;
-  int music_count = 0;
   int video_count = 0;
   int prefs_version = 0;
 
@@ -203,13 +203,16 @@ bool PopulateGalleryPrefInfoFromDictionary(
     volume_metadata_valid = true;
   }
 
-  if (type == MediaGalleryPrefInfo::kScanResult &&
+  if (dict.GetInteger(kMediaGalleriesScanAudioCountKey, &audio_count) &&
       dict.GetInteger(kMediaGalleriesScanImageCountKey, &image_count) &&
-      dict.GetInteger(kMediaGalleriesScanMusicCountKey, &music_count) &&
       dict.GetInteger(kMediaGalleriesScanVideoCountKey, &video_count)) {
+    out_gallery_info->audio_count = audio_count;
     out_gallery_info->image_count = image_count;
-    out_gallery_info->music_count = music_count;
     out_gallery_info->video_count = video_count;
+  } else {
+    out_gallery_info->audio_count = 0;
+    out_gallery_info->image_count = 0;
+    out_gallery_info->video_count = 0;
   }
 
   out_gallery_info->pref_id = pref_id;
@@ -249,9 +252,9 @@ base::DictionaryValue* CreateGalleryPrefInfoDictionary(
     dict->SetString(kMediaGalleriesDisplayNameKey, gallery.display_name);
   }
 
-  if (gallery.type == MediaGalleryPrefInfo::kScanResult) {
+  if (gallery.audio_count || gallery.image_count || gallery.video_count) {
+    dict->SetInteger(kMediaGalleriesScanAudioCountKey, gallery.audio_count);
     dict->SetInteger(kMediaGalleriesScanImageCountKey, gallery.image_count);
-    dict->SetInteger(kMediaGalleriesScanMusicCountKey, gallery.music_count);
     dict->SetInteger(kMediaGalleriesScanVideoCountKey, gallery.video_count);
   }
 
@@ -332,8 +335,8 @@ MediaGalleryPrefInfo::MediaGalleryPrefInfo()
       type(kInvalidType),
       total_size_in_bytes(0),
       volume_metadata_valid(false),
+      audio_count(0),
       image_count(0),
-      music_count(0),
       video_count(0),
       prefs_version(0) {
 }
@@ -362,11 +365,10 @@ base::string16 MediaGalleryPrefInfo::GetGalleryDisplayName() const {
 
 #if defined(OS_CHROMEOS)
     // See chrome/browser/chromeos/fileapi/file_system_backend.cc
-    base::FilePath home_path;
-    if (PathService::Get(base::DIR_HOME, &home_path)) {
-      home_path = home_path.AppendASCII("Downloads");
+    base::FilePath download_path;
+    if (PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS_SAFE, &download_path)) {
       base::FilePath relative;
-      if (home_path.AppendRelativePath(path, &relative))
+      if (download_path.AppendRelativePath(path, &relative))
         return relative.LossyDisplayName();
     }
     return path.BaseName().LossyDisplayName();
@@ -487,7 +489,7 @@ Profile* MediaGalleriesPreferences::profile() { return profile_; }
 
 void MediaGalleriesPreferences::OnInitializationCallbackReturned() {
   DCHECK(!IsInitialized());
-  DCHECK(pre_initialization_callbacks_waiting_ > 0);
+  DCHECK_GT(pre_initialization_callbacks_waiting_, 0);
   if (--pre_initialization_callbacks_waiting_ == 0)
     FinishInitialization();
 }
@@ -754,14 +756,14 @@ MediaGalleryPrefId MediaGalleriesPreferences::AddGallery(
     const base::string16& model_name,
     uint64 total_size_in_bytes,
     base::Time last_attach_time,
+    int audio_count,
     int image_count,
-    int music_count,
     int video_count) {
   DCHECK(IsInitialized());
   return AddGalleryInternal(device_id, base::string16(), relative_path,
                             type, volume_label, vendor_name, model_name,
                             total_size_in_bytes, last_attach_time, true,
-                            image_count, music_count, video_count,
+                            audio_count, image_count, video_count,
                             kCurrentPrefsVersion);
 }
 
@@ -771,7 +773,7 @@ MediaGalleryPrefId MediaGalleriesPreferences::AddGalleryInternal(
     const base::string16& volume_label, const base::string16& vendor_name,
     const base::string16& model_name, uint64 total_size_in_bytes,
     base::Time last_attach_time, bool volume_metadata_valid,
-    int image_count, int music_count, int video_count, int prefs_version) {
+    int audio_count, int image_count, int video_count, int prefs_version) {
   DCHECK(type == MediaGalleryPrefInfo::kUserAdded ||
          type == MediaGalleryPrefInfo::kAutoDetected ||
          type == MediaGalleryPrefInfo::kScanResult);
@@ -821,8 +823,10 @@ MediaGalleryPrefId MediaGalleriesPreferences::AddGalleryInternal(
          (existing.last_attach_time != last_attach_time));
 
     bool update_scan_counts =
-      (new_type == MediaGalleryPrefInfo::kScanResult) &&
-      (image_count > 0 || music_count > 0 || video_count > 0);
+      new_type != MediaGalleryPrefInfo::kRemovedScan &&
+      new_type != MediaGalleryPrefInfo::kBlackListed &&
+      (audio_count > 0 || image_count > 0 || video_count > 0 ||
+       existing.audio_count || existing.image_count || existing.video_count);
 
     if (!update_gallery_name && !update_gallery_type &&
         !update_gallery_metadata && !update_scan_counts)
@@ -854,8 +858,8 @@ MediaGalleryPrefId MediaGalleriesPreferences::AddGalleryInternal(
                           last_attach_time.ToInternalValue());
         }
         if (update_scan_counts) {
+          dict->SetInteger(kMediaGalleriesScanAudioCountKey, audio_count);
           dict->SetInteger(kMediaGalleriesScanImageCountKey, image_count);
-          dict->SetInteger(kMediaGalleriesScanMusicCountKey, music_count);
           dict->SetInteger(kMediaGalleriesScanVideoCountKey, video_count);
         }
         dict->SetInteger(kMediaGalleriesPrefsVersionKey, prefs_version);
@@ -887,8 +891,8 @@ MediaGalleryPrefId MediaGalleriesPreferences::AddGalleryInternal(
   gallery_info.total_size_in_bytes = total_size_in_bytes;
   gallery_info.last_attach_time = last_attach_time;
   gallery_info.volume_metadata_valid = volume_metadata_valid;
+  gallery_info.audio_count = audio_count;
   gallery_info.image_count = image_count;
-  gallery_info.music_count = music_count;
   gallery_info.video_count = video_count;
   gallery_info.prefs_version = prefs_version;
 
@@ -927,14 +931,23 @@ MediaGalleryPrefId MediaGalleriesPreferences::AddGalleryByPath(
                             kCurrentPrefsVersion);
 }
 
-void MediaGalleriesPreferences::ForgetGalleryById(MediaGalleryPrefId pref_id) {
+void MediaGalleriesPreferences::ForgetGalleryById(MediaGalleryPrefId id) {
+  EraseOrBlacklistGalleryById(id, false);
+}
+
+void MediaGalleriesPreferences::EraseGalleryById(MediaGalleryPrefId id) {
+  EraseOrBlacklistGalleryById(id, true);
+}
+
+void MediaGalleriesPreferences::EraseOrBlacklistGalleryById(
+    MediaGalleryPrefId id, bool erase) {
   DCHECK(IsInitialized());
   PrefService* prefs = profile_->GetPrefs();
   scoped_ptr<ListPrefUpdate> update(new ListPrefUpdate(
       prefs, prefs::kMediaGalleriesRememberedGalleries));
   base::ListValue* list = update->Get();
 
-  if (!ContainsKey(known_galleries_, pref_id))
+  if (!ContainsKey(known_galleries_, id))
     return;
 
   for (base::ListValue::iterator iter = list->begin();
@@ -942,10 +955,10 @@ void MediaGalleriesPreferences::ForgetGalleryById(MediaGalleryPrefId pref_id) {
     base::DictionaryValue* dict;
     MediaGalleryPrefId iter_id;
     if ((*iter)->GetAsDictionary(&dict) && GetPrefId(*dict, &iter_id) &&
-        pref_id == iter_id) {
-      RemoveGalleryPermissionsFromPrefs(pref_id);
+        id == iter_id) {
+      RemoveGalleryPermissionsFromPrefs(id);
       MediaGalleryPrefInfo::Type type;
-      if (GetType(*dict, &type) &&
+      if (!erase && GetType(*dict, &type) &&
           (type == MediaGalleryPrefInfo::kAutoDetected ||
            type == MediaGalleryPrefInfo::kScanResult)) {
         if (type == MediaGalleryPrefInfo::kAutoDetected) {
@@ -954,8 +967,8 @@ void MediaGalleriesPreferences::ForgetGalleryById(MediaGalleryPrefId pref_id) {
         } else {
           dict->SetString(kMediaGalleriesTypeKey,
                           kMediaGalleriesTypeRemovedScanValue);
+          dict->SetInteger(kMediaGalleriesScanAudioCountKey, 0);
           dict->SetInteger(kMediaGalleriesScanImageCountKey, 0);
-          dict->SetInteger(kMediaGalleriesScanMusicCountKey, 0);
           dict->SetInteger(kMediaGalleriesScanVideoCountKey, 0);
         }
       } else {
@@ -966,12 +979,44 @@ void MediaGalleriesPreferences::ForgetGalleryById(MediaGalleryPrefId pref_id) {
       InitFromPrefs();
       FOR_EACH_OBSERVER(GalleryChangeObserver,
                         gallery_change_observers_,
-                        OnGalleryRemoved(this, pref_id));
+                        OnGalleryRemoved(this, id));
       return;
     }
   }
 }
 
+bool MediaGalleriesPreferences::NonAutoGalleryHasPermission(
+    MediaGalleryPrefId id) const {
+  DCHECK(IsInitialized());
+  DCHECK(!ContainsKey(known_galleries_, id) ||
+         known_galleries_.find(id)->second.type !=
+             MediaGalleryPrefInfo::kAutoDetected);
+  ExtensionPrefs* prefs = GetExtensionPrefs();
+  const base::DictionaryValue* extensions =
+      prefs->pref_service()->GetDictionary(extensions::pref_names::kExtensions);
+  if (!extensions)
+    return true;
+
+  for (base::DictionaryValue::Iterator iter(*extensions); !iter.IsAtEnd();
+       iter.Advance()) {
+    if (!extensions::Extension::IdIsValid(iter.key())) {
+      NOTREACHED();
+      continue;
+    }
+    std::vector<MediaGalleryPermission> permissions =
+        GetGalleryPermissionsFromPrefs(iter.key());
+    for (std::vector<MediaGalleryPermission>::const_iterator it =
+             permissions.begin(); it != permissions.end(); ++it) {
+      if (it->pref_id == id) {
+        if (it->has_permission)
+          return true;
+        break;
+      }
+    }
+  }
+  return false;
+}
+
 MediaGalleryPrefIdSet MediaGalleriesPreferences::GalleriesForExtension(
     const extensions::Extension& extension) const {
   DCHECK(IsInitialized());
@@ -1046,6 +1091,18 @@ const MediaGalleriesPrefInfoMap& MediaGalleriesPreferences::known_galleries()
   return known_galleries_;
 }
 
+base::Time MediaGalleriesPreferences::GetLastScanCompletionTime() const {
+  int64 last_scan_time_internal =
+      profile_->GetPrefs()->GetInt64(prefs::kMediaGalleriesLastScanTime);
+  return base::Time::FromInternalValue(last_scan_time_internal);
+}
+
+void MediaGalleriesPreferences::SetLastScanCompletionTime(
+    const base::Time& time) {
+  profile_->GetPrefs()->SetInt64(prefs::kMediaGalleriesLastScanTime,
+                                 time.ToInternalValue());
+}
+
 void MediaGalleriesPreferences::Shutdown() {
   weak_factory_.InvalidateWeakPtrs();
   profile_ = NULL;
@@ -1067,6 +1124,10 @@ void MediaGalleriesPreferences::RegisterProfilePrefs(
       prefs::kMediaGalleriesUniqueId,
       kInvalidMediaGalleryPrefId + 1,
       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
+  registry->RegisterInt64Pref(
+      prefs::kMediaGalleriesLastScanTime,
+      base::Time().ToInternalValue(),
+      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
 }
 
 bool MediaGalleriesPreferences::SetGalleryPermissionInPrefs(