Home | History | Annotate | Download | only in app_runtime
      1 // Copyright (c) 2012 The Chromium Authors. 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 "chrome/browser/extensions/api/app_runtime/app_runtime_api.h"
      6 
      7 #include "base/json/json_writer.h"
      8 #include "base/strings/string16.h"
      9 #include "base/strings/string_number_conversions.h"
     10 #include "base/strings/utf_string_conversions.h"
     11 #include "base/values.h"
     12 #include "chrome/browser/extensions/api/file_handlers/app_file_handler_util.h"
     13 #include "chrome/browser/extensions/event_names.h"
     14 #include "chrome/browser/extensions/event_router.h"
     15 #include "chrome/browser/extensions/extension_system.h"
     16 #include "chrome/browser/profiles/profile.h"
     17 #include "chrome/common/extensions/extension.h"
     18 #include "content/public/browser/web_contents.h"
     19 #include "url/gurl.h"
     20 
     21 namespace extensions {
     22 
     23 namespace {
     24 
     25 using event_names::kOnLaunched;
     26 using event_names::kOnRestarted;
     27 
     28 void DispatchOnLaunchedEventImpl(const std::string& extension_id,
     29                                  scoped_ptr<base::ListValue> args,
     30                                  Profile* profile) {
     31   extensions::ExtensionSystem* system =
     32       extensions::ExtensionSystem::Get(profile);
     33   scoped_ptr<Event> event(new Event(kOnLaunched, args.Pass()));
     34   event->restrict_to_profile = profile;
     35   system->event_router()->DispatchEventWithLazyListener(extension_id,
     36                                                         event.Pass());
     37 }
     38 
     39 }  // anonymous namespace
     40 
     41 // static.
     42 void AppEventRouter::DispatchOnLaunchedEvent(
     43     Profile* profile, const Extension* extension) {
     44   scoped_ptr<base::ListValue> arguments(new base::ListValue());
     45   DispatchOnLaunchedEventImpl(extension->id(), arguments.Pass(), profile);
     46 }
     47 
     48 // static.
     49 void AppEventRouter::DispatchOnRestartedEvent(Profile* profile,
     50                                               const Extension* extension) {
     51   scoped_ptr<base::ListValue> arguments(new base::ListValue());
     52   scoped_ptr<Event> event(new Event(kOnRestarted, arguments.Pass()));
     53   event->restrict_to_profile = profile;
     54   extensions::ExtensionSystem::Get(profile)->event_router()->
     55       DispatchEventToExtension(extension->id(), event.Pass());
     56 }
     57 
     58 // static.
     59 void AppEventRouter::DispatchOnLaunchedEventWithFileEntry(
     60     Profile* profile, const Extension* extension,
     61     const std::string& handler_id, const std::string& mime_type,
     62     const extensions::app_file_handler_util::GrantedFileEntry& file_entry) {
     63   scoped_ptr<base::ListValue> args(new base::ListValue());
     64   base::DictionaryValue* launch_data = new base::DictionaryValue();
     65   launch_data->SetString("id", handler_id);
     66   base::DictionaryValue* launch_item = new base::DictionaryValue;
     67   launch_item->SetString("fileSystemId", file_entry.filesystem_id);
     68   launch_item->SetString("baseName", file_entry.registered_name);
     69   launch_item->SetString("mimeType", mime_type);
     70   launch_item->SetString("entryId", file_entry.id);
     71   base::ListValue* items = new base::ListValue;
     72   items->Append(launch_item);
     73   launch_data->Set("items", items);
     74   args->Append(launch_data);
     75   DispatchOnLaunchedEventImpl(extension->id(), args.Pass(), profile);
     76 }
     77 
     78 }  // namespace extensions
     79