Chromium Code Reviews| Index: chrome/browser/apps/drive/drive_app_provider.cc |
| diff --git a/chrome/browser/apps/drive/drive_app_provider.cc b/chrome/browser/apps/drive/drive_app_provider.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..8aca67a5373d7e18aee5b66553ff1588c06bfc07 |
| --- /dev/null |
| +++ b/chrome/browser/apps/drive/drive_app_provider.cc |
| @@ -0,0 +1,229 @@ |
| +// Copyright 2014 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. |
| + |
| +#include "chrome/browser/apps/drive/drive_app_provider.h" |
| + |
| +#include <vector> |
| + |
| +#include "base/bind.h" |
| +#include "base/bind_helpers.h" |
| +#include "base/logging.h" |
| +#include "base/message_loop/message_loop.h" |
| +#include "base/stl_util.h" |
| +#include "chrome/browser/apps/drive/drive_app_converter.h" |
| +#include "chrome/browser/apps/drive/drive_app_mapping.h" |
| +#include "chrome/browser/apps/drive/drive_service_bridge.h" |
| +#include "chrome/browser/drive/drive_app_registry.h" |
| +#include "chrome/browser/extensions/extension_service.h" |
| +#include "chrome/browser/extensions/install_tracker.h" |
| +#include "chrome/browser/extensions/install_tracker_factory.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/common/extensions/manifest_handlers/app_launch_info.h" |
| +#include "extensions/browser/extension_registry.h" |
| +#include "extensions/browser/extension_system.h" |
| +#include "extensions/common/extension.h" |
| + |
| +using extensions::Extension; |
| +using extensions::ExtensionRegistry; |
| + |
| +namespace { |
| + |
| +void IgnoreUninstallResult(google_apis::GDataErrorCode) { |
| +} |
| + |
| +} // namespace |
| + |
| +DriveAppProvider::DriveAppProvider(Profile* profile) |
| + : profile_(profile), |
| + service_bridge_(DriveServiceBridge::Create(profile).Pass()), |
| + mapping_(new DriveAppMapping(profile->GetPrefs())), |
| + weak_ptr_factory_(this) { |
| + service_bridge_->GetAppRegistry()->AddObserver(this); |
| + extensions::InstallTracker::Get(profile_)->AddObserver(this); |
| +} |
| + |
| +DriveAppProvider::~DriveAppProvider() { |
| + extensions::InstallTracker::Get(profile_)->RemoveObserver(this); |
| + service_bridge_->GetAppRegistry()->RemoveObserver(this); |
| +} |
| + |
| +// static |
| +void DriveAppProvider::AppendDependsOnFactories( |
| + std::set<BrowserContextKeyedServiceFactory*>* factories) { |
| + factories->insert(extensions::InstallTrackerFactory::GetInstance()); |
| + DriveServiceBridge::AppendDependsOnFactories(factories); |
| +} |
| + |
| +void DriveAppProvider::SetDriveServiceBridgeForTest( |
| + scoped_ptr<DriveServiceBridge> test_bridge) { |
| + service_bridge_->GetAppRegistry()->RemoveObserver(this); |
| + service_bridge_ = test_bridge.Pass(); |
| + service_bridge_->GetAppRegistry()->AddObserver(this); |
| +} |
| + |
| +void DriveAppProvider::UpdateMappingAndExtensionSystem( |
| + const std::string& drive_app_id, |
| + const Extension* new_app) { |
| + const std::string& new_chrome_app_id = new_app->id(); |
| + |
| + const std::string existing_chrome_app_id = |
| + mapping_->GetChromeApp(drive_app_id); |
| + if (existing_chrome_app_id == new_chrome_app_id) |
| + return; |
| + |
| + mapping_->Add(drive_app_id, new_chrome_app_id); |
| + |
| + const Extension* existing_app = |
| + ExtensionRegistry::Get(profile_)->GetExtensionById( |
| + existing_chrome_app_id, ExtensionRegistry::EVERYTHING); |
| + if (existing_app && existing_app->from_bookmark()) { |
|
benwells
2014/06/11 00:15:20
This from_bookmark doesn't differentiate between u
xiyuan
2014/06/11 04:52:34
You are correct that the code could uninstall user
benwells
2014/06/12 00:16:02
Actually I didn't think it was possible, and I don
xiyuan
2014/06/12 00:53:19
The id of the URL apps are not random and derived
benwells
2014/06/12 06:28:59
Sorry I still don't get it (I'm probably being dum
xiyuan
2014/06/12 19:30:20
This is where we differ. Suppose user installs an
benwells
2014/06/13 02:03:01
Thanks for the detailed explanation, I get it now
|
| + extensions::ExtensionSystem::Get(profile_) |
| + ->extension_service() |
| + ->UninstallExtension(existing_chrome_app_id, false, NULL); |
| + } |
| +} |
| + |
| +void DriveAppProvider::ProcessDeferredOnExtensionInstalled( |
| + const std::string drive_app_id, |
| + const std::string chrome_app_id) { |
| + const Extension* app = ExtensionRegistry::Get(profile_)->GetExtensionById( |
| + chrome_app_id, ExtensionRegistry::EVERYTHING); |
| + if (!app) |
| + return; |
| + |
| + UpdateMappingAndExtensionSystem(drive_app_id, app); |
| +} |
| + |
| +void DriveAppProvider::SchedulePendingConverters() { |
| + if (pending_converters_.empty()) |
| + return; |
| + |
| + if (!pending_converters_.front()->IsStarted()) |
| + pending_converters_.front()->Start(); |
| +} |
| + |
| +void DriveAppProvider::OnLocalAppConverted(const DriveAppConverter* converter, |
| + bool success) { |
| + DCHECK_EQ(pending_converters_.front(), converter); |
| + |
| + if (success) { |
| + UpdateMappingAndExtensionSystem(converter->app_info().app_id, |
| + converter->app()); |
| + } else { |
| + LOG(WARNING) << "Failed to convert drive app to web app, " |
| + << "drive app id= " << converter->app_info().app_id |
| + << ", name=" << converter->app_info().app_name; |
| + } |
| + |
| + pending_converters_.erase(pending_converters_.begin()); |
| + SchedulePendingConverters(); |
| +} |
| + |
| +bool DriveAppProvider::IsDriveAppUpToDate( |
| + const drive::DriveAppInfo& drive_app) const { |
| + const std::string& url_app_id = mapping_->GetChromeApp(drive_app.app_id); |
| + if (url_app_id.empty()) |
| + return false; |
| + |
| + const Extension* url_app = ExtensionRegistry::Get(profile_)->GetExtensionById( |
| + url_app_id, ExtensionRegistry::EVERYTHING); |
| + if (!url_app) |
| + return false; |
| + |
| + return drive_app.app_name == url_app->name() && |
|
benwells
2014/06/11 00:15:20
What if there is a matching app but it is not a UR
xiyuan
2014/06/11 04:52:34
Sounds good. Will rename.
xiyuan
2014/06/11 21:36:43
Renamed to IsMappedUrlAppUpToDate because we shoul
|
| + drive_app.create_url == |
| + extensions::AppLaunchInfo::GetLaunchWebURL(url_app); |
| +} |
| + |
| +void DriveAppProvider::AddOrUpdateDriveApp( |
| + const drive::DriveAppInfo& drive_app) { |
| + const Extension* chrome_app = |
| + ExtensionRegistry::Get(profile_)->GetExtensionById( |
| + drive_app.product_id, ExtensionRegistry::EVERYTHING); |
| + if (chrome_app) { |
| + UpdateMappingAndExtensionSystem(drive_app.app_id, chrome_app); |
| + return; |
| + } |
| + |
| + if (IsDriveAppUpToDate(drive_app)) |
| + return; |
| + |
| + ScopedVector<DriveAppConverter>::iterator it = pending_converters_.begin(); |
| + while (it != pending_converters_.end()) { |
| + if (!(*it)->IsStarted() && (*it)->app_info().app_id == drive_app.app_id) { |
|
benwells
2014/06/11 00:15:20
What will happen if there is a converter in progre
xiyuan
2014/06/11 04:52:34
The "while" loop here is just an optimization. The
benwells
2014/06/13 02:03:01
If two of these do happen now, the second one will
xiyuan
2014/06/13 21:01:15
The current code does not change the "generated" f
|
| + it = pending_converters_.erase(it); |
| + } else { |
| + ++it; |
| + } |
| + } |
| + |
| + pending_converters_.push_back( |
| + new DriveAppConverter(profile_, |
| + drive_app, |
| + base::Bind(&DriveAppProvider::OnLocalAppConverted, |
| + base::Unretained(this)))); |
| +} |
| + |
| +void DriveAppProvider::RemoveDriveApp(const std::string& drive_app_id) { |
|
benwells
2014/06/11 00:15:21
Nit: I think a better name would be ProcessRemoved
xiyuan
2014/06/11 04:52:34
Will do.
|
| + const std::string chrome_app_id = mapping_->GetChromeApp(drive_app_id); |
| + mapping_->Remove(drive_app_id); |
| + |
| + if (chrome_app_id.empty()) |
| + return; |
| + |
| + const Extension* existing_app = |
| + ExtensionRegistry::Get(profile_) |
| + ->GetExtensionById(chrome_app_id, ExtensionRegistry::EVERYTHING); |
| + if (!existing_app || !existing_app->from_bookmark()) |
| + return; |
| + |
| + extensions::ExtensionSystem::Get(profile_) |
| + ->extension_service() |
| + ->UninstallExtension(chrome_app_id, false, NULL); |
| +} |
| + |
| +void DriveAppProvider::OnDriveAppRegistryUpdated() { |
|
benwells
2014/06/11 00:15:20
This is the only place that drive_apps_ is initial
xiyuan
2014/06/11 04:52:34
It actually ties to the ProfileOAuth2TokenService'
|
| + service_bridge_->GetAppRegistry()->GetAppList(&drive_apps_); |
| + |
| + IdSet current_ids; |
| + for (size_t i = 0; i < drive_apps_.size(); ++i) |
| + current_ids.insert(drive_apps_[i].app_id); |
| + |
| + const IdSet existing_ids = mapping_->GetDriveAppIds(); |
| + const IdSet ids_to_remove = |
| + base::STLSetDifference<IdSet>(existing_ids, current_ids); |
| + for (IdSet::const_iterator it = ids_to_remove.begin(); |
| + it != ids_to_remove.end(); |
| + ++it) { |
| + RemoveDriveApp(*it); |
| + } |
| + |
| + for (size_t i = 0; i < drive_apps_.size(); ++i) { |
| + AddOrUpdateDriveApp(drive_apps_[i]); |
| + } |
| + SchedulePendingConverters(); |
| +} |
| + |
| +void DriveAppProvider::OnExtensionInstalled(const Extension* extension) { |
| + for (size_t i = 0; i < drive_apps_.size(); ++i) { |
| + if (drive_apps_[i].product_id == extension->id()) { |
| + base::MessageLoop::current()->PostTask( |
|
benwells
2014/06/11 00:15:21
Why is this deferred? Could you add a comment expl
xiyuan
2014/06/11 04:52:34
Will add a comment. This is because we touch exten
|
| + FROM_HERE, |
| + base::Bind(&DriveAppProvider::ProcessDeferredOnExtensionInstalled, |
| + weak_ptr_factory_.GetWeakPtr(), |
| + drive_apps_[i].app_id, |
| + extension->id())); |
| + return; |
| + } |
| + } |
| +} |
| + |
| +void DriveAppProvider::OnExtensionUninstalled(const Extension* extension) { |
| + std::string drive_app_id = mapping_->GetDriveApp(extension->id()); |
| + if (drive_app_id.empty()) |
| + return; |
| + |
| + service_bridge_->GetAppRegistry()->UninstallApp( |
| + drive_app_id, base::Bind(&IgnoreUninstallResult)); |
| +} |