d8bf715f1638b0ded9b8ce2c5bf68ee3e9a3d860
[platform/framework/web/crosswalk.git] / src / xwalk / application / browser / application_service_tizen.cc
1 // Copyright (c) 2014 Intel Corporation. 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
5 #include "xwalk/application/browser/application_service_tizen.h"
6
7 #include <string>
8 #include <vector>
9
10 #include "xwalk/application/browser/application.h"
11 #include "xwalk/application/common/application_storage.h"
12 #include "xwalk/application/common/id_util.h"
13 #include "xwalk/runtime/browser/runtime_context.h"
14
15 namespace xwalk {
16
17 namespace application {
18
19 namespace {
20
21 const base::FilePath::CharType kApplicationDataDirName[] =
22     FILE_PATH_LITERAL("Storage/ext");
23
24 base::FilePath GetStoragePartitionPath(
25     const base::FilePath& base_path, const std::string& app_id) {
26   return base_path.Append(kApplicationDataDirName).Append(app_id);
27 }
28
29 void CollectUnusedStoragePartitions(RuntimeContext* context,
30                                     ApplicationStorage* storage) {
31   std::vector<std::string> app_ids;
32   if (!storage->GetInstalledApplicationIDs(app_ids))
33     return;
34
35   scoped_ptr<base::hash_set<base::FilePath> > active_paths(
36       new base::hash_set<base::FilePath>()); // NOLINT
37
38   for (unsigned i = 0; i < app_ids.size(); ++i) {
39     active_paths->insert(
40         GetStoragePartitionPath(context->GetPath(), app_ids.at(i)));
41   }
42
43   content::BrowserContext::GarbageCollectStoragePartitions(
44       context, active_paths.Pass(), base::Bind(&base::DoNothing));
45 }
46
47 }  // namespace
48
49 ApplicationServiceTizen::ApplicationServiceTizen(
50     RuntimeContext* runtime_context)
51   : ApplicationService(runtime_context),
52     application_storage_(new ApplicationStorage(runtime_context->GetPath())) {
53   CollectUnusedStoragePartitions(runtime_context, application_storage_.get());
54 }
55
56 ApplicationServiceTizen::~ApplicationServiceTizen() {
57 }
58
59 Application* ApplicationServiceTizen::LaunchFromAppID(
60     const std::string& id, const Application::LaunchParams& params) {
61   if (!IsValidApplicationID(id)) {
62      LOG(ERROR) << "The input parameter is not a valid app id: " << id;
63      return NULL;
64   }
65
66   scoped_refptr<ApplicationData> app_data =
67     application_storage_->GetApplicationData(id);
68   if (!app_data) {
69     LOG(ERROR) << "Application with id " << id << " is not installed.";
70     return NULL;
71   }
72
73   return Launch(app_data, params);
74 }
75
76 }  // namespace application
77 }  // namespace xwalk